Unitest学习笔记

image.png

下面是详细的代码

测试脚本一:

import unittest

class Unit_Test_Case(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print("setupclass")

    @classmethod
    def tearDownClass(cls):
        print("teardownclass")

    def setUp(self):
        print('setup')

    def tearDown(self):
        print('teardown')

    def test_case_1(self):
        print('test case 1')

    def test_case_2(self):
        print('test case 2')

if __name__ == '__main__':
    unittest.main()

测试脚本二:

import unittest

'''
演示如何通过TestLoader()来构造TestSuite
'''

class TestFrist(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_one(self):
        print('one')

    def test_two(self):
        print('two')

class TestSecond(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_three(self):
        print('three')

    def test_four(self):
        print('four')

if __name__ == '__main__':
    testcase1 = unittest.TestLoader().loadTestsFromTestCase(TestFrist)
    testcase2 = unittest.TestLoader().loadTestsFromTestCase(TestSecond)
    testsuite1 = unittest.TestSuite([testcase1, testcase2])
    unittest.TextTestRunner().run(testsuite1)

测试脚本三:

import unittest

'''
换一种方式构造用例集合:定义一个集合,然后通过add test往集合中增加测试用例。
'''


class TestFrist(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_one(self):
        print('one')

    def test_two(self):
        print('two')

class TestSecond(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_three(self):
        print('three')

    def test_four(self):
        print('four')

if __name__ == '__main__':
    testsuite = unittest.TestSuite()
    testsuite.addTest(TestFrist)
    testsuite.addTest(TestSecond)
    unittest.TextTestRunner().run(testsuite)

测试脚本四

import unittest
import HTMLTestRunner

testsuite = unittest.TestLoader().discover('.')
filename = "d:\\CZBK\\four\\subfile4-2\\res.html"
f = open(filename, 'wb')
runner = HTMLTestRunner.HTMLTestRunner(stream=f, title='test report-storm', description='this is a test')
runner.run(testsuite)
f.close()

你可能感兴趣的:(Unitest学习笔记)