Python Pytest报告已跳过测试,unittest.skip已通过
该测试如下所示:Python Pytest报告已跳过测试,unittest.skip已通过,python,pytest,python-unittest,Python,Pytest,Python Unittest,该测试如下所示: 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/pytho
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
如果有人会说,是我做错了什么,还是pytest
中有一个bug?decorator需要一个参数:
@unittest.skip(原因)
无条件跳过测试。理由应该说明原因
正在跳过测试
其用法可在以下内容中找到:
因此,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
看起来您需要调用
unittest.skip
。尝试@unittest.skip()
。
class MyTestCase(unittest.TestCase):
@unittest.skip("demonstrating skipping")
def test_nothing(self):
self.fail("shouldn't happen")
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