import file mismatch in pytest

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.


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.


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