五、pytest接口自动化教程--fixture基础用法

一、fixture详解

1、fixture概念:
fixture是pytest用于将测试前后进行预备、清理工作的代码处理机制。
2、fixture相对于setup和teardown来说有以下几点优势:
(1)fixture命名更加灵活,局限性比较小
(2)conftest.py配置里面可以实现数据共享,不需要import就能自动找到一些配置
3、fixture夹具,@pytest.fixture
(scop=‘function’)每一个函数或方法都会调用
(scop=‘class’)每一个类调用一次
(scop=‘module’)每一个.py文件调用一次
(scop=‘session’)是多个文件调用一次,.py文件就是module
fixture的作用范围:session>module>class>function

(1)(scop=‘function’)每一个函数或方法都会调用

'''
@Author     : 测试工程师Selina
@FileName   : test_fixture.py
@Description: 
'''
import pytest


@pytest.fixture()
def func():
    print('我是前置步骤')


def test_rule1(func):
    assert 1 == 1


def test_rule2(func):
    assert 2 == 2
    print('pytest -q001')


if __name__ == '__main__':
    pytest.main(['s', 'test_fixture.py'])
    
====================== test session starts ==========================
collecting ... collected 2 items
test_fixture.py::test_rule1 
我是前置步骤
PASSED                                       [ 50%]
test_fixture.py::test_rule2 
我是前置步骤
PASSED                                       [100%]pytest -q001
====================== 2 passed in 0.01s ==========================
'''
@Author     : 测试工程师Selina
@FileName   : test_fixture.py
@Description: 
'''
import pytest

## @pytest.fixture(scope='function', autouse=True)
@pytest.fixture(autouse=True)
def func():
    print('我是前置步骤')


def test_rule1():
    assert 1 == 1


def test_rule2():
    assert 2 == 2
    print('pytest -q001')


if __name__ == '__main__':
    pytest.main(['s', 'test_fixture.py'])
    
====================== test session starts ==========================
collecting ... collected 2 items
test_fixture.py::test_rule1 
我是前置步骤
PASSED                                       [ 50%]
test_fixture.py::test_rule2 
我是前置步骤
PASSED                                       [100%]pytest -q001
====================== 2 passed in 0.01s ==========================

(2)(scop=‘class’)每个类调用一次

'''
@Author     : 测试工程师Selina
@FileName   : test_fixture.py
@Description: 
'''
import pytest

## @pytest.fixture(scope='class', autouse=True)
@pytest.fixture(scope='class')
def func():
    print('我是前置步骤')


class Testclassfixture:
    def test_rule1(self, func):
        assert 1 == 1

    def test_rule2(self, func):
        assert 2 == 2
        print('pytest -q001')


if __name__ == '__main__':
    pytest.main(['s', 'test_fixture.py'])
    
====================== test session starts =========================
collecting ... collected 2 items
test_fixture.py::Testclassfixture::test_rule1 
我是前置步骤
PASSED                     [ 50%]
test_fixture.py::Testclassfixture::test_rule2 
PASSED                     [100%]pytest -q001
======================== 2 passed in 0.01s ===========================

(3)(scop=‘module’)每一个.py文件调用一次(不管这个文件里有多少方法,只运行一次)

'''
@Author     : 测试工程师Selina
@FileName   : test_fixture.py
@Description: 
'''
import pytest

@pytest.fixture(scope='module', autouse=True)
def func():
    print('我是前置步骤')

class Testclassfixture:
    def test_rule1(self):
        assert 1 == 1
    def test_rule2(self):
        assert 2 == 2
        print('pytest -q001')

class Testfixture:
    def test_rule3(self):
        assert 1 == 1
    def test_rule4(self):
        assert 2 == 2
        print('pytest -q001')

if __name__ == '__main__':
    pytest.main(['s', 'test_fixture.py'])

====================== test session starts ===========================
collecting ... collected 4 items
test_fixture.py::Testclassfixture::test_rule1
我是前置步骤
PASSED                     [ 25%]
test_fixture.py::Testclassfixture::test_rule2 
PASSED                     [ 50%]pytest -q001

