Pytest

Pytest是一个灵活且功能丰富的Python单元测试框架,支持参数化、自动化测试文件发现、fixture机制和插件化。它适用于各种Python应用的测试,包括Web、移动应用、数据库等。Pytest具有与unittest和nose的兼容性,可扩展性高,便于编写和执行高效的测试代码。使用pytest的基本步骤包括安装、编写测试文件和函数,通过pytest命令运行测试,并可利用参数化和fixture进行复杂场景的测试。
摘要由CSDN通过智能技术生成

pytest是一个Python的单元测试框架,它支持参数化,自动化发现测试文件,fixture机制,插件化扩展等多种特性,使得Python单元测试更加方便和灵活。适用于Python应用程序的单元测试,功能测试和集成测试。它可以用于测试任何Python代码,包括Web应用程序,移动应用程序,数据库,网络协议,GUI应用程序等等。Pytest具有很强的可扩展性,可以与其他测试库和框架无缝集成,比如unittest和nose。它还支持参数化测试,测试生成器,插件架构等特性,使得测试编写和执行变得更加简单和高效。总的来说,Pytest是一个灵活,易用,功能强大的Python测试框架,可以帮助开发者更快速地编写高质量的测试代码以下是使用pytest的基本步骤:

  1. 安装pytest:可以使用pip命令安装pytest,如:pip install pytest

  2. 编写测试文件:测试文件一般以“test_”开头或者结尾,例如test_example.py

  3. 编写测试函数:测试函数以“test_”开头,例如test_example_case

  4. 运行pytest命令:在命令行中切换到测试文件所在目录,然后运行‘pytest’命令即可运行所有的测试用例。如果仅运行某个测试文件或者某个测试函数,可以在pytest命令后加上相应的文件名或函数名。

  5. 使用参数化:如果需要对同一个测试函数进行多次测试,可以使用pytest.mark.parametrize装饰器来实现参数化测试。如:

    import pytest
    
    @pytest.mark.parametrize('test_input, expected_output',
                             [
                                 ('3+5', 8),
                                 ('2+4', 6),
                                 ('6*9', 54)
                             ]
                             )
    def test_eval(test_input, expected_output):
        assert eval(test_input) == expected_output
     
    

  6. 使用fixture:fixture是pytest的一种机制,可以在测试函数运行前和运行后执行一些操作,例如初始化、清理等。如:
    import pytest
    
    @pytest.fixture
    def input_value():
        input = 39
        return input
    
    def test_divisible_by_3(input_value):
        assert input_value % 3 == 0
    
    def test_divisible_by_6(input_value):
        assert input_value % 6 == 0
     
    

实际案例:

import pytest
import requests

BASE_URL = "https://dev-api.example.com"
USERNAME = "testuser"
PASSWORD = "testpassword"
token = ""

def setup():
    global token
    url = BASE_URL + "/auth"
    data = {
        "username": USERNAME,
        "password": PASSWORD
    }
    response = requests.post(url, data=data)
    response_json = response.json()
    assert response_json['status'] == 'success'
    token = response_json['data']['token']
    return token


@pytest.mark.parametrize("product, price, qty", [
    ("apple", 10, 1),
    ("orange", 20, 2)
])
def test_create_order(product, price, qty):
    url = BASE_URL + "/orders"
    headers = {"Authorization": f"Bearer {token}"}
    data = {"product": product, "price": price, "qty": qty}
    response = requests.post(url, headers=headers, json=data)
    assert response.status_code == 200
    assert response.json()['status'] == 'success'


@pytest.mark.parametrize("order_id, product, price, qty", [
    (1, "apple", 10, 1),
    (2, "orange", 20, 2)
])
def test_update_order(order_id, product, price, qty):
    url = BASE_URL + f"/orders/{order_id}"
    headers = {"Authorization": f"Bearer {token}"}
    data = {"product": product, "price": price, "qty": qty}
    response = requests.put(url, headers=headers, json=data)
    assert response.status_code == 200
    response_json = response.json()
    assert response_json['status'] == 'success'
    assert response_json['data']['product'] == product
    assert response_json['data']['price'] == price
    assert response_json['data']['qty'] == qty


@pytest.mark.parametrize("order_id", [1, 2])
def test_delete_order(order_id):
    url = BASE_URL + f"/orders/{order_id}"
    headers = {"Authorization": f"Bearer {token}"}
    response = requests.delete(url, headers=headers)
    assert response.status_code == 200
    response_json = response.json()
    assert response_json['status'] == 'success'


@pytest.mark.parametrize("order_id, expected_product, expected_price, expected_qty", [
    (1, "apple", 10, 1),
    (2, "orange", 20, 2)
])
def test_get_order(order_id, expected_product, expected_price, expected_qty):
    url = BASE_URL + f"/orders/{order_id}"
    headers = {"Authorization": f"Bearer {token}"}
    response = requests.get(url, headers=headers)
    assert response.status_code == 200
    response_json = response.json()
    assert response_json['status'] == 'success'
    assert response_json['data']['product'] == expected_product
    assert response_json['data']['price'] == expected_price
    assert response_json['data']['qty'] == expected_qty

在这个pytest自动化测试脚本中,我们使用了全局变量 token 来保存登录后返回的 token,使用 setup 函数来进行登录操作,并将返回的 token 设置为全局变量 token

在每个测试函数中,我们先使用全局变量 token 来添加认证头部,然后执行对应的订单操作,最后判断返回结果是否符合预期。

我们可以在终端中运行以下命令来执行这些测试:

$ pytest test_order.py

最终的执行结果会显示在终端中,如果有测试失败,将会显示失败的详细信息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值