I'm extending the python 2.7 unittest
framework to do some function testing. One of the things I would like to do is to stop all the tests from running inside of a test, and inside of a setUpClass()
method. Sometimes if a test fails, the program is so broken it is no longer of any use to keep testing, so I want to stop the tests from running.
I noticed that a TestResult has a shouldStop
attribute, and a stop()
method, but I'm not sure how to get access to that inside of a test.
Does anyone have any ideas? Is there a better way?
Once you are in a TestCase , the stop() method for the TestResult is not used when iterating through the tests. Somewhat related to your question, if you are using python 2.7, you can use the -f/--failfast flag when calling your test with python -m unittest . This will stop the test at the first failure. Thanks.
TestCase is used to create test cases by subclassing it. The last block of the code at the bottom allows us to run all the tests just by running the file.
An exception object is created when a Python script raises an exception. If the script explicitly doesn't handle the exception, the program will be forced to terminate abruptly.
Internally, unittest. main() is using a few tricks to figure out the name of the module (source file) that contains the call to main() . It then imports this modules, examines it, gets a list of all classes and functions which could be tests (according the configuration) and then creates a test case for each of them.
In case you are interested, here is a simple example how you could make a decision yourself about exiting a test suite cleanly with py.test:
# content of test_module.py import pytest counter = 0 def setup_function(func): global counter counter += 1 if counter >=3: pytest.exit("decided to stop the test run") def test_one(): pass def test_two(): pass def test_three(): pass
and if you run this you get:
$ pytest test_module.py ============== test session starts ================= platform linux2 -- Python 2.6.5 -- pytest-1.4.0a1 test path 1: test_module.py test_module.py .. !!!! Exit: decided to stop the test run !!!!!!!!!!!! ============= 2 passed in 0.08 seconds =============
You can also put the py.test.exit()
call inside a test or into a project-specific plugin.
Sidenote: py.test
natively supports py.test --maxfail=NUM
to implement stopping after NUM failures.
Sidenote2: py.test
has only limited support for running tests in the traditional unittest.TestCase
style.
Here's another answer I came up with after a while:
First, I added a new exception:
class StopTests(Exception): """ Raise this exception in a test to stop the test run. """ pass
then I added a new assert
to my child test class:
def assertStopTestsIfFalse(self, statement, reason=''): try: assert statement except AssertionError: result.addFailure(self, sys.exc_info())
and last I overrode the run
function to include this right below the testMethod()
call:
except StopTests: result.addFailure(self, sys.exc_info()) result.stop()
I like this better since any test now has the ability to stop all the tests, and there is no cpython-specific code.
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