Question

I want to write my library in TDD methodology, but I have no idea how to design directory structure (or how to use unittest). Now I create a directory tree such as:

myproject (directory)
    - tests (directory)
    - src (directory)
    - test.py (file running tests)

Each class from src has its own unittest class. Each directory has its own __init__.py file. I want to run tests only from test.py file and thanks to this each test can from src.modulename import classname and than runs test unittest.main() function. Unfortunately it doesn't work (runs zero tests).

Is this good approach? What are my mistakes?

Was it helpful?

Solution

The code in file test.py should look like:

from tests import *
import unittest

if __name__ == '__main__':
    testsuite = unittest.TestLoader().discover('.')
    unittest.TextTestRunner(verbosity=1).run(testsuite)

This code copies all tests from tests directory, because it copies entire package. The main method runs all test methods included in tests package's classes. Each test file name must start with test.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top