Search code examples
pythonpython-unittest

Class is in another directory - returns 0 tests


I want to create different classes for my tests. But when I run my test unit, it does not run the test in my test class. The setUp function is never reached and zero tests are made.

python -m unittest discover -s src -p "tests.py"

Ran 0 tests in 0.000s

OK

This is the main script tests.py:

import unittest

def main():
    unittest.main()

if __name__ == '__main__':
    main()

The test class is in another file:

import unittest

from Tools import Tools

class TestSomething(unittest.TestCase):

    def setUp(self):
        self.logger = Tools.get_logger(__file__)
        self.logger.info("here")

    def test_something(self):
        ....

I tried different ways to launch the script but I always have the same problem.

Can't we separate the test units in classes?

I add a screenshot that shows the name of the files present in my src folder:

enter image description here


Solution

  • Your question, on my opinion, shows that the names of your test files don't match with the option -p (--pattern) of the discover sub-command (see test-discover documentation for details).

    Filesystem structure of my example code

    I have created the following structure on my system to write example code (3 files in the folder test_project_root/src):

    test_project_root
      |
      src
       |- Tools.py
       |- classa.py
       |- classb.py
    

    classa.py
    The file classa.py is equal to the file showed in the third snippet of code of your question:

    import unittest
    
    from Tools import Tools
    
    class TestSomething(unittest.TestCase):
    
        def setUp(self):
            self.logger = Tools.get_logger(__file__)
            self.logger.info("here")
    
        def test_something(self):
            print(f"{__file__} test_something()")
    

    classb.py
    The content of the file classb.py is invented by me, but it is very similar to classa.py:

    import unittest
    
    from Tools import Tools
    
    class TestSomething2(unittest.TestCase):
    
        def setUp(self):
            self.logger = Tools.get_logger(__file__)
            self.logger.info("here")
    
        def test_something2(self):
            print(f"{__file__} test_something2()")
    

    Tools.py
    For completeness I have created also the file Tools.py as following:

    import logging
    
    class Tools:
        
        def get_logger(self):
            logging.basicConfig(level=logging.INFO)
            return logging
            
        def info(self, message):
            print(logging.info(message))
    

    Execution of the test code

    With the previous files must be executed 2 test methods:

    • test_something() (from class TestSomething)
    • test_something2() (from class TestSomething2)

    To execute the test it is necessary changing directory to test_project_root and execute the discovery:

    > cd /path/to/test_project_root
    
    > python -m unittest discover -s src -p "class*"
    

    Please note the pattern option: -p "class*" (in particular the presence of the char *).

    The output

    The output of the previous command is:

    INFO:root:here
    /path/to/test_project_root/src/classa.py test_something()
    .INFO:root:here
    /path/to/test_project_root/src/classb.py test_something2()
    .
    ----------------------------------------------------------------------
    Ran 2 tests in 0.000s
    
    OK
    

    The output shows that:

    1. the 2 test methods are executed (see the message Ran 2 tests in 0.000s)
    2. the presence of 2 messages INFO:root:here shows that also the setUp() methods of TestSomething and TestSomething2 have been executed.