您不需要显式验证命令行选项;这将由 arg 解析器完成,如有必要,它将提前中止执行。至于条件检查,您离解决方案不远了。用pytest.exit 立即中止pytest.skip 跳过所有测试pytest.xfail 使所有测试失败(尽管这是预期的失败,因此它不会将整个执行标记为失败)示例夹具:@pytest.fixture(scope='session', autouse=True)def precondition(): if not shutil.which('spam'): # immediate shutdown pytest.exit('Install spam before running this test suite.') # or skip each test # pytest.skip('Install spam before running this test suite.') # or make it an expected failure # pytest.xfail('Install spam before running this test suite.')