Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytest run testfiles with same name in different directory will fail #3151

Closed
iqianxing opened this issue Jan 25, 2018 · 7 comments
Closed
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@iqianxing
Copy link

iqianxing commented Jan 25, 2018

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
@pytestbot pytestbot added the type: bug problem that needs to be addressed label Jan 25, 2018
@iqianxing
Copy link
Author

My project is an example: https://github.com/iqianxing/unittest-demos.git

@nicoddemus
Copy link
Member

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 nicoddemus added type: question general question, might be closed after 2 weeks of inactivity and removed type: bug problem that needs to be addressed labels Jan 25, 2018
@iqianxing
Copy link
Author

@nicoddemus Thank you!

bethebunny added a commit to bethebunny/pytext-1 that referenced this issue Feb 11, 2019
Summary:
pytest-dev/pytest#3151

If there isn't a __init__.py file in a directory, pytest imports a test file into the global namespace. This means in a package structure not including __init__.py files, test files in separate directories with the same basename (filename not including directory) will overlap, and fortunately pytest fails with a visible error message rather than not executing the tests.

Differential Revision: D14031515

fbshipit-source-id: a6032e44eda2b32138235e2728925cf56fc24897
facebook-github-bot pushed a commit to facebookresearch/pytext that referenced this issue Feb 12, 2019
Summary:
Pull Request resolved: #287

pytest-dev/pytest#3151

If there isn't a __init__.py file in a directory, pytest imports a test file into the global namespace. This means in a package structure not including __init__.py files, test files in separate directories with the same basename (filename not including directory) will overlap, and fortunately pytest fails with a visible error message rather than not executing the tests.

Reviewed By: Titousensei

Differential Revision: D14031515

fbshipit-source-id: dc0111b1fa62a630cf1be3da555632041ac7ba3c
st-- added a commit to GPflow/GPflow that referenced this issue Feb 26, 2020
hofstee added a commit to phanrahan/magma that referenced this issue Feb 28, 2020
hofstee added a commit to phanrahan/magma that referenced this issue Feb 28, 2020
idavidmcdonald added a commit to alphagov/notifications-functional-tests that referenced this issue Apr 20, 2020
This should hopefully fix
pytest-dev/pytest#3151

as we have seen the following error on concourse:
```
import file mismatch:
imported module 'test_notify_api_letter' has this __file__ attribute:
  /var/project/tests/functional/preview_and_dev/test_notify_api_letter.py
which is not the same as the test file we want to collect:
  /var/project/tests/functional/staging_and_prod/notify_api/test_notify_api_letter.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
```
@animaxcg
Copy link

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

@bisdak
Copy link

bisdak commented Sep 22, 2020

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

@The-Compiler
Copy link
Member

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

gnn added a commit to openego/eGon-data that referenced this issue Nov 17, 2020
This fixes the tests failing with an error because they're importing two
different `openstreetmap` modules from two different locations but
getting confused between the two. There's an [issue comment][0] and a
[documentation section][1] explaining this. Unfortunately adding the
`--import-mode=importlib` option didn't fix the error so I had to use
the `__init__.py` fix. Maybe it's a doctest specific thing.

[0]: pytest-dev/pytest#3151 (comment)
[1]: https://docs.pytest.org/en/latest/pythonpath.html#import-modes
nokome added a commit to stencila/hub that referenced this issue Nov 24, 2020
rht added a commit to rht/QuantEcon.py that referenced this issue Jan 19, 2021
rht added a commit to rht/QuantEcon.py that referenced this issue Jan 19, 2021
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…st/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…t/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…t/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…stenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…t/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…t/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…st/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…st/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…ist/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…r dist/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
… dist/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…ist/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…st/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
… for dist/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…dist/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…s for dist/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…s for dist/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…rs for dist/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…t/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…dist/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…dirs for dist/testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
bell-sw pushed a commit to bell-sw/alpaquita-aports that referenced this issue Jul 31, 2023
…testenv

[ commit 083eb89bb9af1e5570429a2580c0c2d5d136be4c ]

this works around issues like pytest-dev/pytest#3151
@criccomini
Copy link

It seems like using importlib for import-mode fixes this (example of a pyproject.toml config):

[tool.pytest.ini_options]
addopts = [
    # Allow test files to have the same name in different directories.
    "--import-mode=importlib",
]

https://docs.pytest.org/en/stable/explanation/goodpractices.html#tests-outside-application-code

janboll added a commit to janboll/qontract-reconcile that referenced this issue Mar 22, 2024
janboll added a commit to app-sre/qontract-reconcile that referenced this issue Mar 25, 2024
* Add basic MergeRequestManager

Add a parser to parse open MergeRequests

* Additional code for merge request manager

* Add GitlabFilePersistence

* Various fixes

* Move things around to make code base easier to read

* Various small fixes

* Adding state handling

By this, templates are not rendered, when they where rendered in the past and have not changed. Variable input is also taken into account for this. A state key is kept for each collection, a has is computed over the template content and the variable content

* Adding tests for Merge Request Manager

* Adding renderer tests

* Adding tests for gitlab persistence

* Fix wrong import

* Test filenames need to be unique pytest-dev/pytest#3151

* Fix commit message, rename template_hash, since it's a hash of the collection

* Slight adjustments

Allow closing MRs
Directly open MRs after closing them
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

7 participants