python unittest单元测试框架-3用例执行顺序、多级目录、装饰器、fixtures
1.用例执行顺序
unittest默认会按照ascii码的顺序,依次执行。类名——方法名排序,使用discover也是默认排序。如果不想使用默认排序,就使用testsuite测试集的方式。
import unittest
class TestB(unittest.TestCase):
def setUp(self):
print("class B start")
def testC(self):
print("func c")
def testA(self):
print("func a ")
def tearDown(self):
print("class B end")
class TestA(unittest.TestCase):
def setUp(self):
print("class A start")
def testB(self):
print("func b")
def tearDown(self):
print("class A end")
if __name__=="__main__":
unittest.main()
![](https://img2018.cnblogs.com/blog/1418970/201811/1418970-20181126235135372-293212399.png)
discover,新建runtest.py文件
import unittest
test_dir="./"
discover=unittest.defaultTestLoader.discover(test_dir,pattern="testorder.py")
if __name__=="__main__":
runner=unittest.TextTestRunner()
runner.run(discover)
![](https://img2018.cnblogs.com/blog/1418970/201811/1418970-20181127001148046-811941131.png)
suite
if __name__=="__main__":
suite=unittest.TestSuite()
suite.addTest(TestB("testA"))
suite.addTest(TestA("testB"))
suite.addTest(TestB("testC"))
runner = unittest.TextTestRunner()
runner.run(suite)
![](https://img2018.cnblogs.com/blog/1418970/201811/1418970-20181127000119032-655273503.png)
2.多级目录
执行多级目录,只需要在每个子目录下面添加__init__文件即可
test_a.py
import unittest
class TestA(unittest.TestCase):
def setUp(self):
pass
def test(self):
print("A")
def tearDown(self):
pass
test_b,test_c,test_d只是将test中输出更改为B C D
runtest.py
import unittest
test_dir="./"
discover=unittest.defaultTestLoader.discover(test_dir,pattern="test*.py")
if __name__=="__main__":
runner=unittest.TextTestRunner()
runner.run(discover)
![](https://img2018.cnblogs.com/blog/1418970/201811/1418970-20181127084959336-773397193.png)
3.装饰器:跳过测试和执行失败
运行测试时,需要跳过某些测试用例,或预期失败,此时unittest提供了装饰器来实现这些需求。
装饰器同样适用于类
装饰器 | 说明 |
---|---|
unittest.skip(原因) | 无条件的跳过测试 |
unittest.skipIf(条件, 原因) | 如果条件为真,就跳过测试;否则执行测试 |
unittest.skipUnless(条件, 原因) | 如果条件为真,就执行测试;否则跳过测试 |
unittest.expectedFailure | 预期失败。执行结果失败,pass;执行结果成功,fail |
import unittest
class MyTest(unittest.TestCase):
@unittest.skip("直接跳过test_skip")
def test_aaa(self):
print("直接跳过test_aaa\n")
@unittest.skipIf(3>2,"条件为true时,跳过test_skipIf")
def test_bbb(self):
print("跳过test_bbb\n")
@unittest.skipUnless(3>2,"条件为true时,执行测试test_skipUnless")
def test_ccc(self):
print("执行测试test_ccc\n")
@unittest.expectedFailure
def test_ddd(self):
print("test_ddd执行完毕\n")
@unittest.expectedFailure
def test_eee(self):
self.assertEqual(2,3)
if __name__=="__main__":
unittest.main()
![](https://img2018.cnblogs.com/blog/1418970/201811/1418970-20181127094950216-911522172.png)
4.fixtures
除了用例的fixtures,unittest还提供了更大范围的fixtures,如针对模块和类的
- setUpModule/tearDownModule:在整个模块的开始于结束是被执行
- setUpClass/earDownClass:在测试类的开始与结束时被执行
- setUp/tearDown:在测试用例的开始与结束时被执行
import unittest
def setUpModule():
print("模块:初始化")
def tearDownModule():
print("模块:销毁")
class Test(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("类:初始化")
@classmethod
def tearDownClass(cls):
print("类:销毁")
def setUp(self):
print("用例:初始化")
def tearDown(self):
print("用例:销毁")
def test_case1(self):
print("用例1执行")
def test_case2(self):
print("用例2执行")
if __name__=="__main__":
unittest.main()
![](https://img2018.cnblogs.com/blog/1418970/201811/1418970-20181127101132437-1402918304.png)