最基础的概念
1.setUP(self)看下面的执行顺序
import unittest class TestGo(unittest.TestCase): def setUp(self): print "setUP" def testOne(self): print "testone111" def testTwo(self): print "testtwo222"
def testThree(self):
print "test333" def tearDown(self): print "tearDown" if __name__=='__main__': unittest.main()
测试结果:在每个test方法执行的时候都会执行一遍setUp和tearDown
setUP testone111 tearDown
setUP testtwo222 tearDown
setUP testthree333 tearDown
2.setUPClass(cls)看下面的执行顺序
import unittest class TestGo(unittest.TestCase): @classmethod def setUpClass(cls): print "setUPClass" def testOne(self): print "testone111" def testTwo(self): print "testtwo222" def testThree(self): print "testtwo333" @classmethod def tearDownClass(cls): print "tearDownClass" if __name__=='__main__': unittest.main()
测试结果,在整个类中执行一遍setUPClass()和tearDownClass()
这种运用场景还是挺多的,比如启动某个mockserver服务,ui自动化启动浏览器等
setUPClass
testone111
testtwo333
testtwo222
tearDownClass