Ranserunoputo
Ranserunoputo

Reputation: 23

how can I set the return code of pytest.main() to a non-zero when cases failed?

I am trying to add the exit code to my pytest, but the doc of pytest only tells the basic description of exit code.
It seems pytest.main will return 0 if the test complete successfully, but I want the it to return a non-zero code when any of the cases failed, how can I achieve that?
I tried a few times, it returns 1 when I use ctrl+C to stop the test and returns 2 when I simply types "pytest",but returns 0 even most of the cases failed.
Thank you

part of my code(really sorry I can't show all of it)

#!/usr/bin/env python
# -*- coding:utf8 -*-

import sys
import pytest
import argparse
import time
from pytest import ExitCode


def main():
……
    if args.arg == "all":
        logger.debug('test all')
        ret = pytest.main(["-v", "--capture=tee-sys", "--html=./temp/report_{}.html".format(VERSION), "--self-contained-html", "-n", "auto", "--dist", "no", "e2e/cases"]+sys.argv[2:])
        return ret


if __name__ == '__main__':
    main()

then I use tox to run it.

[tox]
envlist = py37

skipsdist=true
toxworkdir= {toxinidir}/var/.tox
indexserver =
    default = https://pip.nie.netease.com/simple

[testenv]

passenv = *

changedir={toxinidir}
deps =
    -r{toxinidir}/e2e/misc/requirements.txt
recreate=true
setenv =

    PYTHONDONTWRITEBYTECODE = 1
commands =
    python -m e2e all {posargs}

I am sorry if it's uncomplete and hard to understand......

Upvotes: 1

Views: 1194

Answers (1)

anthony sottile
anthony sottile

Reputation: 69914

you need to make sure you exit the process with the return value you're interested in

right now you have

if __name__ == '__main__':
    main()

when main returns nonzero, this return value is discarded

instead, exit the process either with raise SystemExit or sys.exit:

if __name__ == '__main__':
    raise SystemExit(main())

Upvotes: 2

Related Questions