首页 > 解决方案 > 如何将不同的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但找不到我的答案。

标签: pythonjunitpytestpipenv

解决方案


您可以生成一个新报告,然后合并两个 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()

推荐阅读