<p>正如其他人已经提到的,理想情况下,您应该编写多个测试,并且每个测试中只有一个断言(这不是一个硬限制,而是一个很好的指南)</p>
<p><a href="http://pytest.org/latest/parametrize.html#pytest-mark-parametrize-parametrizing-test-functions" rel="noreferrer">^{<cd1>}</a>装饰器使这变得很容易:</p>
<pre><code>import pytest
def parrot(i):
return i
@pytest.mark.parametrize('inp, expected', [(0, 0), (1, 1), (2, 1), (2, 2)])
def test_parrot(inp, expected):
assert parrot(inp) == expected
</code></pre>
<p>使用<code>-v</code>运行时:</p>
<pre><code>parrot.py::test_parrot[0-0] PASSED
parrot.py::test_parrot[1-1] PASSED
parrot.py::test_parrot[2-1] FAILED
parrot.py::test_parrot[2-2] PASSED
=================================== FAILURES ===================================
_______________________________ test_parrot[2-1] _______________________________
inp = 2, expected = 1
@pytest.mark.parametrize('inp, expected', [(0, 0), (1, 1), (2, 1), (2, 2)])
def test_parrot(inp, expected):
> assert parrot(inp) == expected
E assert 2 == 1
E + where 2 = parrot(2)
parrot.py:8: AssertionError
====================== 1 failed, 3 passed in 0.01 seconds ======================
</code></pre>