Jenkins Job not working when pytest fails

Just wanted to learn pytest and integrate it into Jenkins. My pytest test cases

def a(x):
    return x+1

def test_answer():
    assert a(2) == 3

def test_answer2():
    assert a(0) == 2

      

Then I generated a standalone pytest script that I run in Jenkins, generating xml to parse the results.

As test_answer2 fails, Jenkins' job also fails. I am assuming this is because the return code returned is nonzero. How do I get around this i.e. Jenkins work even if 1 or more tests really fail. Thanks to

+3


source to share


3 answers


If you call this test execution in a batch file or shell script or directly using command execution in Jenkins. You can follow below:

Windows:

<your test execution calls>

exit 0

Linux:

set +e

<your test execution calls>

set -e

      



This will ignore the error if thrown at all in batch scripts, and Jenkins will show the status as successful.

+2


source


If you call this test execution in a batch file or shell script or directly using command execution in Jenkins. You can follow below: Below code does NOT work

Linux:

set +e

      



set -e

      

0


source


In addition to the answers already posted:

You can also mark your test as xfail

, which means that you know it will fail

as a pass:

@pytest.mark.skip(reason="no way of currently testing this")
def test_the_unknown():
    ...

      

you can find more about gaps in the Pytest documentation

and on the Jenkins side, you can also manipulate your build state with a simple try / catch statement:

    try {
        bat "python -m pytest ..."
    } catch (pytestError) {
        // rewrite state of you build as you want, Success or Failed
        // currentBuild.result = "FAILED"
        currentBuild.result = "SUCCESS" // your case
        println pytestError
    }

      

But keep in mind that this will mean that the entire build will be successful every time at this point in Pytest running. It is best to run the tests through @pytest.mark.skip

as described above.

0


source







All Articles