How do I make py.test fail to run by external functions?

I am currently writing a script that installs my software under a test and then automatically runs my smoke tests using py.test. If any of these tests fail, I would like to say that my software does not publish software to build servers. This is basically how it goes in pseudocode:

def install_build_and_test():
    # some python code installs some_build
    install_my_build(some_build)

    # then I want to test my build
    subprocess.Popen(["py.test", "smoke_test_suite.py"])
    # test_failures = ???

    # If any failures occurred during testing, do not publish build 
    if test_failures is True:
        print "Build will not publish because there were errors in your logs"

    if test_failures is False:
        publish_build(some_build)

      

My question here is how can I use pytest errors to tell my install_and_test_build code to not post some_build?

+3


source to share


2 answers


Approach # 1

This, I think, is the path that you have taken. Basically, just treat test.py as a black box process and use the exit code to determine if there were any failures in testing (e.g. if there is a zero exit code)

exit_code = subprocess.Popen(["py.test", "smoke_test_suite.py"]).wait()
test_failures = bool(exit_code)

      



Approach # 2

Another cleaner way is to run py.test directly in python .

import pytest
exit_code = pytest.main("smoke_test_suite.py")
test_failures = bool(exit_code)

      

+3


source


py.test should return a non-zero exit code if tests fail. The easiest way to deal with this is subprocess.check_call()

::



try:
    subprocess.check_call(["py.test", "smoke_test_suite.py"])
except subprocess.CalledProcessError:
    print "Smoke tests have failed, not publishing"
else:
    print "Smoke tests have passed, publishing"
    # ...

      

0


source







All Articles