08ea5fbe98
添加用户管理视图、API和状态管理文件
356 lines
10 KiB
Python
356 lines
10 KiB
Python
"""报告管理器单元测试"""
|
|
|
|
import pytest
|
|
from pathlib import Path
|
|
from datetime import datetime
|
|
from unittest.mock import Mock, patch
|
|
from apitest.report.report_manager import ReportManager
|
|
from apitest.models.test_models import TestCase, TestResult, TestSuiteResult, HTTPMethod, PerformanceMetrics
|
|
from apitest.models.exceptions import ReportException
|
|
|
|
|
|
@pytest.fixture
|
|
def report_manager():
|
|
"""创建报告管理器实例"""
|
|
logger = Mock()
|
|
return ReportManager(logger)
|
|
|
|
|
|
@pytest.fixture
|
|
def test_suite_result():
|
|
"""创建测试套件结果"""
|
|
test_cases = [
|
|
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
|
|
)
|
|
]
|
|
|
|
results = [
|
|
TestResult(
|
|
test_case=test_cases[0],
|
|
passed=True,
|
|
status_code=200,
|
|
response_body={"message": "success"},
|
|
response_headers={"Content-Type": "application/json"},
|
|
performance=PerformanceMetrics(
|
|
timestamp=datetime.now(),
|
|
response_time=123,
|
|
request_size=0,
|
|
response_size=0
|
|
)
|
|
),
|
|
TestResult(
|
|
test_case=test_cases[1],
|
|
passed=False,
|
|
status_code=500,
|
|
response_body={"error": "internal error"},
|
|
response_headers={"Content-Type": "application/json"},
|
|
performance=PerformanceMetrics(
|
|
timestamp=datetime.now(),
|
|
response_time=456,
|
|
request_size=0,
|
|
response_size=0
|
|
),
|
|
error_message="服务器内部错误"
|
|
)
|
|
]
|
|
|
|
return TestSuiteResult(
|
|
suite_name="Test Suite",
|
|
total=2,
|
|
passed=1,
|
|
failed=1,
|
|
skipped=0,
|
|
results=results,
|
|
start_time=datetime.now()
|
|
)
|
|
|
|
|
|
def test_generate_html_report_success(report_manager, test_suite_result, tmp_path):
|
|
"""测试生成HTML报告(成功)"""
|
|
report_path = tmp_path / "test_report.html"
|
|
|
|
result_path = report_manager.generate_html_report(
|
|
test_suite_result,
|
|
report_path,
|
|
title="测试报告"
|
|
)
|
|
|
|
assert result_path == str(report_path)
|
|
assert report_path.exists()
|
|
|
|
content = report_path.read_text(encoding="utf-8")
|
|
assert "测试报告" in content
|
|
assert "Test Suite" in content
|
|
assert "TC001" in content
|
|
assert "TC002" in content
|
|
assert "测试用例1" in content
|
|
assert "测试用例2" in content
|
|
assert "50.0%" in content or "50.0" in content
|
|
|
|
|
|
def test_generate_html_report_create_directory(report_manager, test_suite_result, tmp_path):
|
|
"""测试生成HTML报告(创建目录)"""
|
|
report_path = tmp_path / "reports" / "nested" / "test_report.html"
|
|
|
|
result_path = report_manager.generate_html_report(
|
|
test_suite_result,
|
|
report_path
|
|
)
|
|
|
|
assert result_path == str(report_path)
|
|
assert report_path.exists()
|
|
assert report_path.parent.exists()
|
|
|
|
|
|
def test_generate_json_report_success(report_manager, test_suite_result, tmp_path):
|
|
"""测试生成JSON报告(成功)"""
|
|
report_path = tmp_path / "test_report.json"
|
|
|
|
result_path = report_manager.generate_json_report(
|
|
test_suite_result,
|
|
report_path
|
|
)
|
|
|
|
assert result_path == str(report_path)
|
|
assert report_path.exists()
|
|
|
|
import json
|
|
content = report_path.read_text(encoding="utf-8")
|
|
data = json.loads(content)
|
|
|
|
assert data["suite_name"] == "Test Suite"
|
|
assert data["total"] == 2
|
|
assert data["passed"] == 1
|
|
assert data["failed"] == 1
|
|
assert data["skipped"] == 0
|
|
assert len(data["results"]) == 2
|
|
|
|
|
|
def test_generate_json_report_create_directory(report_manager, test_suite_result, tmp_path):
|
|
"""测试生成JSON报告(创建目录)"""
|
|
report_path = tmp_path / "reports" / "nested" / "test_report.json"
|
|
|
|
result_path = report_manager.generate_json_report(
|
|
test_suite_result,
|
|
report_path
|
|
)
|
|
|
|
assert result_path == str(report_path)
|
|
assert report_path.exists()
|
|
assert report_path.parent.exists()
|
|
|
|
|
|
def test_generate_html_report_logger_call(report_manager, test_suite_result, tmp_path):
|
|
"""测试生成HTML报告时调用日志记录器"""
|
|
report_path = tmp_path / "test_report.html"
|
|
|
|
report_manager.generate_html_report(
|
|
test_suite_result,
|
|
report_path
|
|
)
|
|
|
|
report_manager.logger.info.assert_called()
|
|
|
|
|
|
def test_generate_json_report_logger_call(report_manager, test_suite_result, tmp_path):
|
|
"""测试生成JSON报告时调用日志记录器"""
|
|
report_path = tmp_path / "test_report.json"
|
|
|
|
report_manager.generate_json_report(
|
|
test_suite_result,
|
|
report_path
|
|
)
|
|
|
|
report_manager.logger.info.assert_called()
|
|
|
|
|
|
def test_generate_html_report_content_structure(report_manager, test_suite_result, tmp_path):
|
|
"""测试HTML报告内容结构"""
|
|
report_path = tmp_path / "test_report.html"
|
|
|
|
report_manager.generate_html_report(
|
|
test_suite_result,
|
|
report_path,
|
|
title="API测试报告"
|
|
)
|
|
|
|
content = report_path.read_text(encoding="utf-8")
|
|
|
|
assert "<!DOCTYPE html>" in content
|
|
assert "<html" in content
|
|
assert "<head>" in content
|
|
assert "<body>" in content
|
|
assert "API测试报告" in content
|
|
assert "总用例数" in content
|
|
assert "通过" in content
|
|
assert "失败" in content
|
|
assert "跳过" in content
|
|
assert "通过率" in content
|
|
assert "执行时长" in content
|
|
assert "测试结果详情" in content
|
|
assert "</html>" in content
|
|
|
|
|
|
def test_generate_html_report_with_all_passed(report_manager, tmp_path):
|
|
"""测试生成HTML报告(全部通过)"""
|
|
test_cases = [
|
|
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
|
|
)
|
|
]
|
|
|
|
results = [
|
|
TestResult(
|
|
test_case=test_cases[0],
|
|
passed=True,
|
|
status_code=200,
|
|
response_body={"message": "success"},
|
|
response_headers={"Content-Type": "application/json"},
|
|
performance=PerformanceMetrics(
|
|
timestamp=datetime.now(),
|
|
response_time=123,
|
|
request_size=0,
|
|
response_size=0
|
|
)
|
|
),
|
|
TestResult(
|
|
test_case=test_cases[1],
|
|
passed=True,
|
|
status_code=200,
|
|
response_body={"message": "success"},
|
|
response_headers={"Content-Type": "application/json"},
|
|
performance=PerformanceMetrics(
|
|
timestamp=datetime.now(),
|
|
response_time=456,
|
|
request_size=0,
|
|
response_size=0
|
|
)
|
|
)
|
|
]
|
|
|
|
test_suite_result = TestSuiteResult(
|
|
suite_name="Test Suite",
|
|
total=2,
|
|
passed=2,
|
|
failed=0,
|
|
skipped=0,
|
|
results=results,
|
|
start_time=datetime.now()
|
|
)
|
|
|
|
report_path = tmp_path / "test_report.html"
|
|
report_manager.generate_html_report(test_suite_result, report_path)
|
|
|
|
content = report_path.read_text(encoding="utf-8")
|
|
assert "100.0%" in content or "100.0" in content
|
|
|
|
|
|
def test_generate_html_report_with_all_failed(report_manager, tmp_path):
|
|
"""测试生成HTML报告(全部失败)"""
|
|
test_cases = [
|
|
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
|
|
)
|
|
]
|
|
|
|
results = [
|
|
TestResult(
|
|
test_case=test_cases[0],
|
|
passed=False,
|
|
status_code=500,
|
|
response_body={"error": "error"},
|
|
response_headers={"Content-Type": "application/json"},
|
|
performance=PerformanceMetrics(
|
|
timestamp=datetime.now(),
|
|
response_time=123,
|
|
request_size=0,
|
|
response_size=0
|
|
),
|
|
error_message="错误1"
|
|
),
|
|
TestResult(
|
|
test_case=test_cases[1],
|
|
passed=False,
|
|
status_code=404,
|
|
response_body={"error": "not found"},
|
|
response_headers={"Content-Type": "application/json"},
|
|
performance=PerformanceMetrics(
|
|
timestamp=datetime.now(),
|
|
response_time=456,
|
|
request_size=0,
|
|
response_size=0
|
|
),
|
|
error_message="错误2"
|
|
)
|
|
]
|
|
|
|
test_suite_result = TestSuiteResult(
|
|
suite_name="Test Suite",
|
|
total=2,
|
|
passed=0,
|
|
failed=2,
|
|
skipped=0,
|
|
results=results,
|
|
start_time=datetime.now()
|
|
)
|
|
|
|
report_path = tmp_path / "test_report.html"
|
|
report_manager.generate_html_report(test_suite_result, report_path)
|
|
|
|
content = report_path.read_text(encoding="utf-8")
|
|
assert "0.0%" in content or "0.0" in content
|
|
assert "错误1" in content
|
|
assert "错误2" in content
|