如何将不同的 pytest 测试附加到同一个 junit xml 文件而不是覆盖它?

Dan*_*iel 6 python junit pytest pipenv

我在 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;
}
Run Code Online (Sandbox Code Playgroud)

该函数工作正常,在脚本中被调用两次,使用两个不同的文件夹名称,每个文件夹名称都有一个“测试”包,里面有 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}
Run Code Online (Sandbox Code Playgroud)

最终它生成一个tests.xml 文件。唯一的问题是第二个函数调用覆盖了第一个函数调用。

有没有办法生成一个 xml 文件来保存两次运行测试脚本的结果?(附加结果而不是重写文件)我试过查看文档和 pytest --help 但找不到我的答案。

DV8*_*2XL 5

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

  • junitparser 还提供了一个用于合并的[命令行工具](https://github.com/weiwei/junitparser#command-line): `junitparser merge input-1.xml input-2.xml output.xml` (3认同)