Unittest - Python using summary

Unittest - Python using summary

Batch execution

A, UnitTest TestSuite embodiment performs control sequentially with

UnitTest default frame main () method to load the ASCII code of the test according to the order, the sequence of numbers and letters: 0 ~ 9, A ~ Z
, a ~ z If you want to execute a test case, not using the default main ( ) method, need () addTest TestSuite class method in a certain order to load.

Example :

class TestCase01(unittest.TestCase):

    def setUp(self):
        print("case开始执行")

    def tearDown(self):
        print("case结束执行")

    @classmethod
    def setUpClass(cls):
        print("case类开始执行")

    @classmethod
    def tearDownClass(cls):
        pass

    @unittest.skip("这个case不像执行")
    def test_07(self):
        print("执行case07")
        flag = "adfadfadfadfadsfaqeewr"
        s = "fads"
        self.assertIn(s, flag, msg="不包含")

    @unittest.skip("这个case不像执行")
    def test_01(self):
        TestCase01.a = 5
        print("执行case01")
        # res = requests.get(url=url,params=data).json()
        data1 = {
            "user": "11111"
        }
        self.assertDictEqual(data1, data)

    @unittest.skip("这个case不像执行")
    def test_02(self):
        print("----------------> 执行case02")
        data1 = {
            "username": "1111",
            "password": "22222"
        }
        self.assertDictEqual(data1, data, msg="这两个字典不相等")
    
    @unittest.skip("这个case不像执行")
    def test_03(self):
        print("执行case03")
        flag = True
        self.assertFalse(flag, msg="不等于True")

    @unittest.skip("这个case不像执行")
    def test_04(self):
        print("执行case04")
        flag = False
        self.assertTrue(flag, msg="不等于False")

    @unittest.skipIf(host != "http://www.imooc.com", "这个case不执行")
    def test_05(self):
        print("执行case05")
        flag = "111"
        flag1 = "2222"
        self.assertEqual(flag, flag1, msg="两个str不相等")

    def test_06(self):
        res = request.run_main('get', url, data)
        print(res)


if __name__ == "__main__":
    # unittest.main()# 默认顺序执行方式
    suite = unittest.TestSuite()
    '''
    # TestSuite 控制执行顺序
    suite.addTest(TestCase01('test_06'))
    suite.addTest(TestCase01('test_04'))
    suite.addTest(TestCase01('test_02'))
    suite.addTest(TestCase01('test_05'))
    suite.addTest(TestCase01('test_01'))
    suite.addTest(TestCase01('test_07'))
    runner = unittest.TextTestRunner()
    runner.run(suite)
    '''
    # TestSuite 另一种写法
    tests = [TestCase01('test_06'), TestCase01('test_02'), TestCase01('test_03'), TestCase01('test_05'),
             TestCase01('test_01')]
    suite.addTests(tests)#
    runner = unittest.TextTestRunner()
    runner.run(suite)

Second, the use UnitTest TestLoader class Discover () function to perform more test cases

Python Unittest discover () method with the execution sequence complementary

You can create different functions according to different test files, directories or even a different test, test files can also be different small test functions are divided into different classes, write test cases in class, so that the overall structure is more clear

But () by adding addTest, delete a test case becomes very troublesome

TestLoader class provided Discover () method can automatically identify test .py files.

discover(start_dir,pattern='test*.py',top_level_dir= None)

Find all test modules specified directory, and can be found in the test module in the recursive subdirectory only match to the file name when load

  • start_dir: To test the module name or a test case directory
  • pattern = 'test * .py': indicates the file name to match with the principles of embodiments. Here .py matches the type of file "test" beginning, * represents any number of characters

  • top_level_dir = None of the top-level directory of the test module, if not the top-level directory, defaults to None

Example 1:

import unittest
test_dir = './'
#定义测试目录为当前目录
discover = unittest.defaultTestLoader.discover(test_dir,pattern='test*.py')
if __name__ == '__main__':
    runner = unittest.TextTestRunner()
    runner.run(discover)

Discover () according to the test method of automatically match lookup directory test_dir test file and find the test suite to test assembled, therefore, can be directly
method performed discover run (), search and greatly simplifies the implementation of test cases

Example 2:

suite = unittest.TestSuite()
all_cases = unittest.defaultTestLoader.discover(PY_PATH,'Test*.py')
#discover()方法会自动根据测试目录匹配查找测试用例文件(Test*.py),并将查找到的测试用例组装到测试套件中
[suite.addTests(case) for case in all_cases]
report_html = BeautifulReport.BeautifulReport(suite)

Reference: https://blog.csdn.net/happyuu/article/details/80683161

Guess you like

Origin www.cnblogs.com/521world/p/11407753.html