Unittest Framework 简明教程
UnitTest Framework - Skip Test
自 Python 2.7 以来,已添加了跳过测试的支持。可以有条件地或无条件地跳过单个测试方法或 TestCase 类。该框架允许将某个测试标记为“预期故障”。此测试将“失败”,但不会被计为 TestResult 中的失败。
Support for skipping tests has been added since Python 2.7. It is possible to skip individual test method or TestCase class, conditionally as well as unconditionally. The framework allows a certain test to be marked as an 'expected failure'. This test will 'fail' but will not be counted as failed in TestResult.
要无条件地跳过一个方法,可以使用以下 unittest.skip() 类方法:
To skip a method unconditionally, the following unittest.skip() class method can be used −
import unittest
def add(x,y):
return x+y
class SimpleTest(unittest.TestCase):
@unittest.skip("demonstrating skipping")
def testadd1(self):
self.assertEquals(add(4,5),9)
if __name__ == '__main__':
unittest.main()
由于 skip() 是一个类方法,因此它之前带有 @ 令牌。此方法接受一个参数:描述跳过原因的日志消息。
Since skip() is a class method, it is prefixed by @ token. The method takes one argument: a log message describing the reason for the skip.
当执行以上脚本时,将在控制台上显示以下结果:
When the above script is executed, the following result is displayed on console −
C:\Python27>python skiptest.py
s
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK (skipped = 1)
字符“s”表示已跳过某个测试。
The character 's' indicates that a test has been skipped.
跳过测试的替代语法是在测试函数内使用实例方法 skipTest()。
Alternate syntax for skipping test is using instance method skipTest() inside the test function.
def testadd2(self):
self.skipTest("another method for skipping")
self.assertTrue(add(4 + 5) == 10)
以下装饰器实现测试跳过和预期失败:
The following decorators implement test skipping and expected failures −
S.No. |
Method & Description |
1 |
unittest.skip(reason) Unconditionally skip the decorated test. reason should describe why the test is being skipped. |
2 |
unittest.skipIf(condition, reason) Skip the decorated test if condition is true. |
3 |
unittest.skipUnless(condition, reason) Skip the decorated test unless condition is true. |
4 |
unittest.expectedFailure() Mark the test as an expected failure. If the test fails when run, the test is not counted as a failure. |
下面的示例演示了如何使用条件跳过和预期失败。
The following example demonstrates the use of conditional skipping and expected failure.
import unittest
class suiteTest(unittest.TestCase):
a = 50
b = 40
def testadd(self):
"""Add"""
result = self.a+self.b
self.assertEqual(result,100)
@unittest.skipIf(a>b, "Skip over this routine")
def testsub(self):
"""sub"""
result = self.a-self.b
self.assertTrue(result == -10)
@unittest.skipUnless(b == 0, "Skip over this routine")
def testdiv(self):
"""div"""
result = self.a/self.b
self.assertTrue(result == 1)
@unittest.expectedFailure
def testmul(self):
"""mul"""
result = self.a*self.b
self.assertEqual(result == 0)
if __name__ == '__main__':
unittest.main()
在上面的示例中,将跳过 testsub() 和 testdiv()。在第一个情况下,a > b 为真,而在第二个情况下,b != 0 不为真。另一方面,testmul() 已被标记为预期失败。
In the above example, testsub() and testdiv() will be skipped. In the first case a>b is true, while in the second case b == 0 is not true. On the other hand, testmul() has been marked as expected failure.
当运行上述脚本时,两个跳过的测试将显示“s”,而预期失败将显示为“x”。
When the above script is run, two skipped tests show 's' and the expected failure is shown as 'x'.
C:\Python27>python skiptest.py
Fsxs
================================================================
FAIL: testadd (__main__.suiteTest)
Add
----------------------------------------------------------------------
Traceback (most recent call last):
File "skiptest.py", line 9, in testadd
self.assertEqual(result,100)
AssertionError: 90 != 100
----------------------------------------------------------------------
Ran 4 tests in 0.000s
FAILED (failures = 1, skipped = 2, expected failures = 1)