U
U
un1t2016-07-29 11:21:00
Django
un1t, 2016-07-29 11:21:00

Why doesn't pytest see the module and an ImportError: occurs?

Running py.test throws an error
ImportError: No module named 'apps.catalog.tests.factory'; 'apps.catalog.tests' is not a package
All other tests pass fine.
And the code is working.
If you import from python or ./manage.py shell then everything works.
All __init__.py are present in the right places.
This problem is only with one application, all the others are imported normally. I even tried to bang this application and recreate it. Did not help.
Any ideas where to look? What could be the problem?
py.test.ini

[pytest]
DJANGO_SETTINGS_MODULE=project.testing_settings
testpaths=apps
python_files=
    tests.py
    test_*.py
pep8maxlinelength=150
pep8ignore=
    migrations/*.py ALL

$ py.test apps/core/tests/test_dev.py 
============================================================= test session starts =============================================================
platform linux -- Python 3.4.3, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
django settings: project.testing_settings (from ini file)
rootdir: /home/ilya/workspace/myproject, inifile: pytest.ini
plugins: pep8-1.0.6, django-2.9.1, cov-2.3.0, xdist-1.14
collected 0 items / 1 errors 

=================================================================== ERRORS ====================================================================
________________________________________________ ERROR collecting apps/core/tests/test_dev.py _________________________________________________
apps/core/tests/test_dev.py:4: in <module>
    from ..dev import fill_dev_db
apps/core/dev.py:1: in <module>
    from apps.catalog.tests.factory import TagFactory
E   ImportError: No module named 'apps.catalog.tests.factory'; 'apps.catalog.tests' is not a package
=========================================================== 1 error in 0.10 seconds ===========================================================

I figured out that if 'apps.catalog.tests' is set in INSTALLED_APPS, then the test passes.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
un1t, 2016-07-29
@un1t

When writing further code, he suddenly began to give a more adequate error. It turned out that in the catalog application there is both the tests folder and the tests.py file. This was the problem.

import file mismatch:
imported module 'apps.catalog.tests' has this __file__ attribute:
  /home/ilya/workspace/myproject/apps/catalog/tests
which is not the same as the test file we want to collect:
  /home/ilya/workspace/myproject/apps/catalog/tests.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question