我希望失败的测试没问题
I want a failed test to be OK
我有一个测试失败了,但我预计它会失败,我希望 pytest
说它通过了。我该怎么做?
例如,我的最小值 = 30,最大值 = 40。
这是我正在做的事情:
@pytest.mark.parametrize(
"minimum, maximum, expected_min, expected_max", [
(13, 15, 34, 45),
(30, 40, 30, 40),
("sd", 3, 34, 45),
])
我收到:
- 失败(断言错误)
- 通过
- 失败(断言错误)
我怎样才能得到一份表明所有测试都已通过的报告?
最佳做法(如果这是您的代码的预期行为)是编辑测试以反映预期行为。测试用例不只是用来获取您可以展示的报告 manager/customer/colleague/etc,它们也是一种文档形式。测试用例向人们展示您的代码的预期行为,因此任何看到该测试的人都会假设您的代码将接受此处显示的所有输入。
如果第一个和第三个输入是非法输入,您应该创建一个不同的测试用例,测试代码如何处理非法输入。
要在 的基础上使用基于您的用例的实际示例,请考虑以下代码。我们将原始测试数据集分成两组:一组我们期望通过,一组我们期望失败。像这样的单元测试应该是孤立地测试一种情况,使每个测试用例更容易编写和更容易理解。
import pytest
class TestMinMax:
@pytest.mark.parametrize(
"minimum, maximum, expected_min, expected_max", [
(30, 40, 30, 40),
])
def test_valid_examples(self, minimum, maximum, expected_min, expected_max):
assert minimum == expected_min
assert maximum == expected_max
@pytest.mark.parametrize(
"minimum, maximum, expected_min, expected_max", [
(13, 15, 34, 45),
("sd", 3, 34, 45),
])
def test_invalid_examples(self, minimum, maximum, expected_min, expected_max):
with pytest.raises(AssertionError):
assert minimum == expected_min
assert maximum == expected_max
if __name__ == '__main__':
pytest.main(args=[__file__])
输出
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-3.0.1, py-1.4.31, pluggy-0.3.1
rootdir: C:\Users\<<user>>\.PyCharmCE2016.3\config\scratches, inifile:
collected 3 items
scratch_3.py ...
========================== 3 passed in 0.02 seconds ===========================
我有一个测试失败了,但我预计它会失败,我希望 pytest
说它通过了。我该怎么做?
例如,我的最小值 = 30,最大值 = 40。
这是我正在做的事情:
@pytest.mark.parametrize(
"minimum, maximum, expected_min, expected_max", [
(13, 15, 34, 45),
(30, 40, 30, 40),
("sd", 3, 34, 45),
])
我收到:
- 失败(断言错误)
- 通过
- 失败(断言错误)
我怎样才能得到一份表明所有测试都已通过的报告?
最佳做法(如果这是您的代码的预期行为)是编辑测试以反映预期行为。测试用例不只是用来获取您可以展示的报告 manager/customer/colleague/etc,它们也是一种文档形式。测试用例向人们展示您的代码的预期行为,因此任何看到该测试的人都会假设您的代码将接受此处显示的所有输入。
如果第一个和第三个输入是非法输入,您应该创建一个不同的测试用例,测试代码如何处理非法输入。
要在
import pytest
class TestMinMax:
@pytest.mark.parametrize(
"minimum, maximum, expected_min, expected_max", [
(30, 40, 30, 40),
])
def test_valid_examples(self, minimum, maximum, expected_min, expected_max):
assert minimum == expected_min
assert maximum == expected_max
@pytest.mark.parametrize(
"minimum, maximum, expected_min, expected_max", [
(13, 15, 34, 45),
("sd", 3, 34, 45),
])
def test_invalid_examples(self, minimum, maximum, expected_min, expected_max):
with pytest.raises(AssertionError):
assert minimum == expected_min
assert maximum == expected_max
if __name__ == '__main__':
pytest.main(args=[__file__])
输出
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-3.0.1, py-1.4.31, pluggy-0.3.1
rootdir: C:\Users\<<user>>\.PyCharmCE2016.3\config\scratches, inifile:
collected 3 items
scratch_3.py ...
========================== 3 passed in 0.02 seconds ===========================