0

Project Structure

components/
    A.py
    B.py

run_test.py

A.py and B.py each has some functions with doctest test cases.

How can I run all tests in A.py and B.py by running only run_test.py?

Any other approach to achieve "Run all tests in A.py and B.py` will be appreciated too.

Inyoung Kim 김인영
  • 1,434
  • 1
  • 17
  • 38

1 Answers1

1

Should've read the document more in detail.

  1. Used this answer for importing with path.
  2. Using example from doctest document run test
import doctest
import importlib.util

# from link

def import_module(name, path):
    spec = importlib.util.spec_from_file_location(name, path)
    foo = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(foo)
    return foo

if __name__ == "__main__":
    test_modules = [
        ("components.A", "components/A.py"),
        ("components.B", "components/B.py")
    ]
    
    for name, path in test_modules:
        doctest.testmod(import_module(name, path)) # from document
    
Inyoung Kim 김인영
  • 1,434
  • 1
  • 17
  • 38