zoukankan      html  css  js  c++  java
  • pytest(五)用例传fixture参数

    fixture 里面有个参数autouse,默认是False没开启的,可以设置为True开启自动使用fixture功能,这样用例就不用每次都去传参了

     调用fixture三种方法:

    • 函数或类里面方法直接传fixture的函数参数名称
    • 使用装饰器@pytest.mark.usefixtures()修饰
    • auto=True自动使用

    用例传fixture参数

    方法一:先定义start功能,用例全部传start参数,调用该功能

    import time
    import pytest
    
    
    @pytest.fixture(scope="function")
    def start(request):
         print('
    ---------开始执行function-------')
    
    
    def test_a(start):
         print("---------用例a执行-------“)
    
    
    
    class Test_aa():
         def test_1(self, start):
               print('---------用例1------------‘')
    
        
         def test_2(self, start):
               print('---------用例2-------")
    
    if __name__=="__main__"
          pytest.main(["-s", "test_000.py"])
    

      方法二:使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例

    import time
    import pytest
    
    
    @pytest.fixture(scope="function")
    def start(request):
         print('
    ---------开始执行function-------')
    
    
    
    @pytest.mark.usefixtures("start")
    def test_a(start):
         print("---------用例a执行-------“)
    
    
    
    @pytest.mark.usefixtures("start")
    class Test_aa():
         def test_1(self, start):
               print('---------用例1------------‘')
    
        
         def test_2(self, start):
               print('---------用例2-------")
    
    if __name__=="__main__"
          pytest.main(["-s", "test_001.py"])
    

      方法三:设置autouse=True  auto 设置为True 自动调用fixture功能

    • start设置scope为module级别,在当前.py用例模块只执行一次,autouse =True自动使用
    • open_home设置scope 为function级别,每个用例前都调用一次,自动使用
    import time
    import pytest
    
    
    @pytest.fixture(scope="module",autouse = True)
    def start(request):
         print('
    ---------开始执行module-------')
         print('module:%s' % request.module.__name__)
         print('-------启动浏览器------')
         yield
         print("--------结束测试end!-----------")
    
    
    
    @pytest.fixture(scope="function",autouse = True)
    def open_home(request):
         print('function:%s 
    ----------回到首页--------' % request.function.__name__)
    
    
    
    @pytest.mark.usefixtures("start")
    class Test_aa():
         def test_1(self, start):
               print('---------用例1------------‘')
    
        
         def test_2(self, start):
               print('---------用例2-------")
    
    if __name__=="__main__"
          pytest.main(["-s", "test_002.py"])
    

      

  • 相关阅读:
    Spring学习(一):理解IoC容器
    Spring学习(零):我们为什么要学习Spring
    git push提交报错,提示文件过大,且去掉大文件也报同样的错误
    There was a problem with the instance info replicator
    Eureka配置instanceId显示IP
    Cannot execute request on any known server或DiscoveryClient_UNKNOWN/DESKTOP-MQ8D0C9:8761
    Hexo优化 | 创建sitemap站点地图并向Google提交
    matlab与python读取tiff文件
    Visual studio中编译和使用libpng和zlib
    关于softmax、argmax、softargmax
  • 原文地址:https://www.cnblogs.com/zhangying1/p/14281312.html
Copyright © 2011-2022 走看看