DEV Community

Cover image for Alternate to Nose for Newbie Tester
Stacy Montemayor
Stacy Montemayor

Posted on

Alternate to Nose for Newbie Tester

Aloha Y'all

I have recently started learning Python and I'm working my way through Learn Python the Hard Way. In the sections for testing, he shows you how to use Nose for testing and setting up your test environment. The problem is that it looks like Nose is no longer being supported. And, in a nutshell, I keep running into trouble as I'm using it.

Since it's an outdated tool, I thought it would be better if I learn testing with a tool that is up to date and popular.

What simple alternatives to Nose do you like/use? Extra points for keeping in mind I will be testing very simple projects. Thanks!

Top comments (9)

Collapse
 
rhymes profile image
rhymes • Edited

My favorite is pytest

Once you get how pytest fixtures work you can do everything :D

It is active, latest release is from yesterday

Collapse
 
grahamlyons profile image
Graham Lyons

I would second pytest. I was a big fan of Nose but pytest seems much better supported and with more useful features (coloured output in Nose isn't in the core).

I would stay away from pytest fixtures - they're what put me off it for a few years. I use pytest in an xunit style, using unittest.TestCase as a base class. I've worked on a large code base using pytest fixtures and they can go from cute magic trick to arcane sorcery with very little clue to their origin.

No magic in my Python, please.

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 :)

Collapse
 
rhymes profile image
rhymes • Edited

I would stay away from pytest fixtures - they're what put me off it for a few years. I use pytest in an xunit style, using unittest.TestCase as a base class.

I don't really like class based unit tests (most of my Python code is already "functionalish")

I've worked on a large code base using pytest fixtures and they can go from cute magic trick to arcane sorcery with very little clue to their origin.

Ah ah that's true, but documentation should help a little bit. The cool thing about pytest fixtures is that they can be interdependent, they can be executed once per test, once per module, once per session and so on. They do speed up setup if used correctly. They can become quite obscure as you said.

A huge advantage of pytest/functional testing with fixtures is that they make dependency injection very explicit and they make for easier refactoring. You can mostly cut and paste tests around

Thread Thread
 
grahamlyons profile image
Graham Lyons • Edited

I don't really like class based unit tests

See above for my reasoning on code organisation. I tend to write quite functional code too but I'll have a TestCase class with multiple test_* methods for testing the function.

I stand by my mistrust of pytest fixtures though - sorcery and black magic. (But I will go and take another look at them on your recommendation).

Collapse
 
stacy profile image
Stacy Montemayor

Well, it seems like there's strong support for pytest!