Files
everything-is-suitable/everything-is-suitable-api/api-test-tool/tests/unit/test_test_data_manager.py
T
张翔 08ea5fbe98 feat(admin): 添加用户管理相关文件
添加用户管理视图、API和状态管理文件
2026-03-28 14:37:29 +08:00

298 lines
9.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""测试数据管理器单元测试"""
import pytest
from pathlib import Path
from unittest.mock import Mock
from apitest.data.test_data_manager import TestDataManager
from apitest.models.test_models import TestCase, HTTPMethod
from apitest.models.exceptions import TestRunException
@pytest.fixture
def mock_logger():
"""创建模拟日志记录器"""
return Mock()
@pytest.fixture
def data_manager(mock_logger):
"""创建测试数据管理器实例"""
return TestDataManager(logger=mock_logger)
@pytest.fixture
def sample_test_cases():
"""创建示例测试用例"""
return [
TestCase(
id="TC001",
name="测试用例1",
description="测试用例1",
module="test",
endpoint="/api/test1",
method=HTTPMethod.GET,
headers={},
enabled=True
),
TestCase(
id="TC002",
name="测试用例2",
description="测试用例2",
module="test",
endpoint="/api/test2",
method=HTTPMethod.POST,
headers={},
enabled=True
)
]
def test_load_test_cases_from_json_success(data_manager, tmp_path):
"""测试从JSON文件加载测试用例(成功)"""
import json
test_data = [
{
"id": "TC001",
"name": "测试用例1",
"description": "测试用例1",
"module": "test",
"endpoint": "/api/test1",
"method": "GET",
"headers": {},
"enabled": True
},
{
"id": "TC002",
"name": "测试用例2",
"description": "测试用例2",
"module": "test",
"endpoint": "/api/test2",
"method": "POST",
"headers": {},
"enabled": True
}
]
test_file = tmp_path / "test_cases.json"
with open(test_file, "w", encoding="utf-8") as f:
json.dump(test_data, f)
test_cases = data_manager.load_test_cases_from_json(test_file)
assert len(test_cases) == 2
assert test_cases[0].id == "TC001"
assert test_cases[1].id == "TC002"
assert test_cases[0].method == HTTPMethod.GET
assert test_cases[1].method == HTTPMethod.POST
def test_load_test_cases_from_json_file_not_found(data_manager, tmp_path):
"""测试从JSON文件加载测试用例(文件不存在)"""
test_file = tmp_path / "nonexistent.json"
with pytest.raises(TestRunException) as exc_info:
data_manager.load_test_cases_from_json(test_file)
assert "测试用例文件不存在" in str(exc_info.value)
def test_load_test_cases_from_json_invalid_json(data_manager, tmp_path):
"""测试从JSON文件加载测试用例(无效JSON"""
test_file = tmp_path / "invalid.json"
with open(test_file, "w", encoding="utf-8") as f:
f.write("{ invalid json }")
with pytest.raises(TestRunException) as exc_info:
data_manager.load_test_cases_from_json(test_file)
assert "JSON文件解析失败" in str(exc_info.value)
def test_load_test_cases_from_json_invalid_method(data_manager, tmp_path):
"""测试从JSON文件加载测试用例(无效HTTP方法)"""
import json
test_data = [
{
"id": "TC001",
"name": "测试用例1",
"description": "测试用例1",
"module": "test",
"endpoint": "/api/test1",
"method": "INVALID",
"headers": {},
"enabled": True
}
]
test_file = tmp_path / "test_cases.json"
with open(test_file, "w", encoding="utf-8") as f:
json.dump(test_data, f)
test_cases = data_manager.load_test_cases_from_json(test_file)
assert len(test_cases) == 1
assert test_cases[0].method == HTTPMethod.GET
def test_load_test_data_from_csv_success(data_manager, tmp_path):
"""测试从CSV文件加载测试数据(成功)"""
test_file = tmp_path / "test_data.csv"
with open(test_file, "w", encoding="utf-8", newline="") as f:
f.write("param1,param2,param3\n")
f.write("value1,value2,value3\n")
f.write("value4,value5,value6\n")
test_data = data_manager.load_test_data_from_csv(test_file)
assert len(test_data) == 2
assert test_data[0] == {"param1": "value1", "param2": "value2", "param3": "value3"}
assert test_data[1] == {"param1": "value4", "param2": "value5", "param3": "value6"}
def test_load_test_data_from_csv_file_not_found(data_manager, tmp_path):
"""测试从CSV文件加载测试数据(文件不存在)"""
test_file = tmp_path / "nonexistent.csv"
with pytest.raises(TestRunException) as exc_info:
data_manager.load_test_data_from_csv(test_file)
assert "测试数据文件不存在" in str(exc_info.value)
def test_parameterize_test_case_success(data_manager, sample_test_cases):
"""测试参数化测试用例(成功)"""
test_data = [
{"params": {"id": "1"}, "body": {"name": "test1"}},
{"params": {"id": "2"}, "body": {"name": "test2"}}
]
parameterized_cases = data_manager.parameterize_test_case(
sample_test_cases[0],
test_data
)
assert len(parameterized_cases) == 2
assert parameterized_cases[0].id == "TC001_1"
assert parameterized_cases[0].name == "测试用例1 (数据集 1)"
assert parameterized_cases[0].params == {"id": "1"}
assert parameterized_cases[0].body == {"name": "test1"}
assert parameterized_cases[1].id == "TC001_2"
assert parameterized_cases[1].name == "测试用例1 (数据集 2)"
assert parameterized_cases[1].params == {"id": "2"}
assert parameterized_cases[1].body == {"name": "test2"}
def test_parameterize_test_case_empty_data(data_manager, sample_test_cases):
"""测试参数化测试用例(空数据)"""
test_data = []
parameterized_cases = data_manager.parameterize_test_case(
sample_test_cases[0],
test_data
)
assert len(parameterized_cases) == 0
def test_save_test_cases_to_json_success(data_manager, sample_test_cases, tmp_path):
"""测试保存测试用例到JSON文件(成功)"""
output_file = tmp_path / "output.json"
data_manager.save_test_cases_to_json(sample_test_cases, output_file)
assert output_file.exists()
import json
with open(output_file, "r", encoding="utf-8") as f:
data = json.load(f)
assert len(data) == 2
assert data[0]["id"] == "TC001"
assert data[1]["id"] == "TC002"
def test_save_test_cases_to_json_create_directory(data_manager, sample_test_cases, tmp_path):
"""测试保存测试用例到JSON文件(创建目录)"""
output_file = tmp_path / "nested" / "dir" / "output.json"
data_manager.save_test_cases_to_json(sample_test_cases, output_file)
assert output_file.exists()
assert output_file.parent.exists()
def test_save_test_data_to_csv_success(data_manager, tmp_path):
"""测试保存测试数据到CSV文件(成功)"""
test_data = [
{"param1": "value1", "param2": "value2"},
{"param1": "value3", "param2": "value4"}
]
output_file = tmp_path / "output.csv"
data_manager.save_test_data_to_csv(test_data, output_file)
assert output_file.exists()
import csv
with open(output_file, "r", encoding="utf-8") as f:
reader = csv.DictReader(f)
rows = list(reader)
assert len(rows) == 2
assert rows[0]["param1"] == "value1"
assert rows[1]["param1"] == "value3"
def test_save_test_data_to_csv_with_fieldnames(data_manager, tmp_path):
"""测试保存测试数据到CSV文件(指定字段名)"""
test_data = [
{"param1": "value1", "param2": "value2", "param3": "value3"},
{"param1": "value4", "param2": "value5", "param3": "value6"}
]
output_file = tmp_path / "output.csv"
data_manager.save_test_data_to_csv(
test_data,
output_file,
fieldnames=["param1", "param2"]
)
assert output_file.exists()
import csv
with open(output_file, "r", encoding="utf-8") as f:
reader = csv.DictReader(f)
rows = list(reader)
fieldnames = reader.fieldnames
assert len(rows) == 2
assert fieldnames == ["param1", "param2"]
def test_save_test_data_to_csv_empty_data(data_manager, tmp_path):
"""测试保存测试数据到CSV文件(空数据)"""
test_data = []
output_file = tmp_path / "output.csv"
with pytest.raises(TestRunException) as exc_info:
data_manager.save_test_data_to_csv(test_data, output_file)
assert "测试数据为空" in str(exc_info.value)
def test_save_test_data_to_csv_create_directory(data_manager, tmp_path):
"""测试保存测试数据到CSV文件(创建目录)"""
test_data = [{"param1": "value1"}]
output_file = tmp_path / "nested" / "dir" / "output.csv"
data_manager.save_test_data_to_csv(test_data, output_file)
assert output_file.exists()
assert output_file.parent.exists()