Skip to content

Instantly share code, notes, and snippets.

@mpilosov
Last active January 20, 2018 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpilosov/49ac06f16304133a2eb438afa85a9266 to your computer and use it in GitHub Desktop.
Save mpilosov/49ac06f16304133a2eb438afa85a9266 to your computer and use it in GitHub Desktop.

We'll be using nose for testing purposes.

The basic premise is simple. Write a file that imports solely the modules you want to test.

Writing your first nosetest

Write one test for each type-support that you want to be able to check. From this guide, we have the following example of ensuring that a user-defined function multiply operates the way it is intended to:

from unnecessary_math import multiply
def test_numbers_3_4():
    assert multiply(3,4) == 12 
 
def test_strings_a_3():
    assert multiply('a',3) == 'aaa' 

As you can see, the test makes sure that if a string is passed, the interpretation is to repeat the string, whereas integers get multiplied. While python is not a strongly-typed language, it is highly encouraged that you write tests for each type you wish your function to support. This way, when you make changes, the test ensures the consistent functionality of your code.

Running your first nosetest

The syntax (assuming you have the nosetests library installed -- see here for instructions. nosetests test_um_nose.py will run the specific file. You can pass the -v ( --verbose ) flag as well for more information as the tests run.

... and that's about all you need to know to get started! If you want to set up some environmental variables to test functions on, see
http://pythontesting.net/framework/nose/nose-introduction/

which is an excellent walk-through.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment