10

I have two module with two different classes and their corresponding test classes.

 foo.py
 ------
 class foo(object):
     def fooMethod(self):
         // smthg

 bar.py
 ------
 class bar(object):
     def barMethod(self):
         // smthg

 fooTest.py
 ------
 class fooTest(unittest.TestCase):
     def fooMethodTest(self):
         // smthg

 barTest.py
 ------
 class barTest(unittest.TestCase):
     def barMethodTest(self):
         // smthg

In any, test and source module, file, I erased the if __name__ == "__main__": because of increasing coherency and obeying object-oriented ideology.

Like in Java unit test, I'm looking for creating a module to run all unittest. For example,

 runAllTest.py
 -------------
 class runAllTest(unittest.TestCase):
    ?????

 if __name__ == "__main__":
    ?????

I looked for search engine but didn't find any tutorial or example. Is it possible to do so? Why? or How?

Note: I'm using eclipse and pydev distribution on windows machine.

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
caren vanderlee
  • 195
  • 1
  • 4
  • 9
  • 1
    I would rename the test files to 'test_foo.py' format, and run `nosetests` in the test directory. `nose` will autodiscover all the test files and test classes. – suzanshakya Jul 22 '15 at 08:02

6 Answers6

13

When running unit tests based on the built-in python unittest module, at the root level of your project run

python -m unittest discover <module_name>

For the specific example above, it suffices to run

python -m unittest discover .

https://docs.python.org/2/library/unittest.html

mcguip
  • 5,947
  • 5
  • 25
  • 32
6

You could create a TestSuite and run all your tests in it's if __name__ == '__main__' block:

import unittest   

def create_suite():
    test_suite = unittest.TestSuite()
    test_suite.addTest(fooTest())
    test_suite.addTest(barTest())
    return test_suite

if __name__ == '__main__':
   suite = create_suite()

   runner=unittest.TextTestRunner()
   runner.run(suite)

If you do not want to create the test cases manually look at this quesiton/answer, which basically creates the test cases dynamically, or use some of the features of the unittest module like test discovery feature and command line options ..

Community
  • 1
  • 1
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
3

I think what you are looking for is the TestLoader. With this you can load specific tests or modules or load everything under a given directory. Also, this post has some useful examples using a TestSuite instance.

EDIT: The code I usually have in my test.py:

if not popts.tests:
    suite = unittest.TestLoader().discover(os.path.dirname(__file__)+'/tests')
    #print(suite._tests)

    # Print outline
    lg.info(' * Going for Interactive net tests = '+str(not tvars.NOINTERACTIVE))

    # Run
    unittest.TextTestRunner(verbosity=popts.verbosity).run(suite)
else:
    lg.info(' * Running specific tests')

    suite = unittest.TestSuite()

    # Load standard tests
    for t in popts.tests:
        test = unittest.TestLoader().loadTestsFromName("tests."+t)
        suite.addTest(test)

    # Run
    unittest.TextTestRunner(verbosity=popts.verbosity).run(suite)

Does two things:

  1. If -t flag (tests) is not present, find and load all tests in directory
  2. Else, load the requested tests one-by-one
Community
  • 1
  • 1
urban
  • 5,392
  • 3
  • 19
  • 45
2

I think you could just run the following command under the folder where your tests files are located:

python -m unittest

as mentioned here in the doc that "when executed without arguments Test Discovery is started"

Fuyang Liu
  • 1,496
  • 13
  • 26
1

With PyDev right click on a folder in Eclipse and choose "Run as-> Python unit-test". This will run all tests in that folder (the names of the test files and methods have to start with "test_".)

Stefan
  • 10,010
  • 7
  • 61
  • 117
0

You are looking for nosetests.

You might need to rename your files; I'm not sure about the pattern nose uses to find the test files but, personally, I use *_test.py. It is possible to specify a custom pattern which your project uses for test filenames but I remember being unable to make it work so I ended up renaming my tests instead.

You also need to follow PEP 328 conventions to work with nose. I don't use IDEs with Python but your IDE may already follow it---just read the PEP and check.

With a PEP 328 directory/package structure, you can run individual tests as

nosetests path.to.class_test

Note that instead of the usual directory separators (/ or \), I used dots.

To run all tests, simply invoke nosetests at the root of your project.

skytreader
  • 11,467
  • 7
  • 43
  • 61