views:

4062

answers:

8

Hello,

So far I've been using the built-in unittest module for unit-testing Python code. However, for simple cases it seems like an overkill. Being a derivative of xUnit, it appears a bit heavy for the dynamic nature of Python, where I would expect to write less to achieve the same effects. On the other hand, it is built-in, makes you write your tests in an organized way, and tested by time.

The major alternatives I've seen online are:

Which of the frameworks do you prefer, and why ?

A: 

There's always doctest if you want to keep your unit tests close to the code.

HTH

+5  A: 

Regarding doctest: I don't consider it a unit-testing framework per-se. I definitely wouldn't use it to write a large suite of tests for a sizable application. doctest is more suitable for making sure that the examples you provide in the documentation work. It has its place for this need, but it isn't a competitor for unittest, py.test and other frameworks.

Eli Bendersky
This is part of your question. It's not an answer.
S.Lott
+18  A: 

nose isn't really a unit testing framework. It's a test runner and a great one at that. It can run tests created using pyUnit, py.test or doctest.

My preference for unit testing framework is pyUnit. It's similar to other xUnit frameworks and is easy to relate to for people without python background. There is also pretty good support for it in Eclipse/PyDev

On py.test, I find multiple levels of setup/teardowns very confusing. I also find that it leads to highly unstructured and hard to read unit tests.

doctest is OK for simple things, but I find that it's very limiting and doesn't really scale for complex and highly interactive code.

Kozyarchuk
Apparently the build-in unittest module is sometimes referred to as pyUnit: http://docs.python.org/library/unittest.htmlAre you referring to the build-in unittest module, or some other pyUnit?
Dave Cameron
@Dave Cameron: I think he must be talking about the `unittest` module: google doesn't turn up any results for PyUnit that aren't actually the `unittest` module.
intuited
funny enough, in 2010 the unittest2 and cpython-2.7's unittest have introduced the "multilevel" setup/teardowns you find confusing. (Being the original py.test author) I am now rather recommending more flexible ways to manage test resources and fixtures, aka "funcargs" :)
hpk42
+10  A: 

I just use the standard unittest. Not sure how you'd write tests as effectively with another style -- perhaps each test in a function, but then how would you handle setup / teardown?

John Millikin
+3  A: 

The unittest.TestCase is a class. Feel free to subclass it with your own add-on features that allow you to "write less to achieve the same effects".

S.Lott
+2  A: 

One of the nicest features of nose is its plugin system: for example the coverage plugin shows you how much of your code is covered by unittests. After writing lots of unittests it is often shocking to see how much of your code isn't covered ....

RSabet
+3  A: 

nose is not really a full unit testing framework in it's own right; however, it will run any function in a module whose name starts with "test" and it will fail if the function raises an AssertionError which makes writing simple tests really simple.

It also has the concept of test generators which are a really nice implementation of data driven tests.

... and since nose will run unittest tests you can bail out to that when you need to write complicated setUp/tearDown tests.

So: unittest + nose (+ doctest where appropriate) really is a pretty killer combination.

Aaron Maenpaa
+2  A: 

I agree that one nicest features of nose is its plugin system. For example, I started learning Python when the Google App Engine launched and there was a Nose plug-in to support GAE almost immediately. So Nose with its plugins helped me to start doing test-driven development with a new platform like GAE from the start. The coverage plugin was there when I was ready for it as well.

Chris