Running unit tests in Python

Install nose:
sudo easy_install nose
or:
sudo pip install nose

By default nose finds all directories and files that match the following regular expression:
(?:^|[b_./-])[Tt]est [NOSE_TESTMATCH]

I usually create a file test_something.py and place my unit tests there as regular functions, e.g.:
def test_function_1():
...

To run tests in specific file do:
nosetests test_file.py

To run all tests in files satisfying regex above:
nosetests

By default, nose will capture all output sent to stdout. To be able to see print statements inside test functions use –nocapture option.

I usually run all tests by:
nosetests --nocapture -v

or:
nosetests --nocapture -v -w path_to_folder

Advertisements
This entry was posted in Productivity, Programming, Python, UNIX/Linux. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s