Initial nose experience

R

Roy Smith

I've been using unittest for many years, but have steadfastly (perhaps
stubbornly) avoided newfangled improvements like nose. I finally
decided to take a serious look at nose. There were a few pain points I
had to work through to get our existing collection of tests to run under
nose. I figured I'd share them for the benefit of others who may be
going through the same process.

First nose won't import executable files, at least not by default.

All of our test files are executable, with a "#!/usr/bin/env python"
line at the top, and a "if __name__ == '__main__'" block, which does
some setup and invokes unittest.main(), at the bottom. Going to the top
of our source tree and typing "nosetests" was an uninspiring experience:
$ nosetests

The fix is either make them non-executable, or do "nosetests --exe".

Next up was the the setup in the "if __name__ == '__main__'" block
wasn't running. The solution here is to move all the setup to
setUpModule(), where it belongs. SetUpModule() is new in Python 2.7 but
it turns out it's trivial to drop that version into older systems
(http://pypi.python.org/pypi/unittest2).

We found a bunch of tests which require some specific setup before they
could run (most blatantly, some selenium tests which depend on X11).
When we were running tests semi-manually, that was not a big deal. With
nose's "find them all and run them" strategy, this fails. The obvious
fix is that every test needs to either set up the right environment, or
be protected with the appropriate @skip decorator so it doesn't run if
the environment isn't good.

Lastly, nose, by default, doesn't say much. When things go wrong and
you have no clue what's happening, --verbose and --debug are your
friends.
 
P

python

Hi Roy,
I've been using unittest for many years, but have steadfastly
(perhaps stubbornly) avoided newfangled improvements like nose.
I finally decided to take a serious look at nose.

Thanks for sharing your nose experience.

What motivated you to migrate from unittest to nose?

After years of using unittest, what would you say are the pros and
cons of nose?

Thank you,
Malcolm
 
R

Roy Smith

Hi Roy,

(perhaps stubbornly) avoided newfangled improvements like nose.
I finally decided to take a serious look at nose.

Thanks for sharing your nose experience.

What motivated you to migrate from unittest to nose?

Mostly I was just looking for a better way to run our existing tests.
We've got a bunch of tests written in standard unittest, but no good way
to start at the top of the tree and run them all with a single command.
 
P

Philipp Hagemeister

Mostly I was just looking for a better way to run our existing tests.
We've got a bunch of tests written in standard unittest, but no good way
to start at the top of the tree and run them all with a single command.

Currently, $ python -m unittest does nothing useful (afaik). Would it
break anything to look in . , ./test, ./tests for any files matching
test_* , and execute those?

- Philipp


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEAREKAAYFAlAD8tEACgkQ9eq1gvr7CFwZkACeI5ItZTw3cogMyF0R88p8aTPM
DbgAn0KAcoqy5/0UqQPPXF5VZWDrcf0L
=r1wp
-----END PGP SIGNATURE-----
 
R

Roy Smith

After years of using unittest, what would you say are the pros and
cons of nose?

BTW, although I'm currently using nose just as a unittest aggregator, I
can see some nice advantages to native nose functionality. The most
obvious is that tests can be plain-old static functions at the top level
of a module.

In unittest, you have to subclass TestCase, then write methods for that
(showing its JUnit/SUnit roots). In 99% of the tests I write, I don't
do anything special in my TestCase subclasses, so that's all just
boilerplate busywork. I like the idea that I can skip that all now.
 
P

Philipp Hagemeister


That's precisely it. Can we improve the discoverability of the discover
option, for example by making it the default action, or including a
message "use discover to find test files automatically" if there are no
arguments?

- Philipp


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEAREKAAYFAlAECw8ACgkQ9eq1gvr7CFzGfgCgsAbj1OfYWMEzBX5acuJcSELV
ejAAoLsCs7nPQ9eQhD9bSqUq43JzwUAx
=062U
-----END PGP SIGNATURE-----
 
P

Philipp Hagemeister

Can we improve the discoverability of the discover
option, for example by making it the default action, or including a
message "use discover to find test files automatically" if there are no
arguments?
Oops, already implemented as of Python 3.2. Sorry, should've checked before.

- Philipp


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEAREKAAYFAlAEDcQACgkQ9eq1gvr7CFzxKACgu/ynpNNx9TxZB5HXjNWenqIX
BCcAnRAOgkAaaPmxRjQGO+WOAaIGV62Q
=wfvU
-----END PGP SIGNATURE-----
 
R

Roy Smith

Does nose run all of its collected tests in a single process?

I've got a test which monkey-patches an imported module. Will all of the other tests collected in the same run of nosetests see the patch?
 
R

Roy Smith

Roy Smith said:
Lastly, nose, by default, doesn't say much. When things go wrong and
you have no clue what's happening, --verbose and --debug are your
friends.

I found another example of nose not saying much, and this one is kind of
annoying. Unittest has much richer assertions, and better reporting
when they fail. If a nose assertion fails, you just get:

------------------------------------------------------------------
Traceback (most recent call last):
File
"/home/roy/production/python/lib/python2.6/site-packages/nose/case.py",
line 197, in runTest
self.test(*self.arg)
File "/home/roy/songza/pyza/djapi/test_middleware.py", line 48, in
test_update_non_json_cookie
assert user_list == [9990]
AssertionError
------------------------------------------------------------------

Under unittest, it would have printed the value of user_list. Yeah, I
know, I can stick a "print user_list" statement into the test, and the
output will get suppressed if the test fails. But that means when a
test fails, I need to go back and edit the test code, which is a pain.

On the other hand, there *is* an incremental efficiency gain of writing:

assert x == y

instead of

assertEqual(x, y)

many times. So maybe overall it's a wash.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top