Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manupulate Python unittest output '.' , E and F

Python unittest give '.', E or F on ok, error or fail. We can avoid it by setting verbosity = 0, or /dev/null. but how can we change it. I mean I want to write after every test PASS, FAIL or ERROR instead of ., E or F, without using verbosity flag -V for output.

I am looking for following kind of output:

  1. test one PASS
  2. test two FAIL
  3. test three PASS
  4. test four ERROR

Thanks Zubair

like image 420
Zubair Maalick Avatar asked Oct 18 '25 14:10

Zubair Maalick


1 Answers

So using extra verbosity (-v) on the command line gets the desired output format:

Passing the -v option to your test script will instruct unittest.main() to enable a higher level of verbosity, and produce the following output:

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK

(https://docs.python.org/3/library/unittest.html#basic-example)

But we can also set the verbosity level from code:

You can run tests with more detailed information by passing in the verbosity argument:

if __name__ == '__main__':
    unittest.main(verbosity=2)

(https://docs.python.org/3/library/unittest.html#unittest.main)

If you want to further customize the output, see Stef's answer.

like image 170
Thom Wiggers Avatar answered Oct 21 '25 10:10

Thom Wiggers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!