Reputation: 103
When using my python unit tests, sometimes my tests become somewhat long due to test-specific inputs, or other lengthy logic. If one of my tests fail, unittest will print out the entire test, from the top of the list down to the assertion failure. The problem is that if my test is long for any reason, then when the test fails, the output becomes kind of a nuisance to scroll through.
Does anyone know of a way to make python unittest still show the assertion failure, but NOT print the entire test case?
E.G. for test:
def testArbitraryStringTest(self):
arbStrings = [
'abcdef',
'1a23',
'12l;kf3j',
'va;kljda',
'roenwfas',
'asdjvoisdp',
'v9jw3oawe',
'a;lvnpaos',
'vaoispvdjdas',
'avnoasdpf',
'voasdivpas',
'asvnoiasdv;',
'1a23',
'12l;kf3j',
'va;kljda',
'roenwfas',
'asdjvoisdp',
'v9jw3oawe',
'a;lvnpaos',
'vaoispvdjdas',
'avnoasdpf',
'voasdivpas',
'asvnoiasdv;',
'1a23',
'12l;kf3j',
'va;kljda',
'roenwfas',
'asdjvoisdp',
'v9jw3oawe',
'a;lvnpaos',
'vaoispvdjdas',
'avnoasdpf',
'voasdivpas',
'asvnoiasdv;',
'1a23',
'12l;kf3j',
'va;kljda',
'roenwfas',
'asdjvoisdp',
'v9jw3oawe',
'a;lvnpaos',
'vaoispvdjdas',
'avnoasdpf',
'voasdivpas',
'as',
]
for arbStr in arbStrings:
self.assertGreater(len(arbStr), 3)
instead of output:
============================= test session starts ==============================
collecting ... collected 1 item
SemanticTest.py::SemanticTest::testArbitraryStringTest FAILED [100%]
SemanticTest.py:15 (SemanticTest.testArbitraryStringTest)
self = <SemanticTest.SemanticTest testMethod=testArbitraryStringTest>
def testArbitraryStringTest(self):
arbStrings = [
'abcdef',
'1a23',
'12l;kf3j',
'va;kljda',
'roenwfas',
'asdjvoisdp',
'v9jw3oawe',
'a;lvnpaos',
'vaoispvdjdas',
'avnoasdpf',
'voasdivpas',
'asvnoiasdv;',
'1a23',
'12l;kf3j',
'va;kljda',
'roenwfas',
'asdjvoisdp',
'v9jw3oawe',
'a;lvnpaos',
'vaoispvdjdas',
'avnoasdpf',
'voasdivpas',
'asvnoiasdv;',
'1a23',
'12l;kf3j',
'va;kljda',
'roenwfas',
'asdjvoisdp',
'v9jw3oawe',
'a;lvnpaos',
'vaoispvdjdas',
'avnoasdpf',
'voasdivpas',
'asvnoiasdv;',
'1a23',
'12l;kf3j',
'va;kljda',
'roenwfas',
'asdjvoisdp',
'v9jw3oawe',
'a;lvnpaos',
'vaoispvdjdas',
'avnoasdpf',
'voasdivpas',
'as',
]
for arbStr in arbStrings:
> self.assertGreater(len(arbStr), 3)
E AssertionError: 2 not greater than 3
SemanticTest.py:65: AssertionError
============================== 1 failed in 0.34s ===============================
It would be nice to have something more like:
============================= test session starts ==============================
collecting ... collected 1 item
SemanticTest.py::SemanticTest::testArbitraryStringTest FAILED [100%]
E AssertionError: 2 not greater than 3
SemanticTest.py:65: AssertionError
============================== 1 failed in 0.34s ===============================
Thanks for looking!
Upvotes: 1
Views: 899
Reputation: 1329
I believe unittest does not have such traceback by default:
F
======================================================================
FAIL: testArbitraryStringTest (__main__.Test1)
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\repos\stackoverflow\tst.py", line 52, in testArbitraryStringTest
self.assertGreater(len(arbStr), 3)
AssertionError: 2 not greater than 3
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (failures=1)
Seems like you are using pytest
which has --tb
cli option.
You can use it as pytest --tb=short
so it will have following output:
============================= test session starts =============================
collecting ... collected 1 item
tst.py::Test1::testArbitraryStringTest FAILED [100%]
tst.py:2 (Test1.testArbitraryStringTest)
tst.py:52: in testArbitraryStringTest
self.assertGreater(len(arbStr), 3)
E AssertionError: 2 not greater than 3
============================== 1 failed in 0.06s ==============================
As you are using PyCharm: one of possible options to pass this argument is Configuration -> Additional Arguments
Upvotes: 1