test_fixture.py::Testfixture::test_rule3 
PASSED                          [ 75%]
test_fixture.py::Testfixture::test_rule4 
PASSED                          [100%]pytest -q001
========================= 4 passed in 0.01s ================================

(4)(scop=‘session’)是多个文件调用一次,.py文件就是module
一般所有的前置步骤放在conftest.py的文件中

二、pytest使用conftest管理fixture

为了方便测试框架的使用,所有的fixture前置后置内容都会放在conftest.py的文件中
在这里插入图片描述

'''
@Author     : 测试工程师Selina
@FileName   : test_fixture.py
@Description: 
'''
import pytest


def test_rule1(func):
    assert 1 == 1


def test_rule2(func):
    assert 2 == 2
    print('pytest -q001')


if __name__ == '__main__':
    pytest.main(['s', 'test_fixture.py'])
    
================= test session starts =========================
collecting ... collected 2 items
test_fixture.py::test_rule1 
我是前置步骤,我需要先运行
PASSED                                       [ 50%]
test_fixture.py::test_rule2 
我是前置步骤,我需要先运行
PASSED                                       [100%]
pytest -q001
======================== 2 passed in 0.01s ==========================

三、pytest使用fixture返回数据

(1)return返回值

@pytest.fixture(scope='function')
def func():
     return params
'''
@Author     : 测试工程师Selina
@FileName   : test_fixture.py
@Description: 
'''
import pytest
import requests


@pytest.fixture()
def get_DoctorDept():
    params = {'workerId': '1037', 'hospitalCode': '1'}
    return params


def test_DoctorDept(get_DoctorDept):
    Id = get_DoctorDept['workerId']
    hospitalCode = get_DoctorDept['hospitalCode']
    print('测试医生就诊科室')
    r=requests.get('http://ip地址:端口号/outPatient/reception/getDoctorDept',
               params={'workerId':Id,'hospitalCode':hospitalCode})  
    print(r.status_code)

(2)参数可以放在conftest.py文件中:

'''
@Author     : 测试工程师Selina
@FileName   : conftest.py
@Description: 
'''
import pytest

@pytest.fixture()
def get_DoctorDept():
    params = {'workerId': '1037', 'hospitalCode': '1'}
    return params
    
@pytest.fixture(scope='session')
def test_session():
    print('我是session级别的fixture')

四、pytest使用yield做后置处理

'''
@Author     : 测试工程师Selina
@FileName   : conftest.py
@Description: yield可以传参,一般不建议这样使用
'''
import pytest
@pytest.fixture(scope='function')
def func():
    print('我是前置步骤,我需要先运行')
    yield 'Selina'
    print('我是后置步骤,我最后运行')
'''
@Author     : 测试工程师Selina
@FileName   : test_fixture.py
@Description: 
'''
import pytest


def test_rule1(func):
    assert 1 == 1
    
def test_rule2(func):
    assert 2 == 2
    print('pytest -q001')
    
============================= test session starts ==============================
collecting ... collected 2 items

test_fixture.py::test_rule1 
我是前置步骤,我需要先运行
PASSED                                       [ 50%]
我是后置步骤,我最后运行

test_fixture.py::test_rule2 
我是前置步骤,我需要先运行
PASSED                                       [100%]pytest -q001
我是后置步骤,我最后运行


============================== 2 passed in 0.01s ===============================

五、pytest的fixture执行顺序

fixture的作用范围:session>module>class>function

'''
@Author     : 测试工程师Selina
@FileName   : test_fixture.py
@Description: pytest的fixture执行顺序
'''
import pytest


@pytest.fixture(scope='session')
def t_session():
    print('我是session级别的fixture')


@pytest.fixture(scope='module')
def t_module():
    print('我是module级别的fixture')


@pytest.fixture(scope='class')
def t_class():
    print('我是class级别的fixture')


@pytest.fixture(scope='function')
def t_function():
    print('我是function级别的fixture')


class TestOrder:
    def test_order(self, t_session, t_module, t_function, t_class):
        assert 1 == 1
        
======================= test session starts ==========================
collecting ... collected 1 item

