Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to run all unit test?

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.

like image 708
caren vanderlee Avatar asked Jul 22 '15 07:07

caren vanderlee


People also ask

How do I run all unit tests in Java?

In order to run all of the tests in a directory including tests in nested directories you will need to use something like googlecode. junittool box. Right clicking on this class and selecting Run As JUnit test runs all of the tests in the specified directory including all tests in nested subfolders.

Should I unit test all methods?

The answer to the more general question is yes, you should unit test everything you can. Doing so creates a legacy for later so changes down the road can be done with peace of mind. It ensures that your code works as expected.

How do you run all test classes at once?

Run All Tests From Developer Console Go to Setup | Developer Console. From the developer console Click Test | Run All. All the tests will run and a breakdown of the code coverage in the bottom right of the screen with the overall Code coverage and per-class code coverage is shown.

Should unit tests only test one thing?

This guideline is much more aggressive and recommended if you work in a test driven manner rather than write the tests after the code has been written. The main goal here is better code coverage or test coverage.


2 Answers

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

like image 78
mcguip Avatar answered Oct 05 '22 23:10

mcguip


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
like image 36
urban Avatar answered Oct 06 '22 00:10

urban