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")
    
  • 相关阅读:
    花儿飘落何处
    别了,攀枝花
    致我心爱的梦中女孩
    解锁华为云AI如何助力无人车飞驰“新姿势”,大赛冠军有话说
    技术实操丨HBase 2.X版本的元数据修复及一种数据迁移方式
    技术实践丨手把手教你使用MQTT方式对接华为IoT平台 华为云开发者社区
    必须收藏:20个开发技巧教你开发高性能计算代码
    原来AI也可以如此简单!教你从0到1开发开源知识问答机器人
    诸多老牌数据仓库厂商当前,Snowflake如何创近12年最大IPO金额
    详解GaussDB(DWS) explain分布式执行计划
  • 原文地址:https://www.cnblogs.com/gerenboke/p/13617016.html
Copyright © 2011-2022 走看看