unittest中的setUp/setUpClass的区别

1、setUp案例

import unittest
class TestSetup(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()

结果:

...
----------------------------------------------------------------------
Ran 3 tests in 0.002s

OK
setUP
testone111
tearDown
setUP
test333
tearDown
setUP
testtwo222
tearDown

Process finished with exit code 0

2、setUpClass案例

import unittest
class TestSetuoclass(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print("setUpclass")
    def test_one(self):
        print("test1111")
    def test_two(self):
        print("test222")
    def test_htreee(self):
        print("test333")
    @classmethod
    def tearDownClass(cls):
        print("teardownclass")
if __name__ == '__main__':
    unittest.main()

结果:

...
----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
setUpclass
test333
test1111
test222
teardownclass

Process finished with exit code 0

 

你可能感兴趣的:(Python学习篇)