Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append different pytest tests to the same junit xml file instead of overriding it?

I have the following function in a shell script:

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;
}

The function is working fine. It is called twice in the script with two different folder names, such that each of them has a "test" package with pytests inside.

The line pipenv run run-tests is running the following script:

#!/bin/bash
python3.7 -m pytest -s --cov-append --junitxml=/home/algobot-packer/tests.xml $PWD/tests/
EXIT_CODE=$?

exit ${EXIT_CODE}

Eventually it generates a tests.xml file. The only problem is that the second function call is overriding the first one.

Is there a way to generate one xml file that holds the results of running the tests script twice (appending the results instead of rewriting the file)?

I've tried looking at the docs and pytest --help but couldn't find my answer.

like image 764
Daniel Avatar asked Mar 24 '20 15:03

Daniel


Video Answer


1 Answers

Instead of appending the JUnit XML report, you can generate a new one, then merge the two XML reports. There are a number of libraries to do this.

Here's an example using junitparser to merge two JUnit reports:

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()
like image 81
DV82XL Avatar answered Oct 11 '22 01:10

DV82XL