insomnihack

When sleep eludes, the keyboard beckons

IPython Color Console Output

December 16, 2009 Dale 1 Comment

IPython does a decent job of providing console output in color on all the platforms I use it on. If you’re like me and you’ve wanted to use its coloring functionality for your own terminal output, here’s a snippet that should get you started.

[python]
from IPython import ColorANSI
from IPython.genutils import Term
tc = ColorANSI.TermColors()
red = tc.Red + "Red" + tc.Normal
print >> Term.cout, red
blue = tc.Blue + "Blue" + tc.Normal
print >> Term.cout, blue
[/python]

Assuming you have color output enabled (if you’re on Windows, you’ll need PyReadline), you should be able to %paste the plain text from the snippet above into IPython and see the output below.

Red
Blue

Programming IPython, Python

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

« Previous Page
Next 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