我正在使用 pytest 来测试一些基于 TensorFlow 的代码。
TestCase
的定义很简单,如下所示:
class TestCase(tf.test.TestCase):
# ...
问题是 tf.test.TestCase
提供了一个有用的函数 self.test_session()
,它在 pytest 中被视为一个测试方法,因为它的名称以 测试_
。
由于 test_session()
方法,结果 pytest 报告比我定义的测试方法更多的成功测试。
我使用以下代码跳过 test_session
:
class TestCase(tf.test.TestCase):
@pytest.mark.skip
@contextmanager
def test_session(self):
with super().test_session() as sess:
yield sess
但是在测试报告中会有一些“s”表示有一些跳过测试。
无论如何,我可以在不全局更改 pytest 测试发现规则的情况下将一种确切的方法标记为不是测试方法吗?
最佳答案
在收集测试项目后过滤掉误报:使用自定义收集后 Hook 在您的测试目录中创建一个 conftest.py
:
# conftest.py
def pytest_collection_modifyitems(session, config, items):
items[:] = [item for item in items if item.name != 'test_session']
pytest
仍将收集 test_session
方法(您会注意到在 pytest
报告行 collected n tests
),但不要将它们作为测试执行,也不要在测试运行的任何地方考虑它们。
相关:修复 unittest
风格的测试
查看 this answer .
关于python - 如何标记一个函数不是 pytest 的测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50610557/