in Python by
How to Creating a Test Project in Python?

1 Answer

0 votes
by
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
...