python - 如何将不同的pytest测试附加到同一个junit xml文件而不是覆盖它?
问题描述
我在 shell 脚本中有以下函数:
test_handler(){
FOLDER_NAME=$1
echo "running tests in: ${FOLDER_NAME} package"
cd ${SOURCE_CODE_FOLDER}/${FOLDER_NAME}
pipenv install --dev
#need to run this with pipenv run to get the install dependencies.
pipenv run run-tests
EXIT_CODE=$?
if [ ${EXIT_CODE} != 0 ];then
echo "error, Exit code=${EXIT_CODE} in ${FOLDER_NAME}'s tests." >> /home/logs.txt;
exit 1;
fi;
echo "${FOLDER_NAME}'s tests succeeded." >> /home/logs.txt;
}
该功能工作正常。它在脚本中用两个不同的文件夹名称调用两次,这样它们每个都有一个“测试”包,里面有 pytests。
该行pipenv run run-tests
正在运行以下脚本:
#!/bin/bash
python3.7 -m pytest -s --cov-append --junitxml=/home/algobot-packer/tests.xml $PWD/tests/
EXIT_CODE=$?
exit ${EXIT_CODE}
最终它会生成一个tests.xml
文件。唯一的问题是第二个函数调用覆盖了第一个。
有没有办法生成一个 xml 文件来保存两次运行测试脚本的结果(附加结果而不是重写文件)?
我试过查看文档,pytest --help
但找不到我的答案。
解决方案
您可以生成一个新报告,然后合并两个 XML 报告,而不是附加 JUnit XML 报告。有许多图书馆可以做到这一点。
这是一个使用junitparser合并两个 JUnit 报告的示例:
from junitparser import JUnitXml
full_report = JUnitXml.fromfile('/path/to/full_report.xml')
new_report = JUnitXml.fromfile('/path/to/new_report.xml')
# Merge in place and write back to same file
full_report += new_report
full_report.write()
推荐阅读
- python - 从 python 字典中获取所有子键的列表
- excel - 将数据从多个工作簿复制和组织到另一个工作簿
- html - HTML 输入字段右填充
- typescript - next.js/typescript/webpack 的问题:“未处理的拒绝(ReferenceError):未定义 XYZ”
- ios - LAContext.biometryType 总是返回 LABiometryTypeTouchID
- waf - 使用多个依赖输入文件生成 WAF 代码
- oracle-adf - 如何使 af:inputText 在 adf 中可调整大小?
- android - 为什么使用 ScrollView.FOCUS_DOWN 时 ScrollView 滚动到顶部然后到底部?
- python - 使用 TensorFlow 进行未来预测
- javascript - 将 post 请求中的标头发送到 API 的问题