import os
import os
def pytest_terminal_summary(terminalreporter, exitstatus, config):
summary_stats = terminalreporter.stats
passed = len(summary_stats.get('passed', []))
failed = len(summary_stats.get('failed', []))
skipped = len(summary_stats.get('skipped', []))
errors = len(summary_stats.get('error', []))
xfailed = len(summary_stats.get('xfailed', []))
xpassed = len(summary_stats.get('xpassed', []))
warnings = len(summary_stats.get('warnings', []))
print(f"测试总数: {passed + failed + skipped}")
print(f"成功的测试: {passed}")
print(f"失败的测试: {failed}")
print(f"跳过的测试: {skipped}")
print(f"出现错误的测试: {errors}")
print(f"预计失败的测试 (XFailed): {xfailed}")
print(f"意外通过的测试 (XPassed): {xpassed}")
print(f"警告数: {warnings}")
html_report = getattr(config.option, "htmlpath", None)
json_report = getattr(config.option, "json_report_file", None)
if html_report:
print(f"HTML 报告已生成: {os.path.abspath(html_report)}")
if json_report:
print(f"JSON 报告已生成: {os.path.abspath(json_report)}")
current_dir = os.path.dirname(os.path.abspath(__file__))
report_file = os.path.join(current_dir, 'pytest_results.txt')
with open(report_file, 'w') as f:
f.write(f"PASSED={passed}\n")
f.write(f"FAILED={failed}\n")
f.write(f"SKIPPED={skipped}\n")
f.write(f"ERRORS={errors}\n")
report_file="tests/pytest_results.txt"
if [[ ! -f $report_file ]]; then
echo "Report file not found!"
exit 1
fi
while IFS='=' read -r key value; do
case $key in
PASSED) PASSED=$value ;;
FAILED) FAILED=$value ;;
SKIPPED) SKIPPED=$value ;;
ERRORS) ERRORS=$value ;;
esac
done < "$report_file"
echo "Passed tests: $PASSED"
echo "Failed tests: $FAILED"
echo "Skipped tests: $SKIPPED"
echo "Errors: $ERRORS"
其它:
```bash
import os
import subprocess
import xml.etree.ElementTree as ET
def run_tests_and_generate_report():
current_directory = os.path.dirname(os.path.abspath(__file__))
report_directory = os.path.join(current_directory, "unit", "reports")
html_report_file = "report.html"
xml_report_file = "report.xml"
html_report_path = os.path.join(report_directory, html_report_file)
xml_report_path = os.path.join(report_directory, xml_report_file)
os.makedirs(report_directory, exist_ok=True)
result = subprocess.run(
["pytest", f"--html={html_report_path}", f"--junitxml={xml_report_path}"],
capture_output=True,
text=True,
encoding="utf-8",
)
print(result.stdout)
print(result.stderr)
absolute_html_report_path = os.path.abspath(html_report_path)
print(f"HTML report generated at: {absolute_html_report_path}")
parse_test_results(xml_report_path)
if result.returncode == 0:
print("Tests ran successfully.")
else:
print("Tests failed.")
return result.returncode
def parse_test_results(xml_report_path):
tree = ET.parse(xml_report_path)
root = tree.getroot()
for testsuite in root.findall("testsuite"):
name = testsuite.get("name")
errors = int(testsuite.get("errors") or 0)
failures = int(testsuite.get("failures") or 0)
skipped = int(testsuite.get("skipped") or 0)
tests = int(testsuite.get("tests") or 0)
passed = tests - (failures + errors + skipped)
print(f"Testsuite: {name}")
print(f"Errors: {errors}")
print(f"Failures: {failures}")
print(f"Skipped: {skipped}")
print(f"Total tests: {tests}")
print(f"Passed tests: {passed}")
print(f"Time: {testsuite.get('time')}")
print(f"TESTSUITE_NAME='{name}'")
print(f"ERRORS={errors}")
print(f"FAILURES={failures}")
print(f"SKIPPED={skipped}")
print(f"TOTAL_TESTS={tests}")
print(f"PASSED_TESTS={passed}")
print(f"TIME='{testsuite.get('time')}'")