Docker build: returned a non-zero code: 1, when test is failed

Viewed 3608

When I run Docker build with my project Docker+Selenium+Pytest in Jenkins CI with tests that end with the SUСCESS status - the build is pushed and the results are published to reports, and if at least one test fails - the build fails and the results are not published

Build Error: The command 'pytest test_page.py -s -v --alluredir=reports/allure-results' returned a non-zero code: 1

Maybe my instructions for Docker are incorrectly configured. My DockerFile

FROM python:latest  as python3
FROM selenium/standalone-chrome
USER root
WORKDIR /my-projest
ADD . /my-projest
RUN pip3 install --no-cache-dir --user -r requirements.txt
RUN sudo pip3 install pytest
RUN ["pytest", "test_page.py", "-s", "-v", "--alluredir=reports/allure-results"]

and SHELL Command

echo "Build docker image and run container"
docker build -t $IMAGE_NAME .
docker run -d --name $CONTAINER_NAME $IMAGE_NAME

echo "Copy allure-results into Jenkins container"
rm -rf reports; mkdir reports;

docker cp $CONTAINER_NAME:my-project/reports/allure-results reports
3 Answers

It may be that your tests are failing on an assertion and that failed assertion may be throwing the non 0 error code.

this link outlines the expected exit codes for each scenario

Exit code 0
All tests were collected and passed successfully

Exit code 1
Tests were collected and run but some of the tests failed

Exit code 2
Test execution was interrupted by the user

Exit code 3
Internal error happened while executing tests

Exit code 4
pytest command line usage error

Exit code 5
No tests were collected

Problem is when testcases are failing docker build is exiting with non-zero code.

One way around to generate report even when testcases are failed

echo "Build docker image and run container"
docker build -t $IMAGE_NAME .


echo "Copy allure-results into Jenkins container"
rm -rf reports
docker create -it --name $CONTAINER_NAME $IMAGE_NAME /bin/bash
docker cp $CONTAINER_NAME:my-project/reports/allure-results ./reports
docker rm -f $CONTAINER_NAME

You can user report copy part in Jenkins pipeline in post stage under always block, so that whether build pass or fail you can always get reports.

I found a solution to this issue: added at the end of the RUN command - exit 0

Related