DEV Community

Discussion on: Alternate to Nose for Newbie Tester

Collapse
 
j23schoen profile image
Justin Schoen

My beef with using unittest.TestCase is that if you use TestCase assertions, your code is not very pythonic. When you use pytest as your test runner, you can use the built in python assert and pytest will override assert with its own built in assertions.

For example:

thing = True
assert thing vs self.assertTrue(thing)

items = [1, 2, 3, 4]
assert 1 in items vs self.assertIn(1, items)

I think the tests look cleaner when it's pure python code and you don't have to think about or look up what assertion methods are available to you.

Thread Thread
 
rhymes profile image
rhymes

pytest also allows you to run tests in parallel if you want

Thread Thread
 
grahamlyons profile image
Graham Lyons

I like the xunit style for the test organisation, the well-defined life-cycle (setUp, tearDown (ignoring the un-Pythonic camel-casing)) and the portability.

Yes, you can organise your tests in a module but it's likely you'll have multiple functions and classes in a module and I like to have a corresponding test module with a TestCase class per function/class.

You can use the pytest life-cycle decorators on functions but that then ties you more closely to pytest. Using unittest allows you to switch runners more easily.

I have stopped using the assert* methods on self though - I use plain assert statements which give nicer output from pytest.

Thread Thread
 
rhymes profile image
rhymes

Good compromise :)