test_fixture_order.py::TestOrder::test_order 
我是session级别的fixture
我是module级别的fixture
我是class级别的fixture
我是function级别的fixture
PASSED                      [100%]

========================= 1 passed in 0.01s =========================

六、pytest的usefixtures方法

@pytest.fixture(scope='function')
def func():
xxx

第一种,可以接受返回值
def test_func(func):
    xxx
第二种,无法接收返回值
@pytest.mark.usefixtures('func')
def test_func():
    xxx

七、pytest的fixture中的params和ids

(params=[‘参数1’,‘参数2’], ids=[‘用例1’],[‘用例2’])

'''
@Author     : 测试工程师Selina
@FileName   : test_fixture_params.py
@Description: 
'''
import pytest

'''ids就是测试用例的名字,params是测试用例传递的参数'''


@pytest.fixture(params=['数据1', '数据2'], ids=['case1', 'case2'])
def params_fixture(request):
    return request.param


def test_params(params_fixture):
    print(params_fixture)


if __name__ == '__main__':
    pytest.main(['s', 'test_fixture_params.py'])

    
====================== test session starts ===========================
collecting ... collected 2 items
test_fixture_params.py::test_params[case1] 
PASSED                        [ 50%]数据1
test_fixture_params.py::test_params[case2] 
PASSED                        [100%]数据2
===================== 2 passed in 0.01s ==============================
Process finished with exit code 0
### 回答1: 在进行接口测试时,使用自动化框架可以提高工作效率并减少出错的可能性。Python中的requests库可以方便地发送HTTP请求并获取响应,而pytest则是一个功能强大的测试框架,可以支持接口测试自动化。 1. requests库是Python中的HTTP客户端库,可以方便地构建和发送HTTP请求,并解析返回的响应数据。requests库的文档详细且易于理解,可以快速掌握该库的使用方法,例如发送基本的GET、POST、PUT等HTTP请求,设置请求头、请求体、cookies等,也可以进行文件上传和下载。 2. pytest是一个基于Python的测试框架,它提供了完整的测试和夹具API,并支持并行化测试和分布式测试。pytest的测试用例可以非常灵活和高效地管理和维护,可以根据需要添加更多的测试用例,而不会破坏原有的测试逻辑。 3. 接口自动化测试主要包括如下几个步骤:构建请求参数、发送HTTP请求、解析响应数据、对返回结果进行断言。使用requests和pytest库可以方便地完成以上操作,例如使用pytestfixture、参数化和mark机制可以让测试用例更容易编写和维护,而requests则可以方便地发送HTTP请求和获取响应。 4. 使用Python、requests以及pytest可以轻松构建接口自动化测试框架,并集成到CI/CD流程中实现持续集成和自动化部署,从而提高软件质量、减少故障率和提高开发效率。同时,Python作为一门便于阅读的语言,使得代码易于理解和维护,也更适合于多人协作开发。 ### 回答2: Python requests pytest 接口自动化框架是一种基于 Python 语言的测试框架,主要用于对接口进行自动化测试,旨在提高测试效率,降低测试成本。该框架由 Python requests 库和 pytest 测试框架组成,可以快速、方便地进行接口测试。 Python requests 库是 Python 中的一个 HTTP 库,用于发送 HTTP 请求,处理 HTTP 响应并支持 RESTful API。该库简化了 HTTP 请求的过程,提供了易于使用的方法和参数。 而 pytest 是一个功能强大的 Python 测试框架,可以自动化执行测试用例,提供了易于编写和扩展的测试样式。与传统的测试框架相比,pytest 拥有更高的执行效率和更好的测试结果报告。 在使用该框架进行接口自动化测试时,首先需要使用 pytest 编写测试用例,然后使用 Python requests 库发送 HTTP 请求,并进行断言和验证响应结果。该框架支持多种请求方法、参数、Cookie、Headers 和响应格式等,可根据需要进行自定义。 另外,该框架还支持测试用例的参数化、Fixture 和模块化等功能,可以实现重复测试用例的复用、测试环境的配置和共享等。 总之,Python requests pytest 接口自动化框架是一种功能强大、易学易用的自动化测试工具,可以有效提高测试效率和质量。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Selina 0_0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值