insomnihack

When sleep eludes, the keyboard beckons

Nose – Fix nosetests finding 0 tests

November 26, 2009 Dale 2 Comments

I ran into an odd problem with nosetests not finding my unit test files after a git pull. After a little googling, I found someone who’d had similar issues with the permissions of their test files getting changed. For whatever reason (perhaps pushing from Cygwin on a Windows box?), my git pull had set the execution bit on my test files.

Typically, nosetests ignores executable files unless you pass it the --exe command line option. For now, I ran a chmod -x path/to/test.py to make the file non-executable again. I’m going to have to keep an eye on this to see if it reoccurs.

Programming Python, unit tests

Python Unit Test Location and Nose

November 21, 2009 Dale 2 Comments

Honestly? I’ve spent a ridiculous amount of time trying to figure out where to put my python unit test files. Sure, they worked fine located in the same directory as my source. However, the Felix Unger in me recoiled at the thought of my test files intermingling unsupervised with my main package source code. I’m pretty sure that’s how the Neanderthal went out.

After playing with __name__, relative imports, a half dozen folder configurations, IPython config tweaks, and in general just pulling my hair out at the roots as I chased the rabbit deeper into its hole, I decided to step back and revisit a tool I’d been meaning to check out for a long time: nose.

Sweet Judas Priest, the hours I could have saved! If you’re unfamiliar with nose, it extends the test loading and running features of unittest, making it easier to write, find and run tests. Its pure, buttery, test-running goodness.

What follows may be a bit basic for most experienced Pythonistas. For the rest of you Python noobs out there like me, here’s how I’m setting my packages and tests up now.

[text]
/MyPackage
/mypackage
__init__.py
spam.py
eggs.py
/tests
__init__.py
spam_tests.py
eggs_tests.py
[/text]

The source for my package goes under /mypackage. The __init__.py there contains the usual kind of stuff.

[python]
__all__ = [‘spam’, ‘eggs’]
[/python]

The unit tests for /mypackage go in /mypackage/tests. The __init__.py file in my /tests folder is empty. Since I’ve pretty much settled on nose, I’m not performing any additional test running tricks. Were I to add imports for my tests classes here similar to what I did in /mypackage/__init__.py, nose would run all my unit tests twice.

In my unit test files, I use relative imports to get at my source files. Relative paths only work within packages, so remember that the __init__.py files are required to make Python treat the directories as containing packages. For example, spam_tests.py would contain the following.

[python]
import unittest
from ..spam import Spam

class SpamTests(unittest.TestCase):
"""Unit tests for the Spam class."""

def testDefaultInitialization(self):
"""Verify the default values of a new Spam instance."""
s = Spam()
self.assertEqual(s.goteggs, False, ‘Spam already had eggs!’)

# etc…
[/python]

I don’t bother with adding if __name__ == '__main__' to the bottom of my test scripts in order to call unittest.main(). Setting the __name__ to '__main__' breaks relative imports.

Once I’ve got this basic layout configured, I’m ready to write my tests, implement the functionality in my classes, and verify everything works by running nosetests from my /tests directory.

Programming Python, unit tests

Virtualenv useful on Mac, not so much on PC

November 8, 2009 Dale Leave a Comment

Well, after wrestling with virtualenv on both the PC and Mac, I think I’m going to stick with it for my Mac but bail on it for PC python work.

Mac vs PC on Virtualenv

I really like it on the Mac because now that I’ve moved on to Snow Leopard and Python 2.6, I’m not using a nice isolated environment like the one EPD provided me with. Even though I’m using MacPorts Python 2.6.x, packages get installed into the same location as Snow Leopard’s Python 2.6.1. Since python packages don’t come with an uninstall option, and because I haven’t a clue how to restore Snow Leopard’s Python if I end up adding too much 3rd party gorp, virtualenv seems the prudent course for development on OSX. The only 3rd party libraries I have installed in the main python site-packages folder are setuptools, virtualenv, pip, ipython, and yolk. Everything else (so far) is getting put into a virtual environment. Also, now that I’ve figured out how to get virtualenv and ipython to play nice, isolated development is running smooth as silk.

On Windows, I’ve run into issues related to path names with spaces, and other minor problems. Since I control where python gets installed on Windows, and since python is fairly self contained, its easy to just blow out my install and start over if I add so much cruft that python blows up on me. There’s not much incentive at this time to get virtualenv configuration debugged on my PC.

Programming mac, packaging, Python, virtualenv

« Previous Page

About Me

Hi, my name's Dale, and this is my tech blog. Read More…

Search

Tags

blogging c# database Django documentation Email furniture git hacks htpc IPython Linux mac nas packaging PyCharm Python Roleplaying tools twitter unit tests virtualenv Visual Studio vmware Windows

Copyright © 2025 · Equilibre Theme on Genesis Framework · WordPress · Log in