Recursive unittest detect

I have a package with the tests directory in which I store my unit tests. My package looks like this:

.
β”œβ”€β”€ LICENSE
β”œβ”€β”€ models
β”‚   └── __init__.py
β”œβ”€β”€ README.md
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ tc.py
β”œβ”€β”€ tests
β”‚   β”œβ”€β”€ db
β”‚   β”‚   └── test_employee.py
β”‚   └── test_tc.py
└── todo.txt

In my package directory I want to find both tests/test_tc.pyand tests/db/test_employee.py. I would prefer not to install a third-party library ( noseor etc.), or you need to manually create it TestSuiteto run this.

Of course, is there a way to tell him unittest discovernot to stop looking as soon as he finds a test? python -m unittest discover -s testswill find tests/test_tc.pyand python -m unittest discover -s tests/dbfind tests/db/test_employee.py. Is there no way to find both?

+11
source share
2 answers

, , , , python -m unittest discover. , __init__.py , .

.
β”œβ”€β”€ LICENSE
β”œβ”€β”€ models
β”‚   └── __init__.py
β”œβ”€β”€ README.md
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ tc.py
β”œβ”€β”€ tests
β”‚   β”œβ”€β”€ db
β”‚   β”‚   β”œβ”€β”€ __init__.py       # NEW
β”‚   β”‚   └── test_employee.py
β”‚   β”œβ”€β”€ __init__.py           # NEW
β”‚   └── test_tc.py
└── todo.txt

__init__.py, python -m unittest discover test_*.

+28

__init__.py , load_tests, .

( __init__.py)          load_tests.          , , , .

load_tests , ,         load_tests .

, , :

import os
import pkgutil
import inspect
import unittest

# Add *all* subdirectories to this module path
__path__ = [x[0] for x in os.walk(os.path.dirname(__file__))]

def load_tests(loader, suite, pattern):
    for imp, modname, _ in pkgutil.walk_packages(__path__):
        mod = imp.find_module(modname).load_module(modname)
        for memname, memobj in inspect.getmembers(mod):
            if inspect.isclass(memobj):
                if issubclass(memobj, unittest.TestCase):
                    print("Found TestCase: {}".format(memobj))
                    for test in loader.loadTestsFromTestCase(memobj):
                        print("  Found Test: {}".format(test))
                        suite.addTest(test)

    print("=" * 70)
    return suite

, .

(Docs).

pkgutil, , .

, , , , , unittest.TestCase. , .

,

python -m unittest discover -p tests

-p. , , , - :

Found TestCase: <class 'test_tc.TestCase'>
  Found Test: testBar (test_tc.TestCase)
  Found Test: testFoo (test_tc.TestCase)
Found TestCase: <class 'test_employee.TestCase'>
  Found Test: testBar (test_employee.TestCase)
  Found Test: testFoo (test_employee.TestCase)
======================================================================
....
----------------------------------------------------------------------
Ran 4 tests in 0.001s

OK

, - : testFoo testBar .

: , , :

def load_tests(loader, suite, pattern):
    for imp, modname, _ in pkgutil.walk_packages(__path__):
        mod = imp.find_module(modname).load_module(modname)
        for test in loader.loadTestsFromModule(mod):
            print("Found Tests: {}".format(test._tests))
            suite.addTests(test)

loader.loadTestsFromModule() loader.loadTestsFromTestCase(), . - tests , , , .

-, testuite testuite suite:

python -m unittest discover -p tests
Found Tests: [<test_tc.TestCase testMethod=testBar>, <test_tc.TestCase testMethod=testFoo>]
Found Tests: [<test_employee.TestCase testMethod=testBar>, <test_employee.TestCase testMethod=testFoo>]
======================================================================
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK

- 4 , , .

+4

Source: https://habr.com/ru/post/1688299/


All Articles