import file mismatch in pytest
Asked Answered
T

8

55

I've got a file in the package with 'test' in its name and when I run pytest I got an error

import file mismatch:
imported module 'my_project.my_file_test' has this __file__ attribute:
  /my_project/src/my_project/build/lib/python2.7/site-packages/foo/my_file_test.py
which is not the same as the test file we want to collect:
  /my_project/src/my_project/build/private/python2.7/lib/foo/my_file_test.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules

If I remove 'test' from the file it works fine but unfortunately I can't change it.

So the question is how to tell pytest to ignore this file?

Tailback answered 24/12, 2018 at 22:25 Comment(4)
Did you try to remove the __pycache__ dirs?Amaranthaceous
Yes. That was the first I did - remove *.pyc and pycache but it didn’t helpTailback
renaming the file in question fixed it for me. Tried deleting the pycache, creating the init file but nothing fixed it.Underage
In my case is that I was using the same name for a module in my project directory tree. Is tricky to understand the error if the project is extensive.Momentum
T
7

So finally it was easy, I just had to add test file pattern to the pytest.ini file:

python_files = test_*.py

so pytest stopped looking for files with test in the end of the name what it did by default.

Tailback answered 27/12, 2018 at 0:29 Comment(2)
Might help someone. I had two test fiiles with the same name, renaming one resolved my issue.Perfectly
@Perfectly thanks, that saved me some time, it blows my mind how awful that is thoughWherefore
R
90

In my case, it was missing __init__.py file in tests directory.

Redress answered 26/5, 2020 at 13:57 Comment(5)
Adding an __init__.py works like a charm. Hopefully they add it to the hint.Spotweld
This other answer suggests removing __init__.py from the tests directory...Leto
Worked for me by removing init.py . Weird.Taryn
Hello @Mateusz, why do you need an __init__.py file inside your test/ directory?, can you share with us some reference about it?Momentum
Notice, if you have different tests under different directories, you might need to add an __init__.py file to each of them.Tarver
L
15

As pytest explains:

For historical reasons, pytest defaults to the prepend import mode instead of the importlib import mode we recommend for new projects. The reason lies in the way the prepend mode works.

See https://docs.pytest.org/en/7.1.x/explanation/goodpractices.html#choosing-an-import-mode
and https://docs.pytest.org/en/7.1.x/explanation/pythonpath.html#import-modes

Basically it means, when using old prepend(default) tests are imported as packages, and therefore you need __init__.py files inside test folders.

Solution defined by pytest at the mentioned links:

[tool.pytest.ini_options]
addopts = "--import-mode=importlib"

With this, importlib is used to collect tests and therefore __ini__.py files are not longer needed and test files can share names (always that are located in different folders).

Lotson answered 2/8, 2023 at 12:42 Comment(0)
T
7

So finally it was easy, I just had to add test file pattern to the pytest.ini file:

python_files = test_*.py

so pytest stopped looking for files with test in the end of the name what it did by default.

Tailback answered 27/12, 2018 at 0:29 Comment(2)
Might help someone. I had two test fiiles with the same name, renaming one resolved my issue.Perfectly
@Perfectly thanks, that saved me some time, it blows my mind how awful that is thoughWherefore
K
7

A working solution from Pytest Issues 3151

Exact Quotes of the solver below:

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.

Apart from this, if possible try changing the names of your testcases file.

Kirkcudbright answered 24/1, 2023 at 10:20 Comment(0)
G
0

I was facing the same issue even though my file name was not exactly same as other.

I had test_01_login_chrome.py and test_01_login_firefox.py

Error I was getting

import file mismatch:
imported module 'test_01_login_chrome.py' has this __file__ attribute:
  /Users/avicii/PycharmProjects/Omni_Sanity/TestCase/Firefox_TestCases/test_01_login_chrome.py
which is not the same as the test file we want to collect:
  /Users/avicii/PycharmProjects/Omni_Sanity/TestCase/01_Chrome_TestCases/test_01_login_chrome.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules

I tried adding __init__.py to the test cases folder and it did not work.

Then I tried a workaround. I created a python package for my test cases, which created __init__.py by default and then moved my test cases to that folder.

And my issue was resolved.

Gaiter answered 6/9, 2020 at 5:23 Comment(1)
incomprehensible. Please describe betterBattista
C
0

A small variation of this case using the _pytester_ plugin included with _pytest_ (that yields the same error.)

There is a feature of the plugin that allows placement of example tests in your project root (outside of the package), and those tests can be copied into a test temp directory via _pytester.copy_example()_.

This feature can be used as a test loader, and also as a way to test examples.

If you use this feature, you will also want to put an \_\_init\_\_.py into the source directory for your examples.

E.g.: if copying the example from the examples directory:

pytester.copy_example("examples/test_zfs.py")

To avoid the import file mismatch error:

$ touch examples/__init__.py
Cinnamon answered 9/1, 2023 at 4:28 Comment(0)
N
0

In my case, I was getting the error because I was missing __init__.py file not in tests directory but the one above it.

Nador answered 28/4, 2023 at 13:48 Comment(2)
this answer is not novel. It is the same as what has already been answered above.Mainsail
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewCervical
D
0

In my case, I was using tests directory in each app and I forgot to remove tests.py file that is already created by default in Django. When I removed the tests.py file, everything worked fine.

Deaminate answered 8/2 at 3:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.