python Interface Automation Framework uses 8-unittest

Foreword

unittest: Python unit testing framework, sSmalltalk testing framework based on the JUnit Erich Gamma and Kent Beck's.

A, unittest using basic frame

unittest should be noted that: ① class, inheriting unit testing unittest TestCase class; ② test cases must begin with a test. Learn basic front and rear flexible use and can be asserted.

1, setUp: each use case "before" are performed once, tearDown: "" After each use case is executed once, with the beginning of the test cases.

setUp, tearDown each with both front and rear cases are executed.

from the unittest Import TestCase 

# class inheritance: TestCase
 class Test_Login (TestCase): 

    DEF the setUp (Self): 
        Print ( ' each case "front" are executed once ' ) 

    DEF the tearDown (Self): 
        (Print ' after each use case " "are executed once ' ) 

    # the following are the use cases, need to have the beginning of the test, you can write multiple use cases. 
    test_001 DEF (Self): 
        Print ( ' I was Example: case_01 ' ) 

    DEF test_002 (Self): 
        Print ( ' I was Example: case_02 ' )

 2, setUpClass: Pre executed only once; tearDownClass: post only once.

 Need to use modifiers: @classmethod, and the function used in parentheses: cls

from unittest import TestCase

# class 继承:TestCase
class Test_Login(TestCase):
    
    # 需使用修饰符:@classmethod,且函数用:cls
    @classmethod
    def setUpClass(cls):
        print('全部用例“前”只 执行 1 次')

    @classmethod
    def tearDownClass(cls):
        print('全部用例“后”只 执行 1 次')

    def setUp(self):
        print('每个用例“前”都执行 1 次')

    def tearDown(self):
        print('每个用例“后”都执行 1 次')

    # 以下是用例,需已 test 开头,用例可以写多个。
    def test_001(self):
        print('我是用例:case_01')

    def test_002(self):
        print('我是用例:case_02')

 3、常用的两种断言方法(基本能供日常使用,不够自己去翻TestCase源码,如图二)

①两值相等:assertEqual(a , b)

②A值在B值里面:assertIn(A, B, msg) ;msg自己想写啥就写啥,类似出现错误后备注:断言失败返回控制台日志,等等

from unittest import TestCase

# class 继承:TestCase
class Test_Login(TestCase):

    def setUp(self):
        print('每个用例“前”都执行 1 次')

    def tearDown(self):
        print('每个用例“后”都执行 1 次')

    # 以下是用例,需已 test 开头,用例可以写多个。
    def test_001(self):
        print('我是用例:case_01')
        self.assertEqual((1+2), 3)                      # 断言两个值相等

    def test_002(self):
        print('我是用例:case_02')
        res = '广深'  # 假如这是实际结果
        self.assertIn('小龙', res, msg='他说没有小龙')  # 断言 小龙 在 res 中,如果不在msg是返回值
        self.assertTrue(res == '广深')                  # 断言两个值相等(也可以False,断言==、!=、in

 注意:不要执行用例去调用用例,用例是相互独立的,执行用例的顺序按照assic码:0-9 ,A-Z, a-z。欢迎来QQ交流群:482713805

Guess you like

Origin www.cnblogs.com/gsxl/p/11964177.html