in Python by
Brief Introduction to nose in Python?

▼ Show 1 Answer

0 votes
nose is another unit testing module in Python.

nose makes testing easier by extending unittest framework.

nose can be used to run automated tests and also the tests written in other frameworks like unittest.

nose is available as a third party library and can be installed using pip command as shown below.

pip install nose

Successful installation of nose can be verified by running the below command.

nosetests -V

Writing your first nose test

Consider the function test_sample_nosetest defined in This function is a sample nose test.

def test_sample_nosetest():

    assert 'HELLO' == 'hello'.upper()

From above example, you can observe that, nose allows writing a test case outside a test class.

Even writing any test class does not need to derived from a parent class in nose.

nose uses python's assert method for checking the truthiness of an expression.

Running a nose test

Tests in can be run using the below command.

python -m nose




Ran 1 test in 0.000s


For verbose output use -v option at the end.

python -m nose -v


sample_nose_test.test_sample_nosetest ... ok


Ran 1 test in 0.001s


You can also run the test module using nosetests command as shown below.

nosetests -v

The above command produces same output as verbose one.

Creating a Test Project

For creating a test module, let's create a new project named Project2 similar to Project1.

Create sub folders proj and test inside Project2.

Create two empty files named inside proj and test folders respectively.

Create a copy of Project1/proj/ and place it in Project2/proj/

Add the line all = ['sample_module'] to Project2/proj/ file.

Creating a Test Module

Create an empty test module named inside test folder.

Add the following code to Project2/test/

from proj.sample_module import add2num

class Testadd2num:

    def test_sum_2pos_num(self):

      assert add2num(6, 7)==13

    def test_sum_1pos_and_1neg_num(self):

      assert add2num(-10, 9)==-1

Add the line all = ['test_module1'] to Project2/test/ file.

Running a Test Module

Now you can run all the tests in test module with the below command.

nosetests test.test_module1 -v

Verbose Output

test.test_module1.Testadd2num.test_sum_1pos_and_1neg_num ... ok

test.test_module1.Testadd2num.test_sum_2pos_num ... ok


Ran 2 tests in 0.002s


Syntax for running a Test class is slightly different from unittest as shown below. Here you use : instead of ..

nosetests test.test_module1:Testadd2num -v

And a single test can be run as shown below.

nosetests test.test_module1:Testadd2num.test_sum_2pos_num -v
Learn More with Madanswer

Related questions

0 votes
asked Jul 2, 2020 in Python by GeorgeBell