Pytest 报告跳过 unittest.skip 的测试已通过

Pytest reports test skipped with unittest.skip as passed

测试看起来像这样:

import unittest

class FooTestCase(unittest.TestCase):

    @unittest.skip
    def test_bar(self):
        self.assertIsNone('not none')

当 运行 使用 pytest 时,报告类似于:

path/to/my/tests/test.py::FooTestCase::test_bar <- ../../../../../usr/lib/python3.5/unittest/case.py PASSED

另一方面,如果我将 @unittest.skip 替换为 @pytest.mark.skip,它会被正确地报告为已跳过:

path/to/my/tests/test.py::FooTestCase::test_bar <- ../../../../../usr/lib/python3.5/unittest/case.py SKIPPED

如果有人能说,我做错了什么还是 pytest 中的错误?

unittest.skip() 装饰器需要一个参数:

@unittest.skip(reason)

Unconditionally skip the decorated test. reason should describe why the test is being skipped.

它的用法可以在他们的 examples:

中找到
class MyTestCase(unittest.TestCase):

    @unittest.skip("demonstrating skipping")
    def test_nothing(self):
        self.fail("shouldn't happen")

因此 unittest.skip 本身不是一个装饰器,而是一个装饰器工厂 - 实际的装饰器是调用 unittest.skip.

的结果

这解释了为什么您的测试通过而不是被跳过或失败,因为它实际上等同于以下内容:

import unittest

class FooTestCase(unittest.TestCase):

    def test_bar(self):
        self.assertIsNone('not none')

    test_bar = unittest.skip(test_bar)
    # now test_bar becomes a decorator but is instead invoked by
    # pytest as if it were a unittest method and passes