首页 > 解决方案 > 有没有办法只标记一个要运行的测试?

问题描述

我正在调试一个失败的测试。有没有办法只标记一个要运行的测试?

就像是:

@pytest.only # it doesn't work
def test_some_test():
  ...

标签: pythonpytest

解决方案


您可以用 标记您的测试pytest.mark。标记是动态创建的,因此您几乎可以选择任何名称。这是文档的链接。

例如在tt.py

import pytest

@pytest.mark.one_test
def test_foo():
    assert 1 == 1

def test_bar():
    assert 2 == 2

enter code here

然后运行pytest tt.py -m one_test

pytest tt.py -m one_test

======================= test session starts =========================

platform darwin -- Python 3.6.3, pytest-3.6.1, py-1.5.3, pluggy-0.6.0

rootdir: /Users/foobarna/workspace/random, inifile:
collected 2 items / 1 deselected

tt.py .
[100%]

============= 1 passed, 1 deselected in 0.04 seconds ================

推荐阅读