0 votes
in Python by
Brief Introduction to nose in Python?

1 Answer

0 votes
by
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 sample_nose_test.py. 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 sample_nose_test.py can be run using the below command.

python -m nose sample_nose_test.py

Output

.

----------------------

Ran 1 test in 0.000s

OK

For verbose output use -v option at the end.

python -m nose sample_nose_test.py -v

Output

sample_nose_test.test_sample_nosetest ... ok

-------------------------

Ran 1 test in 0.001s

OK

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

nosetests  sample_nose_test.py -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 __init__.py inside proj and test folders respectively.

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

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

Creating a Test Module

Create an empty test module named test_module1.py inside test folder.

Add the following code to Project2/test/test_module1.py

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/__init__.py file.

Running a Test Module

Now you can run all the tests in test module test_module1.py 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

OK

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

Related questions

0 votes
asked Jul 2, 2020 in Python by GeorgeBell
0 votes
asked Jul 2, 2020 in Python by GeorgeBell
...