美文网首页
Pytest-4 fixture

Pytest-4 fixture

作者: iscona | 来源:发表于2023-08-14 17:24 被阅读0次
    1. 用例1需要先登录,用例2不需要登录,用例3需要先登录。很显然这就无法用setup和teardown来实现了
      firture相对于setup和teardown来说应该有以下几点优势:
    • 命名方式灵活,不局限于setup和teardown这几个命名
    • conftest.py 配置里可以实现数据共享,不需要import就能自动找到一些配置
    • scope=”module” 可以实现多个.py跨文件共享前置
    • scope=”session” 以实现多个.py跨文件使用一个session来完成多个用例
    fixture(scope="function", params=None, autouse=False, ids=None, name=None):
        """使用装饰器标记fixture的功能
         可以使用此装饰器(带或不带参数)来定义fixture功能。 fixture功能的名称可以在以后使用
         引用它会在运行测试之前调用它:test模块或类可以使用pytest.mark.usefixtures(fixturename标记。 
         测试功能可以直接使用fixture名称作为输入参数,在这种情况下,夹具实例从fixture返回功能将被注入。
    
        :arg scope: scope 有四个级别参数 "function" (默认), "class", "module" or "session".
    
        :arg params: 一个可选的参数列表,它将导致多个参数调用fixture功能和所有测试使用它
    
        :arg autouse:  如果为True,则为所有测试激活fixture func 可以看到它。 如果为False(默认值)则显式需要参考来激活fixture
    
        :arg ids: 每个字符串id的列表,每个字符串对应于params 这样他们就是测试ID的一部分。 如果没有提供ID它们将从params自动生成
    
        :arg name:   fixture的名称。 这默认为装饰函数的名称。 如果fixture在定义它的同一模块中使用,夹具的功能名称将被请求夹具的功能arg遮蔽; 解决这个问题的一种方法是将装饰函数命名
                           “fixture_ <fixturename>”然后使用”@ pytest.fixture(name ='<fixturename>')“”。
    
    1. fixture参数传入(scope=”function”)
      实现场景:用例1需要先登录,用例2不需要登录,用例3需要先登录
    # coding:utf-8
    
    import pytest
    
    #不带参数时默认scope="function"此时的级别的function,针对函数有效
    @pytest.fixture()
    def login():
        print("输入账号,密码先登录")
    
    def test_s1(login):
        print("用例1:登录之后其他动作111")
    
    def test_s2():
        print("用例2:不需要登录,操作222")
    
    def test_s3(login):
        print("用例3:登陆之后其他动作333")
    
    if __name__ == "__main__":
        pytest.main(["-s", "test_fixture.py"])
    
    ➜  testcases pytest test_fixture.py -s
    ================================= test session starts ==================================
    platform darwin -- Python 3.7.2, pytest-7.4.0, pluggy-1.2.0
    rootdir: /Users/qina/workspace/python-space/python-test/testcases
    collected 3 items
    
    test_fixture.py 输入账号,密码先登录
    用例1:登录之后其他动作111
    .用例2:不需要登录,操作222
    .输入账号,密码先登录
    用例3:登陆之后其他动作333
    .
    
    ================================== 3 passed in 0.01s ===================================
    
    1. conftest.py配置
      上面一个案例是在同一个.py文件中,多个用例调用一个登陆功能,如果有多个.py的文件都需要调用这个登陆功能的话,那就不能把登陆写到用例里面去了。
      此时应该要有一个配置文件,单独管理一些预置的操作场景,pytest里面默认读取conftest.py里面的配置
      conftest.py配置需要注意以下点:
      conftest.py配置脚本名称是固定的,不能改名称
      conftest.py与运行的用例要在同一个pakage下,并且有init.py文件
      不需要import导入 conftest.py,pytest用例会自动查找
    __init__.py
    conftest.py
      # coding:utf-8
      import pytest
    
      @pytest.fixture()
      def login():
          print("输入账号密码,需要先登录")
    
    test_fix1.py
      # coding:utf-8
      import pytest
    
      def test_s1(login):
          print("[test_fix1][test_s1]  登陆之后其他动作111")
    
      def test_s2():
          print("[test_fix1][test_s2]  不需要登录,操作222")
    
      def test_s3(login):
          print("[test_fix1][test_s2]  登陆之后其他动作333")
    
      if __name__ == "__main__":
          pytest.main(["-s", "test_fix1.py"])
    
    
    test_fix2.py
    
      # coding:utf-8
      import pytest
    
      def test_s4(login):
          print("[test_fix2][test_s3]  登录之后其他动作444")
    
      def test_s5():
          print("[test_fix2][test_s5]  不需要登录,操作555")
    
      if __name__ == "__main__":
          pytest.main(["-s", "test_fix2.py"])
    
    ➜  testcases pytest -q test_fix1.py test_fix2.py -s
    输入账号密码,需要先登录
    [test_fix1][test_s1]  登陆之后其他动作111
    .[test_fix1][test_s2]  不需要登录,操作222
    .输入账号密码,需要先登录
    [test_fix1][test_s2]  登陆之后其他动作333
    .输入账号密码,需要先登录
    [test_fix2][test_s3]  登录之后其他动作444
    .[test_fix2][test_s5]  不需要登录,操作555
    .
    5 passed in 0.01s
    

    相关文章

      网友评论

          本文标题:Pytest-4 fixture

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