美文网首页
(五)pytest-测试用例编写注意事项以及用例排序

(五)pytest-测试用例编写注意事项以及用例排序

作者: Sandra_liu | 来源:发表于2021-07-15 19:00 被阅读0次

    1、pytest中class中不需要添加object
    2、测试用例(module)以test开头或者以test结尾
    类以Test开头
    方法以test开头
    3、用例排序:
    方法一:
    *模块级(setup_module/teardown_module):作用于模块(开始和结束各执行一次。)

    *函数级(setup_function/teardown_function):仅对函数用例生效(不在类中,每个函数执行一次。)

    *类级(setup_class/teardown_class):作用于类(在每个类中开始和结束各执行一次。)

    *方法级(setup_method/teardown_method):作用于方法(在类中,每个方法开始和结束各执行一次。)

    *类里面的(setup/teardown):作用于方法(每个方法执行一次。)

    import logging
    import os
    import pytest
    
    def setup_module():
        logging.info("setup_module")
    
    def teardown_module():
        logging.info("teardown_module")
    
    class Test_C:
        logging.info("class C")
    
    
    class Test_D:
        @classmethod
        def setup_class(cls):
            logging.info("setup_class")
    
        @classmethod
        def teardown_class(cls):
            logging.info("teardown_class")
    
        def setup_method(self):
            logging.info("setup_method")
    
        def teardown_method(self):
            logging.info("teardown_method")
    
        def setup(self):
            logging.info("setup")
    
        def teardown(self):
            logging.info("teardown")
    
        # 列表断言
        def test_007(self):
            logging.info("7")
            # print(os.getcwd())
            assert [1, 2] == [3, 4]
    
        # 元组断言
        def test_008(self):
            logging.info("8")
            assert (1, 2) == (1, 2)
    
        # 字典断言
        def test_009(self):
            logging.info("9")
            assert {'a': 1, 'b': 2} == {'a': 1, 'b': 2}
    
    if __name__ == '__main__':
        pytest.main(["-q", "--log-level=INFO" ,"-s","test_suite.py"])
    
    image.png

    方法二:pytest-ordering(已废弃)

    #使用字母排序
    import logging
    
    import pytest
    
    
    class Test_A2:
        logging.basicConfig(format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s',
                            level=logging.INFO)
        @pytest.mark.first
        def test_001(self):
            logging.info("1")
    
        @pytest.mark.last
        def test_002(self):
            logging.info("2")
    
        @pytest.mark.second
        def test_003(self):
            logging.info("3")
    
    
        @pytest.mark.second_to_last
        def test_004(self):
            logging.info("4")
    
    
    #使用数字排序
    class Test_A1:
        @pytest.mark.run(order = 1)
        def test_001(self):
            logging.info("1")
    
        @pytest.mark.run(order =3)
        def test_002(self):
            logging.info("2")
    
    
        @pytest.mark.run(order = 2)
        def test_003(self):
            logging.info("3")
    
    
        @pytest.mark.run(order= 4)
        def test_004(self):
            logging.info("4")
    if __name__ == '__main__':
        pytest.main(["-q", "--log-level=INFO" ,"-s","test_order_2.py"])
    
    
    

    相关文章

      网友评论

          本文标题:(五)pytest-测试用例编写注意事项以及用例排序

          本文链接:https://www.haomeiwen.com/subject/fbxupltx.html