4. Test code组织结构
4.1 在unittest中,test case都是测试类的实力,所以编写testcase时,必须预先定义测试子类。继承自Testcase或者FunctionTestcase。
Testcase提供的
assert*()方法是用于检测测试结果,
官方说明:
Note that in order to test something, we use one of the
assert*()
methods provided by the
TestCase
base class. If the test fails, an exception will be raised, and
unittest
will identify the test case as a
failure. Any other exceptions will be treated as
errors. This helps you identify where the problem is:
failures
are caused by incorrect results - a 5 where you expected a 6.
Errors
are caused by incorrect code - e.g., a
TypeError
caused by an incorrect function call.
例子:
import unittest
class WidgetTestCase(unittest.TestCase): def setUp(self): self.widget = Widget('The widget') def tearDown(self): self.widget.dispose() self.widget = None def test_default_size(self): self.assertEqual(self.widget.size(), (50,50), 'incorrect default size') def test_resize(self): self.widget.resize(100,150) self.assertEqual(self.widget.size(), (100,150), 'wrong size after resize')
(1)如果setUp()succeed, tearDown()不管testcase是否成功都会执行。
如果setUp() failed, testcase不执行
(2)我们可以指定执行某一个testcase
widgetTestSuite = unittest.TestSuite() widgetTestSuite.addTest(WidgetTestCase('test_default_size')) widgetTestSuite.addTest(WidgetTestCase('test_resize'))
也可以直接执行整个类里面的所有testcase:
suite = unittest.TestLoader().loadTestsFromTestCase(WidgetTestCase)
我们也可以同时run指定所有类里面的testcase:
suite1 = module1.TheTestSuite() suite2 = module2.TheTestSuite() alltests = unittest.TestSuite([suite1, suite2])
5
5.1.怎样skip某些expected的failure
unnitest能将某些预先定义的failure标记为expectd,从而将这些failure不加入早testresult数据结果中。也可以直接skip掉某些方法或者类
使用skip() decorator这个修饰器来skip
(1)skip某些method
例子:
import unittest
import sys
import time
class MyTestCase(unittest.TestCase):
@unittest.skip("demonstrating skipping")
def test_nothing(self):
self.fail("shouldn't happen")
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
def test_windows_support(self):
# windows specific testing code
pass
def test_path(self):
zone = time.timezone
print "the zone is %s" ,zone
suite = unittest.TestLoader().loadTestsFromTestCase(MyTestCase)
unittest.TextTestRunner(verbosity=2).run(suite)
output如下:
the zone is %s -28800
test_nothing (__main__.MyTestCase) ... skipped 'demonstrating skipping'
test_path (__main__.MyTestCase) ... ok
test_windows_support (__main__.MyTestCase) ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK (skipped=1)
(2)Skip某些class
@unittest.skip("showing class skipping") class MySkippedTestCase(unittest.TestCase): def test_not_run(self): pass
注意:setUp()也能被skip,当某些source不存在时,可以使用skip
5.2 expected failure的使用
定义需要使用expectedFailure()
decorator
例子:
class ExpectedFailureTestCase(unittest.TestCase):@unittest.expectedFailuredef test_fail(self):self.assertEqual(1, 0, "broken")suite = unittest.TestLoader().loadTestsFromTestCase(ExpectedFailureTestCase)unittest.TextTestRunner(verbosity=2).run(suite)output如下:test_fail (__main__.ExpectedFailureTestCase) ... expected failure----------------------------------------------------------------------Ran 1 test in 0.001sOK (expected failures=1)5.3 skip和expected failure联合作战
-
用法如下:
-
Unconditionally skip the decorated test. reason should describe why the test is being skipped.
unittest.
skip
(
reason)
-
Skip the decorated test if condition is true
unittest.
skipIf
(
condition,
reason)
-
Skip the decorated test unless condition is true
unittest.
skipUnless
(
condition,
reason)
-
Mark the test as an expected failure. If the test fails when run, the test is not counted as a failure
unittest.
expectedFailure
()
-
exception
-
This exception is raised to skip a test.
unittest.
SkipTest
(
reason)
Usually you can use TestCase.skipTest() or one of the skippingdecorators instead of raising this directly.Skipped tests will not have setUp() or tearDown() run around them. Skipped classes will not have setUpClass() or tearDownClass() run.