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

ImportError when running tests using ptw #129

Closed
sohang3112 opened this issue Nov 18, 2022 · 2 comments
Closed

ImportError when running tests using ptw #129

sohang3112 opened this issue Nov 18, 2022 · 2 comments

Comments

@sohang3112
Copy link

I have a folder structure like this:

root
|- title_api.py
|- tests
    |- test_title_api.py

Content of test_title_api.py is:

from title_api import *

def test_dummy():
    assert False

Running pytest in the root directory successfully runs test_dummy. But when ptw is run in the root directory, it complains of an ImportError as Python can't find title_api module.

pytest-watch should work similarly to pytest here (add the parent directory to sys.path before running tests).

Notes:

  • I install latest commit of pytest-watch from its Github url.
  • I am running version 7.2.0 of pytest in Python 3.11.
  • My OS is Windows 11.
@kirkbroadhurst
Copy link

I have experienced this issue myself.

It seems to be resolved by adding an __init__.py file to your tests folder.

I haven't dug in to explore why it occurs. Adding an (empty) __init__.py to your tests folder can have other impacts, mostly around static analysis, but is generally pretty safe / minor.

@sohang3112
Copy link
Author

It seems to be resolved by adding an init.py file to your tests folder.

Thanks, this worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants