#使用pytest的登录接口自动化测试案例
pip install pytest requests pytest-yaml


# test_login.py

import pytest
import requests
import yaml

def read_yaml(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        data = yaml.safe_load(file)
    return data

@pytest.fixture
def base_url():
    return "http://yourapi.com"

@pytest.mark.parametrize("username, password", read_yaml('test_data.yaml'))
def test_login(base_url, username, password):
    url = f"{base_url}/login"
    payload = {
        "username": username,
        "password": password
    }
    
    response = requests.post(url, json=payload)
    
    assert response.status_code == 200
    assert response.json()['success'] is True

包含参数化数据的 YAML 文件 test_data.yaml

- [ "testuser1", "password1" ]
- [ "testuser2", "password2" ]
- [ "testuser3", "password3" ]