TDD with nose or py.test

Roy Smith roy at panix.com
Sat Dec 5 18:00:21 EST 2009


In article 
<2519ffb0-fd49-4340-857b-62fca5c71421 at 33g2000vbe.googlegroups.com>,
 Lacrima <lacrima.maxim at gmail.com> wrote:

> Hello!
> 
> I am learning TDD with Python and there is not much information about
> this topic. Python is shipped with unittest module. That is fine, but
> I also discovered other libraries: nose and py.test. They promise to
> make life yet easier for a developer. But I still can't figure out,
> which combination I should use them in.
> Nose seems simpler than py.test, but py.test offers more features. And
> both py.test and nose say that they do not replace unittest module,
> but extend it.
> So as I understand my choice should be nose + unittest, or py.test +
> unittest.
> And now I'd like to hear about best practices in TDD in Python. Maybe
> not best practices, but basic approach.
> So which tests should I write with unittest, which with nose or
> py.test? How should I combine them?

You're obsessing over details.  Pick one and go with it.  I've been using 
unittest for years and I'm mostly happy with it.  I've played with py.test 
a bit, and while I like some features, I keep finding myself wandering back 
to unittest.  Maybe it's just what I know so I'm comfortable with it.

I've glanced at nose, but every time I look at it, I ask myself, "Do I want 
to learn another testing tool, or do I just want to get on with testing the 
code I'm writing now?", which inevitably brings me back to just sticking 
with unittest.

I am very much a fan of TDD.  Whenever I write a new class, here's the 
first test I write:

class TestFoo(unitest.TestCase):
   def test_construct(self):
      foo = Foo()

If that passes, it means I've managed to create a new class, register it 
with my build system (and, most likely, my source control system).  In 
keeping with the "write the very smallest amount of code you need to make 
your test pass", my class will usually look like this at the point the 
above test first passes:

class Foo:
   pass

After that, it's all about adding features :-)



More information about the Python-list mailing list