python unitest automation framework

The following is the simplest unitest example, including remarks. You can understand the principle by pulling the code and running it once.

import unittest
import os

class TestSample(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        print('整个测试类只执行一次')

    def setUp(self) -> None:
        print("每个测试开始前执行一次")

    def test_equal(self):
        self.assertEqual(1,1)

    def test_no_equal(self):
        self.assertNotEqual(1,2)

    def tearDown(self) -> None:
        print('每个测试结束后执行一次')

    @classmethod
    def tearDownClass(cls) -> None:
        print('整个个测试执行一次')



if __name__ == '__main__':
    # unittest.main()
    #添加用例集
    suite = unittest.defaultTestLoader.discover(os.path.join(os.path.dirname(__file__)), pattern='*.py',top_level_dir=os.path.dirname(__file__))
    #执行用例,默认函数名开头为test的用例
    runner = unittest.TextTestRunner(verbosity=2)
    runner.run()

There are pre-functions and post-functions above, commonly known as test fixtures;

The test result report is output on the console by default. If you want to have an HTML test report, you can use HTMLTestRunner; the corresponding git code example is attached below.

Mr_wilson_liu/Python unitest example · GitCode

Show results:

 

Guess you like

Origin blog.csdn.net/Mr_wilson_liu/article/details/132562572