I wrote some 'unittest' code, and it's great. I can see it on the CLI when I run it manually.
Now I want to hook it up to run automatically as part of a merge to master hook on my repository. I have everything set up with dummy code except for the part of grabbing the results of the unittest programmatically.
When I call unittest.main()
to run them all, it throws a SystemExit. I've tried catching it and rerouting the standard output, but I wasn't able to get it to work, and it also feels like I'm doing it wrong. Is there an easier way to get the results of the unittests, like in a Python list of line strings, or even a more complicated result object?
Really for my purposes, I'm only interested in 100% pass or fail, and then showing that visual in the repository on a pull request to master, with a link to the full unittest result details.
I'm also not married to 'unittest' if some other Python unit test framework can be called and pass off results easily.
You can pass exit=False
to unittest.main
, and capture the return value. To run it from another script, or the interactive interpreter, you can specify a target module
as well.
That gives us an instance of the TestCase
or TestSuite
class that was executed. The internal unittest
machinery will make a TestResult
object available in the result
attribute of that object.
That object will have a TestResult.wasSuccessful
method that gives the result you're looking for.
Assuming you have some file tests.py
:
from unittest import main
test = main(module='tests', exit=False)
print(test.result.wasSuccessful())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With