<p>pytest创建测试并按字母顺序运行它们(默认行为),因此四个<code>test_edit_config</code>测试将在<code>test_get_config</code>测试之前运行。你知道吗</p>
<p>您可以创建一个调用其他测试函数的测试</p>
<pre><code>@pytest.mark.parametrize("interval, lacp_mode", [("fast", "active"), ("fast", "passive"), ("slow", "active"), ("slow", "passive")])
class TestExample:
def test_config(self, interval, lacp_mode):
self.__edit_config(interval, lacp_mode)
self.__get_config(interval, lacp_mode)
def __edit_config(self, interval, lacp_mode):
pass
def __get_config(self, interval, lacp_mode):
pass
</code></pre>
<p>另一个选择是使用<a href="https://pypi.org/project/pytest-ordering/" rel="nofollow noreferrer">pytest-ordering</a>插件并动态添加顺序</p>
<pre><code>def data_provider():
i = 0
for data in [("fast", "active"), ("fast", "passive"), ("slow", "active"), ("slow", "passive")]:
i += 1
yield pytest.param(data[0], data[1], marks=pytest.mark.run(order=i))
class TestExample:
@pytest.mark.parametrize('interval, lacp_mode', data_provider())
def test_edit_config(self, interval, lacp_mode):
pass
@pytest.mark.parametrize('interval, lacp_mode', data_provider())
def test_get_config(self, interval, lacp_mode):
pass
</code></pre>