如何在我的本地机器上运行一个特定的测试,使用pytest



我有几个测试,我想在我的本地机器上运行,但不是在CI上。

我正在使用pytest,我该怎么做呢?

我可以测试是否有一个特定的环境键,并且只在它存在的情况下运行测试,例如:

def test_something():
if 'key' in os.environ:
< do test stuff >

我觉得可能有一个更好的方法,也许使用一些我不知道的pytest方法/decorator。

您可以使用pytest的功能跳过:

skipif

如果您希望有条件地跳过某些内容,则可以使用skipif。

import sys

@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher")
def test_function():
...

所以试试这样写:

@pytest.mark.skipif("key" not in os.environ)
def test_something():
...

最新更新