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)
My favorite is pytest
Once you get how pytest fixtures work you can do everything :D
It is active, latest release is from yesterday
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.
My beef with using
unittest.TestCase
is that if you useTestCase
assertions, your code is not very pythonic. When you usepytest
as your test runner, you can use the built in pythonassert
andpytest
will overrideassert
with its own built in assertions.For example:
thing = True
assert thing
vsself.assertTrue(thing)
items = [1, 2, 3, 4]
assert 1 in items
vsself.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.
pytest
also allows you to run tests in parallel if you wantI 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 onself
though - I use plainassert
statements which give nicer output from pytest.Good compromise :)
I don't really like class based unit tests (most of my Python code is already "functionalish")
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
See above for my reasoning on code organisation. I tend to write quite functional code too but I'll have a
TestCase
class with multipletest_*
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).
Well, it seems like there's strong support for pytest!