Unittest cannot detect / run tests

There are some related questions , but they do not apply.

This is my directory tree:

Β» tree abc_backend
abc_backend/
β”œβ”€β”€ backend_main.py
β”œβ”€β”€ FundDatabase.db
β”œβ”€β”€ healthcheck.py
β”œβ”€β”€ __init__.py
β”œβ”€β”€ init.py
β”œβ”€β”€ portfolio.py
β”œβ”€β”€ private.py
β”œβ”€β”€ __pycache__
β”œβ”€β”€ questionnaire.py
β”œβ”€β”€ recurring.py
β”œβ”€β”€ registration.py
β”œβ”€β”€ tests
β”‚   β”œβ”€β”€ config.py
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ __pycache__
β”‚   β”œβ”€β”€ test_backend.py
β”‚   β”œβ”€β”€ test_healthcheck.py
β”‚   └── test_private.py
β”œβ”€β”€ trading.py
β”œβ”€β”€ Users.db
β”œβ”€β”€ VERSION
└── visualisation.py

unittest can't find anything:

top Β» python -m unittest abc_backend

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

Not even inside abc_backend:

abc_backend Β» python -m unittest tests

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

What I already checked:

  • my test methods are correctly named ( test_whatever)
  • my test extensions are expanding unittest.TestCase
  • directories abc_backendand abc_backend/testshave (empty)__init__.py
  • all test modules are imported (see below)
  • unittest discover finds tests, but has problems with relative imports (see below).
  • nose able to detect and run tests, no problem

I would like to understand:

  • discover unittest, ? unittest discover? ( , unittest ). :

python -m unittest python -m unittest

  • ( discover), ?

Β» python
Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import abc_backend.tests
>>> import abc_backend.tests.test_private
>>> import abc_backend.tests.test_healthcheck
>>> import abc_backend.tests.test_backend

unittest

:

top Β» python -m unittest discover abc_backend
======================================================================
ERROR: tests.test_private (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3.4/unittest/case.py", line 58, in testPartExecutor
    yield
  File "/usr/lib/python3.4/unittest/case.py", line 577, in run
    testMethod()
  File "/usr/lib/python3.4/unittest/loader.py", line 32, in testFailure
    raise exception
ImportError: Failed to import test module: tests.test_private
Traceback (most recent call last):
  File "/usr/lib/python3.4/unittest/loader.py", line 312, in _find_tests
    module = self._get_module_from_name(name)
  File "/usr/lib/python3.4/unittest/loader.py", line 290, in _get_module_from_name
    __import__(name)
  File "/foo/bar/abc_backend/tests/test_private.py", line 6, in <module>
    from .. import init
ValueError: attempted relative import beyond top-level package

abc_backend:

abc_backend Β» python -m unittest discover tests

======================================================================
ERROR: test_private (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3.4/unittest/case.py", line 58, in testPartExecutor
    yield
  File "/usr/lib/python3.4/unittest/case.py", line 577, in run
    testMethod()
  File "/usr/lib/python3.4/unittest/loader.py", line 32, in testFailure
    raise exception
ImportError: Failed to import test module: test_private
Traceback (most recent call last):
  File "/usr/lib/python3.4/unittest/loader.py", line 312, in _find_tests
    module = self._get_module_from_name(name)
  File "/usr/lib/python3.4/unittest/loader.py", line 290, in _get_module_from_name
    __import__(name)
  File "/foo/bar/abc_backend/tests/test_private.py", line 6, in <module>
    from .. import init
SystemError: Parent module '' not loaded, cannot perform relative import
+4
2

CPython 3.5, 3.4, 3.5.

, , , - , abc_backend.

-,

topΒ» python3 -m unittest discover abc_backend

, abc_backend . , /home/user/top/abc_backend sys.path /home/user/top. ,

topΒ» python3 -m unittest discover abc_backend -t .

in-abc_backend.

abc_backendΒ» python3 -m unittest discover tests

abc_backend , /home/user/top/abc_backend/tests dir abc_backend.

abc_backendΒ» python3 -m unittest discover tests -t ../

/home/user/top dir () sys.path.

-t ( --top-level-directory) ( .). , sys.path , , , .

-m unittest -m unittest discover

topΒ» python3 -m unittest abc_backend

unittest/__main__.py. main(module=None) , loadTestsFromModule,

tests = []
for name in dir(module):
    obj = getattr(module, name)
    if isinstance(obj, type) and issubclass(obj, case.TestCase):
        tests.append(self.loadTestsFromTestCase(obj))

abc_backend/__init__.py , isinstance(obj, type) and issubclass(obj, case.TestCase) False ( tests ).

, ( -stdlib ): ( load_tests).

,

topΒ» python3 -m unittest discover abc_backend

?

, :

if len(argv) > 1 and argv[1].lower() == 'discover':
    # -m unittest discover
    loader.discover(...)
else:
    # -m unittest
    loader.loadTestsFromNames(...)

argv ['python3 -m unittest', 'discover', 'abc_backend'], . argv ['python3 -m unittest', 'abc_backend'], loadTestsFromNames, loadTestsFromModule - , . , unittest/main.py.

+4

, API Python:

(, init.py), load_tests. , package.load_tests (, , ). , , load_tests loader.discover.

, , , load_tests TestCase __init __. py

0

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


All Articles