Python Python3 中忽略的单元测试预期失败
Python unittest expected failures ignored in Python3
我想将我的 python 测试工具(基于 Python 的单元测试模块)从 Python2 升级到 Python3。但是, unittest.expectedFailure
装饰器似乎不再具有相同的效果。特别是,以下代码具有不同的行为,具体取决于 Python 版本,即使规范几乎相同:
#!/usr/bin/env python2
#!/usr/bin/env python3
# Switch between the two lines above to get the different outcome
import unittest
class ComparisonTests(unittest.TestCase):
def runTest(self):
""" This method is needed even if empty """
def add_test(self, the_suite):
def testMain():
self.testFunc()
testMain = unittest.expectedFailure(testMain)
the_case = unittest.FunctionTestCase(testMain)
the_suite.addTest(the_case)
def testFunc(self):
self.assertTrue(False)
if __name__ == '__main__':
SUITE = unittest.TestSuite()
ComparisonTests().add_test(SUITE)
the_runner = unittest.TextTestRunner(verbosity=2)
the_runner.run(SUITE)
如果我在 MacOS 10.14.1
和 Python 2.7.15
上保留第一行 (#!/usr/bin/env python2
) 和 运行 那么输出如下:
unittest.case.FunctionTestCase (testMain) ... expected failure
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK (expected failures=1)
这是我期望的行为。但是,如果我切换到将使用 Python 3.7.3
的第二行 (#!/usr/bin/env python3
),我会得到以下信息:
unittest.case.FunctionTestCase (testMain) ... FAIL
======================================================================
FAIL: unittest.case.FunctionTestCase (testMain)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./unittest_test_2.py", line 12, in testMain
self.testFunc()
File "./unittest_test_2.py", line 18, in testFunc
self.assertTrue(False)
AssertionError: False is not true
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (failures=1)
看起来 unittest.expectedFailure
装饰器被忽略了。查看源代码我可以看到明显的区别:
# Python 3.7 source:
def expectedFailure(test_item):
test_item.__unittest_expecting_failure__ = True
return test_item
# Python 2.7 source:
def expectedFailure(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
func(*args, **kwargs)
except Exception:
raise _ExpectedFailure(sys.exc_info())
raise _UnexpectedSuccess
return wrapper
如何在 Python3 版本的 unittest 中定义预期失败?
Python 3 版本的 unittest.expectedFailure
装饰器预计将在单元测试测试用例上运行,而不是像在 Python 2 中那样在方法上运行。所以为了上面的测试工具与 Python 一起工作 3 需要在 the_case
上使用 expectedFalure
装饰器,如下所示:
the_case = unittest.FunctionTestCase(testMain)
the_case = unittest.expectedFailure(the_case)
我想将我的 python 测试工具(基于 Python 的单元测试模块)从 Python2 升级到 Python3。但是, unittest.expectedFailure
装饰器似乎不再具有相同的效果。特别是,以下代码具有不同的行为,具体取决于 Python 版本,即使规范几乎相同:
#!/usr/bin/env python2
#!/usr/bin/env python3
# Switch between the two lines above to get the different outcome
import unittest
class ComparisonTests(unittest.TestCase):
def runTest(self):
""" This method is needed even if empty """
def add_test(self, the_suite):
def testMain():
self.testFunc()
testMain = unittest.expectedFailure(testMain)
the_case = unittest.FunctionTestCase(testMain)
the_suite.addTest(the_case)
def testFunc(self):
self.assertTrue(False)
if __name__ == '__main__':
SUITE = unittest.TestSuite()
ComparisonTests().add_test(SUITE)
the_runner = unittest.TextTestRunner(verbosity=2)
the_runner.run(SUITE)
如果我在 MacOS 10.14.1
和 Python 2.7.15
上保留第一行 (#!/usr/bin/env python2
) 和 运行 那么输出如下:
unittest.case.FunctionTestCase (testMain) ... expected failure
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK (expected failures=1)
这是我期望的行为。但是,如果我切换到将使用 Python 3.7.3
的第二行 (#!/usr/bin/env python3
),我会得到以下信息:
unittest.case.FunctionTestCase (testMain) ... FAIL
======================================================================
FAIL: unittest.case.FunctionTestCase (testMain)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./unittest_test_2.py", line 12, in testMain
self.testFunc()
File "./unittest_test_2.py", line 18, in testFunc
self.assertTrue(False)
AssertionError: False is not true
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (failures=1)
看起来 unittest.expectedFailure
装饰器被忽略了。查看源代码我可以看到明显的区别:
# Python 3.7 source:
def expectedFailure(test_item):
test_item.__unittest_expecting_failure__ = True
return test_item
# Python 2.7 source:
def expectedFailure(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
func(*args, **kwargs)
except Exception:
raise _ExpectedFailure(sys.exc_info())
raise _UnexpectedSuccess
return wrapper
如何在 Python3 版本的 unittest 中定义预期失败?
Python 3 版本的 unittest.expectedFailure
装饰器预计将在单元测试测试用例上运行,而不是像在 Python 2 中那样在方法上运行。所以为了上面的测试工具与 Python 一起工作 3 需要在 the_case
上使用 expectedFalure
装饰器,如下所示:
the_case = unittest.FunctionTestCase(testMain)
the_case = unittest.expectedFailure(the_case)