zoukankan      html  css  js  c++  java
  • pytest(9):fixture的高级用法

    fixture之autouse

      我们已经知道fixture结合conftest.py使用时会更加方便,那么如果我们每条测试用例都需要使用fixture时怎么处理呢,这时就可以在装饰器里面添加一个参数 autouse=True,它会自动应用到所有的测试方法中,只是没有办法返回值给测试用例。

    使用方法:

    创建conftest.py文件

    1 import pytest
    2 @pytest.fixture(autouse=True)
    3 def login():
    4     print("输入登录的用户名和密码")
    5     print ('username','password')
    6     yield
    7     print("登录结束")

    创建文件“test_autouse.py”,代码如下:

    @pytest.fixture 里设置 autouse 参数值为 true后,每个测试函数都会自动调用这个fixture函数,autouse 默认是 false

     1 # Author xuejie zeng
     2 # encoding utf-8
     3 
     4 class Test_login():
     5     def test_1(self):
     6         print("test_1,需要登录")
     7 
     8     def test_2(self):
     9         print("test_2,需要登录 ")
    10 
    11     def test_3(self):
    12         print("test_case3,需要登录")

    运行结果:

     1 test_fixture.py::Test_login::test_1 输入登录的用户名和密码
     2 username password
     3 test_1,需要登录
     4 PASSED登录结束
     5 
     6 test_fixture.py::Test_login::test_2 输入登录的用户名和密码
     7 username password
     8 test_2,需要登录
     9 PASSED登录结束
    10 
    11 test_fixture.py::Test_login::test_3 输入登录的用户名和密码
    12 username password
    13 test_case3,需要登录
    14 PASSED登录结束

    从上面的运行结果可以看出,在方法上面添加了装饰器 @pytest.fixture(autouse=True)后,测试用例无须传入这个 fixture 的名字,它会自动在每条用例前执行这个 fixture。

    fixture 传递参数

    测试中我们可能会需要大量的测试数据,但是每条数据都写一遍用例,代码量是非常大的,这时候我们就可以使用 fixture 的参数化功能, @pytest.fixture(params=[1,2,3]),分别将这三个数据传入到用例当中。传入的数据使用固定的参数名 request 来接收。

    创建"conftest,py"文件:

    # Author xuejie zeng
    # encoding utf-8
    
    import pytest
    @pytest.fixture(params=[1,2,3])
    def params_conf(request):
        return request.param

    创建“test_params.py文件”:

    # Author xuejie zeng
    # encoding utf-8
    
    class Test_login():
        def test_1(self,params_conf):
            print(f'"test_1,参数值",{params_conf}')

    运行结果:

    test_fixture.py::Test_params::test_1[1] "test_1,参数值",1
    PASSED
    test_fixture.py::Test_params::test_1[2] "test_1,参数值",2
    PASSED
    test_fixture.py::Test_params::test_1[3] "test_1,参数值",3
    PASSED

    从运行结果可以看出,用例运行时会把params参数里面的每个值都调用一次,每个参数生成一个结果。

    多个参数

    稍微复杂的情况就是我们经常需要传入多个参数,举个例子比如登录时需要分别传入账号和密码,那么此时我们就需要最少传入两组数据。

    创建"conftest,py"文件:

    # Author xuejie zeng
    # encoding utf-8
    import pytest
    
    @pytest.fixture(scope="module")
    def input_user(request):
        username = request.param
        print(f'"登录账户:{username}')
        return username
    
    @pytest.fixture(scope="module")
    def input_psw(request):
        password = request.param
        print(f'"登录账户:{password}')
        return password

    创建“test_params.py文件”:

    # Author xuejie zeng
    # encoding utf-8
    import pytest
    
    # 测试账号数据
    test_user = ["account1", "account2"]
    test_psw = ["aaaa", "bbbb"]
    
    class Test_login():
        @pytest.mark.parametrize("input_user", test_user)
        @pytest.mark.parametrize("input_psw", test_psw)
        def test_1(self,input_user,input_psw):
            user = input_user
            psw = input_psw
            print(f'测试账号:{user},测试密码:{psw}')

    运行结果:

    test_fixture.py::Test_login::test_1[aaaa-account1] 测试账号:account1,测试密码:aaaa
    PASSED
    test_fixture.py::Test_login::test_1[aaaa-account2] 测试账号:account2,测试密码:aaaa
    PASSED
    test_fixture.py::Test_login::test_1[bbbb-account1] 测试账号:account1,测试密码:bbbb
    PASSED
    test_fixture.py::Test_login::test_1[bbbb-account2] 测试账号:account2,测试密码:bbbb
    PASSED

    从这个结果来看,当有两组参数时,用例的结果是一个排列组合的方式共有 4组结果。

    关注个人公众号:测试开发进阶之路

      

  • 相关阅读:
    Vue动画操作
    js this
    flask 操作mysql的两种方式-sqlalchemy操作
    flask 操作mysql的两种方式-sql操作
    ansible批量加用户
    使用 WTForms 进行表单验证的例子
    flask开发用户管理系统wtf版
    [转]Python格式化输出
    scrapy中response.body 与 response.text区别
    flask开发表单
  • 原文地址:https://www.cnblogs.com/zengxuejie/p/13740662.html
Copyright © 2011-2022 走看看