1

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?

2
  • Have you looked at doc testing? docs.python.org/2/library/doctest.html I always liked its simplicity Commented Dec 16, 2013 at 21:34
  • Yes I have. I have plenty of tests written in unittest and if it is possible I would rather use them instead of rewrite on another framework. Commented Dec 16, 2013 at 21:37

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.