Assume this sample code:
假设此示例代码:
def test_foo():
dict = load_dict()
try:
value = dict[foo][bar]
except KeyError:
print('missing foo or bar')
If it raises KeyError
because either foo
or bar
doesn't exist, the test will not fail because the exception is captured. If I add a raise SystemExit(1)
, it fails, prints the message and shows all the traceback.
如果它引发KeyError,因为foo或bar不存在,测试将不会失败,因为捕获了异常。如果我添加一个提升SystemExit(1),它会失败,打印消息并显示所有回溯。
My question is, how can I tell pytest that if a KeyError
occurred it means the test failed, so that I don't need to raise a SystemExit
?
我的问题是,如何告诉pytest如果发生KeyError则意味着测试失败,这样我就不需要提出SystemExit了?
2 个解决方案
#1
2
There is a function pytest.fail
that explicitly fails the test:
有一个函数pytest.fail明确地失败了测试:
import pytest
def test_foo():
d1 = {'foo': 'bar'}
try:
value = d1['baz']
except KeyError as err:
pytest.fail('this was unexpected: {}'.format(err))
However, the idiomatic way would be using the pytest.raises
context manager that verifies the exception is raised, capturing it for analysis with some convenient utilities:
然而,惯用的方法是使用pytest.raises上下文管理器来验证引发的异常,使用一些方便的实用程序捕获它以进行分析:
import pytest
def test_foo():
d1 = {'foo': 'bar'}
with pytest.raises(KeyError) as excinfo:
value = d1['baz']
assert excinfo.type == KeyError
assert excinfo.match('baz')
Check out the docs for more examples. If you are familiar with unittest
, pytest.raises
is the pendant to unittest.TestCase.assertRaises
, while pytest.fail
is the pendant to unittest.TestCase.fail
.
查看文档以获取更多示例。如果您熟悉unittest,pytest.raises是unittest.TestCase.assertRaises的吊坠,而pytest.fail是unittest.TestCase.fail的吊坠。
#2
1
You can use the with pytest.raises
constructor:
您可以使用with pytest.raises构造函数:
def test_connection_fails(self,):
with pytest.raises(KeyError) as excinfo:
buckets = list_all_buckets()
Then you can raise an error without using sys.exit
然后,您可以在不使用sys.exit的情况下引发错误
#1
2
There is a function pytest.fail
that explicitly fails the test:
有一个函数pytest.fail明确地失败了测试:
import pytest
def test_foo():
d1 = {'foo': 'bar'}
try:
value = d1['baz']
except KeyError as err:
pytest.fail('this was unexpected: {}'.format(err))
However, the idiomatic way would be using the pytest.raises
context manager that verifies the exception is raised, capturing it for analysis with some convenient utilities:
然而,惯用的方法是使用pytest.raises上下文管理器来验证引发的异常,使用一些方便的实用程序捕获它以进行分析:
import pytest
def test_foo():
d1 = {'foo': 'bar'}
with pytest.raises(KeyError) as excinfo:
value = d1['baz']
assert excinfo.type == KeyError
assert excinfo.match('baz')
Check out the docs for more examples. If you are familiar with unittest
, pytest.raises
is the pendant to unittest.TestCase.assertRaises
, while pytest.fail
is the pendant to unittest.TestCase.fail
.
查看文档以获取更多示例。如果您熟悉unittest,pytest.raises是unittest.TestCase.assertRaises的吊坠,而pytest.fail是unittest.TestCase.fail的吊坠。
#2
1
You can use the with pytest.raises
constructor:
您可以使用with pytest.raises构造函数:
def test_connection_fails(self,):
with pytest.raises(KeyError) as excinfo:
buckets = list_all_buckets()
Then you can raise an error without using sys.exit
然后,您可以在不使用sys.exit的情况下引发错误