跳过所有 Python 测试中的异常
Skipping an exception in all Python tests
我正在使用 Python 的 unittest
和 pytest
来针对第三方 API.
集成测试库
一些 API 调用暂时返回一个错误,这在我的代码中引发了一个特定的异常。此行为在代码中没有问题。
但是,与其让测试失败,我宁愿跳过这些临时错误。
我有超过 150 个测试。而不是像这样重写每个测试:
class TestMyLibrary(unittest.TestCase):
def test_some_test(self):
try:
// run the test as normal
// assert the normal behaviour
except SomeException:
// skip the test
def test_some_other_test(self):
try:
// run the test as normal
// assert the normal behaviour
except SomeException:
// skip the test
我能否以某种方式将它们全部包装在 class 级别或类似级别?
如果您预计会出现此异常,为什么不在它应该引发的时候检查它呢?
您可以使用:
pytest.raises(Exceptiontype, Foo())
这可以用装饰器来完成。例如:
def handle_lastfm_exceptions(f):
def wrapper(*args, **kw):
try:
return f(*args, **kw)
except pylast.WSError as e:
if (str(e) == "Invalid Method - "
"No method with that name in this package"):
msg = "Ignore broken Last.fm API: " + str(e)
print(msg)
pytest.skip(msg)
else:
raise(e)
return wrapper
然后对有问题的函数进行修饰:
class TestMyLibrary(unittest.TestCase):
@handle_lastfm_exceptions
def test_some_bad_test(self):
// run the test as normal
// assert the normal behaviour
def test_some_good_test(self):
// run the test as normal
// assert the normal behaviour
有同样的问题(不稳定的第 3 方库,等待修复...)。结果是这样的:
def pytest_runtest_makereport(item, call):
from _pytest.runner import pytest_runtest_makereport as orig_pytest_runtest_makereport
tr = orig_pytest_runtest_makereport(item, call)
if call.excinfo is not None:
if call.excinfo.type == SomeExceptionFromLibrary:
tr.outcome = 'skipped'
tr.wasxfail = "reason: SomeExceptionFromLibrary. shame on them..."
return tr
很有魅力
我正在使用 Python 的 unittest
和 pytest
来针对第三方 API.
一些 API 调用暂时返回一个错误,这在我的代码中引发了一个特定的异常。此行为在代码中没有问题。
但是,与其让测试失败,我宁愿跳过这些临时错误。
我有超过 150 个测试。而不是像这样重写每个测试:
class TestMyLibrary(unittest.TestCase):
def test_some_test(self):
try:
// run the test as normal
// assert the normal behaviour
except SomeException:
// skip the test
def test_some_other_test(self):
try:
// run the test as normal
// assert the normal behaviour
except SomeException:
// skip the test
我能否以某种方式将它们全部包装在 class 级别或类似级别?
如果您预计会出现此异常,为什么不在它应该引发的时候检查它呢? 您可以使用:
pytest.raises(Exceptiontype, Foo())
这可以用装饰器来完成。例如:
def handle_lastfm_exceptions(f):
def wrapper(*args, **kw):
try:
return f(*args, **kw)
except pylast.WSError as e:
if (str(e) == "Invalid Method - "
"No method with that name in this package"):
msg = "Ignore broken Last.fm API: " + str(e)
print(msg)
pytest.skip(msg)
else:
raise(e)
return wrapper
然后对有问题的函数进行修饰:
class TestMyLibrary(unittest.TestCase):
@handle_lastfm_exceptions
def test_some_bad_test(self):
// run the test as normal
// assert the normal behaviour
def test_some_good_test(self):
// run the test as normal
// assert the normal behaviour
有同样的问题(不稳定的第 3 方库,等待修复...)。结果是这样的:
def pytest_runtest_makereport(item, call):
from _pytest.runner import pytest_runtest_makereport as orig_pytest_runtest_makereport
tr = orig_pytest_runtest_makereport(item, call)
if call.excinfo is not None:
if call.excinfo.type == SomeExceptionFromLibrary:
tr.outcome = 'skipped'
tr.wasxfail = "reason: SomeExceptionFromLibrary. shame on them..."
return tr
很有魅力