I have a script for testing a module using unittest. When I run the script using the python console I get the output:
test_equal (__main__.TestOutcome) ... ok
test_win_amount (__main__.TestOutcome) ... ok
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
But, on running the same script using IPython console, I don't get any output.
I am using the following to run my script,
suite = unittest.TestLoader().loadTestsFromTestCase(TestOutcome)
unittest.TextTestRunner(verbosity=2).run(suite)
Any ideas if this might be due to IPython settings?
The command to run the tests is python -m unittest filename.py . In our case, the command to run the tests is python -m unittest test_utils.py .
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.
Which is better – pytest or unittest? Although both the frameworks are great for performing testing in python, pytest is easier to work with. The code in pytest is simple, compact, and efficient. For unittest, we will have to import modules, create a class and define the testing functions within that class.
Calling TextTestRunner with the stream parameter will make it work in IPython. This is how I run the tests:
suite = unittest.TestLoader().loadTestsFromTestCase(MyTest)
unittest.TextTestRunner(verbosity=1,stream=sys.stderr).run(suite)
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