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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章