擅长:python、mysql、java
<p>您可以在pytest.ini文件中使用<a href="https://docs.pytest.org/en/latest/reference.html#confval-addopts" rel="nofollow noreferrer">addopts</a>。它不需要调用任何命令行开关。</p>
<pre><code># content of pytest.ini
[pytest]
addopts = --maxfail=2 # exit after 2 failures
</code></pre>
<p>也可以在运行测试之前设置环境变量“PYTEST_ADDOPTS”。</p>
<p>如果要在第一次失败后使用python代码退出,可以使用以下代码:</p>
<pre><code>import pytest
@pytest.fixture(scope='function', autouse=True)
def exit_pytest_first_failure():
if pytest.TestReport.outcome == 'failed':
pytest.exit('Exiting pytest')
</code></pre>
<p>此代码将exit_pytest_first_failure fixture应用于所有测试,并在第一次失败时退出pytest。</p>