如何标记一个函数而不是pytest的测试?

xia*_*x48 7 python unit-testing pytest tensorflow

我正在使用 pytest 来测试一些基于 TensorFlow 的代码。

ATestCase的定义是为了简单起见,例如:

class TestCase(tf.test.TestCase):
    # ...
Run Code Online (Sandbox Code Playgroud)

问题是tf.test.TestCase提供了一个有用的函数self.test_session(),由于它的名字以test_.

结果 pytest 报告了比我由于test_session()方法定义的测试方法更多的成功测试。

我使用以下代码跳过test_session

class TestCase(tf.test.TestCase):
    @pytest.mark.skip
    @contextmanager
    def test_session(self):
        with super().test_session() as sess:
            yield sess
Run Code Online (Sandbox Code Playgroud)

然而,测试报告中会有一些“s”表示有一些跳过测试。

无论如何,我可以在不全局更改 pytest 测试发现规则的情况下标记一种确切的方法而不是测试方法吗?

hoe*_*ing 4

收集测试项目后过滤掉误报:conftest.py使用自定义后收集挂钩在测试目录中创建一个:

# conftest.py
def pytest_collection_modifyitems(session, config, items):
    items[:] = [item for item in items if item.name != 'test_session']
Run Code Online (Sandbox Code Playgroud)

pytest仍然会收集方法(您会在报告行test_session中注意到),但不会将它们作为测试执行,并且不会在测试运行中的任何地方考虑它们。pytestcollected n tests


相关:修复unittest-style 测试

看看这个答案