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

标签 python unit-testing tensorflow pytest

我正在使用 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/

相关文章:

python - 将 pandas DataFrame(具有许多列)的所有行分组为给定列中具有相同值的

python - 在 PyCharm 控制台中运行代码

python - 具有多维输出目标的 LSTM

python - TensorFlow 变量范围 : reuse if variable exists

python - 模块未找到错误: No module named 'tensorflow' Vs code

python - 如何在 Pandas 数据框中将其分开?

python - 如何在不改变其形状的情况下在pygame中旋转表面

c# - 如何使用 JustMock 模拟构造函数?

swift - 将 Swift 单元测试添加到项目时,构建操作失败但未指定任何错误

unit-testing - hadoop mapreduce 作业的最佳单元测试工具/方法