nose module

Date: January 15th 2016
Last updated: January 15th 2016

nose extends the unittest module. nose can run existing tests defined with unittest and it can find them by auto-discovery. Basic usage found here.

# install
pip install nosetests
easy_install nosetests

Examples start with the unittest module already created. Then I look at making test functions using node.

File structure:

Project
├── test
│   ├── __init__.py      # make test a module
│   └── test_vowels.py   # unittest test functions
└── vowels
    ├── __init__.py      # make vowels a module
    └── vowels.py        # used in doctest example

Version of nose used here: -V

nosetests -V
# Output
# nosetests version 1.3.7

Run:

# run from Project directory
# run the existing unittest module with auto discovery
nosetests

Make the output more verbose: -v

# run from Project directory
# increase verbosity by adding v
nosetests -v #names the tests that were run
#nosetests -vv

Specify the location of test files:

# run from root
nosetests ~/python/scripts/project/ 
# allows test files in different locations
nosetests ~/path/of-test/one/ ~/path/of-test/two/

Add detail to the output of failed tests: -d

# run from Project directory
nosetests -d

Stop after the first error: -x

# run from Project directory
nosetests -x

Run the tests that failed in the last run: --failed

# run from Project directory
nosetests --failed

Run multiple commands at once:

# root 
cd
nosetests --failed -x -d -v ~/python/scripts/project

Run doctests: --with-doctest

# run from Project directory
nosetests --with-doctest

Drop into pdb debugger if a test fails: --pdb

# run from Project directory
nosetests --pdb             # go to pdb on failures and errors
nosetests --pdb-failures    # go to pdb on failures
nosetests --pdb-errors      # go to pdb on errors

Drop into pudb: --pudb

pip install nose-pudb
# run from Project directory
nosetests --pudb

Making tests with nose...
Assuming this file structure:

Project
├── test
│   ├── __init__.py             # make test a module
│   ├── test_vowels.py          # unittest example
│   └── test_vowels_nose.py     # nose example (see below)
└── vowels
    ├── __init__.py             # make vowels a module
    └── vowels.py               # used in doctest example

Create simple nose test function:

# test_vowels_nose.py
from vowels import vowels

def test_cv_onestring():
        assert vowels.count_vowels('this is a test') == 4

Run the nose test function:

# run from Project directory
nosetests -v test/test_vowels_nose.py

Output:
nosetests output

Testing a class with nose:

Modified vowels script:

class CountVowels:
    """
    A class for evaluating nose testing functions.
    """
    def __init__(self, sentence):
        # instantiating a class requires a string
        self.sentence =  sentence

    def count_vowels(self):
        """Returns the number of vowels in a string."""
        self.count = 0  # Set counter to zero
        vowels = ['a', 'e', 'i', 'o', 'u']  

        for i in self.sentence: # loop through letters
            if i in vowels # match letters to vowels
                self.count += 1 # increase counter

        return self.count

File structure:

Project
├── test
│   ├── __init__.py
│   ├── test_vowels_nose.py     # updated test script
│   └── test_vowels.py
└── vowels
    ├── __init__.py
    ├── vowelsclass.py          # new vowels script
    └── vowels.py

Modified nose test function:

# import the module called "vowelsclass"
from vowels import vowelsclass 

def test_cv_onestring():
        # instantiate the class called CountVowels
        vc = vowelsclass.CountVowels('this is a test')
        # test the function called count_vowels()
        assert vc.count_vowels() == 4

results matching ""

    No results matching ""