根據我的理解,在test_main_version_exception單元測驗中,compare_version.version_match()函式將引發一個例外,理想情況下這個例外應該被except塊捕獲。但是由于某些原因,流程沒有進入except塊。
(第一塊似乎作業正常)
誰能幫幫我?
有人能幫我解決這個問題嗎?我們也歡迎任何關于改進這些測驗的建議。
main.py檔案
try:
is_version_same = compare_version.version_match()
if is_version_same:
LOGGER.info("Upgrade not required")
sys.exit()
except compare_version.VersionError as error: #custom error[/span]。
####,當我運行test_main_version_exception(mocker)時,這兩條陳述句沒有得到覆寫:
LOGGER.exception("exception occurred in compare_version: %s"/span>, error)
sys.exit(f "exception occurred in compare_version: {error}")
UNIT TESTS:
def test_main_same_version(mocker)。
mocker.patch(
"compare_version.version_match"。
return_value=True。
)
with pytest.raises(SystemExit) as ex:
main.main()
assert ex.type == SystemExit
def test_main_version_exception(mocker)。
mocker.patch(
"compare_version.version_match"。
side_effect=VersionError,
)
with pytest.raises(VersionError) as ex:
main.main()
assert ex.type == VersionError
uj5u.com熱心網友回復:
你必須通過mocker.patch("main.compare_version.version_match", ...)對main.py中使用的compare_version進行專門修補,不管它是否是:
- 匯入到main.py。
from some_file import compare_version - 或者在main.py中定義。
class compare_version。 # sample implementation only as you haven't included this in the question. version_match = lambda: None。 VersionError = 例外
如檔案:
a.py -> 定義了一些類 b.py -> from a import SomeClass -> some_function instantiates SomeClass現在我們想測驗some_function,但我們想用patch()來模擬出SomeClass。如果我們使用patch()來模擬出a.SomeClass,那么它將對我們的測驗沒有影響;模塊b已經有一個對真正的SomeClass的參考,看起來我們的修補沒有影響。
關鍵是要在使用 SomeClass 的地方打上補丁(或在它被查找的地方)。在這種情況下,some_function實際上將在模塊b中查找SomeClass,我們在那里匯入了它。補丁應該是這樣的:
@patch('b.SomeClass')
另外,你的斷言with pytest.raises(VersionError) as ex:是不正確的,因為你的實作不再引發VersionError,相反,它抓住了那個錯誤并引發了SystemExit。所以你需要在這里斷言的是它是否引發了SystemExit。如果你真正需要的是VersionError,那么就替換這一行:
sys.exit(f "exception occurred in compare_version: {error}")
致:
raise # Will re-raise the previous exception, which is the VersionError
你可能想把樣本運行作為參考。
main.py
import sys
# # 是否在另一個檔案中定義了它。
# from some_file import compare_version
# 無論是在這里定義的
class compare_version:
version_match = lambda: None: 版本錯誤 = ValueError
VersionError = ValueError
def main()。
try:
is_version_same = compare_version.version_match()
if is_version_same:
print("Upgrade not required"/span>)
sys.exit()
except compare_version.VersionError as error: #custom error[/span]。
print("exception occurred in compare_version: %s"/span>, error)
# 如果你想讓這個塊引發SystemExit的話
sys.exit(f "exception occurred in compare_version: {error}")
# If you want this block to re-raise VersionError.
# raise。
test_main.py
import pytest
import main
def test_main_same_version(mocker)。
mocker.patch(
"main.compare_version.version_match"。
return_value=True。
)
with pytest.raises(SystemExit) as ex:
main.main()
assert ex.type == SystemExit
def test_main_version_exception(mocker)。
mocker.patch(
"main.compare_version.version_match"。
side_effect=main.compare_version.VersionError,
)
with pytest.raises(SystemExit) as ex:
main.main()
assert ex.type == SystemExit
輸出
$ pytest -q -rP
================================================================================================= PASSES ==================================================================================================
_________________________________________________________________________________________ test_main_same_version __________________________________________________________________________________________
------------------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------------------
升級不需要。
_______________________________________________________________________________________ test_main_version_exception _______________________________________________________________________________________
------------------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------------------
例外發生 in compare_version: %s
2通過in 0.04s
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/310306.html
標籤:
