pytest run testfiles with same name in different directory will fail

Created on 25 Jan 2018  路  6Comments  路  Source: pytest-dev/pytest

Thanks for submitting an issue!

Here's a quick checklist in what to include:

  1. The console output like below:
D:\pytest>pytest
============================= test session starts =============================
platform win32 -- Python 2.7.13, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: D:\pytest, inifile:
collected 40 items / 1 errors

=================================== ERRORS ====================================
_________________ ERROR collecting pytest/test_helloworld.py __________________
import file mismatch:
imported module 'test_helloworld' has this __file__ attribute:
  D:\pytest\case\test_helloworld.py
which is not the same as the test file we want to collect:
  D:\pytest\pytest\test_helloworld.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.88 seconds ===========================
  1. The test files recongalize like below:
    image
question

Most helpful comment

Ho @iqianxing, this is happening because your test files have the same name in two different directories, and they are imported into the global namespace because there's no `__init__ files; there's a more detailed explanation in the docs.

In this case you can either create case/__init__.py and pytest/__init__.py files or rename the test files to unique names.

All 6 comments

Ho @iqianxing, this is happening because your test files have the same name in two different directories, and they are imported into the global namespace because there's no `__init__ files; there's a more detailed explanation in the docs.

In this case you can either create case/__init__.py and pytest/__init__.py files or rename the test files to unique names.

@nicoddemus Thank you!

init files everywhere and this is still an issue when your module your testing imports a library that as the same name as a library you are testing for another module

Create a 'pytest.ini' file. Here's an example.

[pytest]
DJANGO_SETTINGS_MODULE = booktime.settings
python_files = tests.py test_*.py *_tests.py

It's better to put all your test modules in to one package like this one.
tests/
-- __init__.py
-- test_sample1.py
-- test_sample2.py

Also see the explanation in the docs here: https://docs.pytest.org/en/stable/goodpractices.html#tests-outside-application-code

Was this page helpful?
0 / 5 - 0 ratings