Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unittest output in IPython

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?

like image 660
Nitin Avatar asked Aug 19 '13 00:08

Nitin


People also ask

How do I run a Unittest in Python?

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 .

What does Unittest main () do?

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?

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.


Video Answer


1 Answers

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)
like image 59
pvl Avatar answered Oct 14 '22 14:10

pvl