zoukankan      html  css  js  c++  java
  • test_fixture

    import pytest
    
    #函数级别
    #每个测试函数前都会执行一次
    # @pytest.fixture()
    # def before():
    #     print("在函数前执行")
    #
    # def test_a(before):
    #     print("test_a在执行")
    #
    #
    # def test_b(before):
    #     print("test_b在执行")
    #
    
    
    ######################################################
    #作为装饰器执行
    
    # @pytest.fixture()
    # def before():
    #     print("在函数前执行")
    #
    # @pytest.mark_usefixtures('before')
    # def test_a():
    #     print("test_a在执行")
    #
    #
    # @pytest.mark_usefixtures('before')
    # def test_b():
    #     print("test_b在执行")
    #
    # @pytest.mark_usefixtures('before')
    # class Test_ABC():
    #     def test_c(self):
    #         print("test_c在执行")
    #     def test_d(self):
    #         print("test_d在执行")
    
    ###################################################
    
    ###################################################
    #自动运行
    # @pytest.fixture(scope="function",autouse=True)
    # def before():
    #     print("在函数前执行")
    #
    # class Test_ABC():
    #     def test_a(self):
    #         print("test_a执行")
    #     def test_b(self):
    #         print("test_b执行")
    
    #####################################################
    #作用域改为类,类里面的每个函数只执行一次
    #但是类外面的函数每次都会调用before
    # @pytest.fixture(scope="class",autouse=True)
    # def before():
    #     print("
    在函数前执行
    ")
    #
    # @pytest.mark.usefixtures("before")
    # def test_c():
    #     print("
    test_c在执行
    "
    #
    # @pytest.mark.usefixtures("before")
    # def test_d():
    #     print("
    test_d在执行
    ")
    #
    # class Test_ABC():
    #     def test_a(self):
    #         print("
    test_a执行
    ")
    #     def test_b(self):
    #         print("test_b执行")
    ######################################################
    
    #改为模块的使用
    #在模块中只执行一次,不管是否调用
    # @pytest.fixture(scope="module",autouse=True)
    # def before():
    #     print("
    在函数前执行
    ")
    #
    # @pytest.mark.usefixtures("bofore")
    # def test_1():
    #     print("
    test1在执行")
    #
    # @pytest.mark.usefixtures("bofore")
    # class Test_ABC():
    #     def test_a(self):
    #         print("
    test_a在执行")
    #     def test_b(self):
    #         print("
    test_b在执行")
    
    #使用param传递参数
    @pytest.fixture(params=[1,2,3])
    def need_data(request): #传入参数request 系统封装参数
        return request.param    #取列表中单个值,默认的取值方式
    
    class Test_ABC():
        def test_a(self, need_data):
            print("test_a的值是 %s" % need_data)
    if __name__ == '__main__':
        pytest.main("test_fixture.py")
    
  • 相关阅读:
    猫与路由器(还没看)
    ORA-12154: TNS: 无法解析指定的连接标识符(未解决)
    easy-batch demo
    mongodb 创建用户
    docker mongodb
    监听器,过滤器,拦截器
    mysql docker-compose启动异常:Database is uninitialized and password option is not specified
    设计模式-builder模式的价值
    【C++ Template | 04】折叠表达式
    【vim】vim插件教程
  • 原文地址:https://www.cnblogs.com/gerenboke/p/13617016.html
Copyright © 2011-2022 走看看