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

[BUG] - Cryptic error with pytest when you don't call pytest with the settings variable #254

Open
mrchrisadams opened this issue May 30, 2022 · 0 comments
Labels
bug Something isn't working Severity: high Bug severity level Severity: low Bug severity level Severity: medium Bug severity level

Comments

@mrchrisadams
Copy link
Member

Describe the bug

You can run the tests in this project by just calling pytest.

When you do, you see cryptic error that isn't very easy to debug.

ImportError: No module named 'path'

If you run pytest and you are explicit about using the testing settings with pytest --ds=greenweb.settings.testing then you'll see the culprit.

In this case the error was as follows:


>   from apps.greencheck.importers.amazon_importer import AmazonImporter
E   ModuleNotFoundError: No module named 'apps.greencheck.importers.amazon_importer'

apps/greencheck/management/commands/update_networks_in_db_amazon.py:1: ModuleNotFoundError

How to get these tests passing properly

Rather than remembering to pass the correct params to pytest, you can also run make test instead.

make test

Under the hood, this will call

pytest -s --create-db --ds=greenweb.settings.testing

Explained in more detail, this command runs:

  • a) makes sure we have a fresh database with create-db
  • b) Makes sure we skip capturing output so we can drop into an interactive debugging session with -s
  • c) uses the settings as outlined in the django settings file at greenweb.settings.testing, with the flag --ds=greenweb.settings.testing.

Fixing this

It might make sense to get pytest to throw an exception when it's run with the development or production settings, to catch this in future.

I think you can either do that, or make sure that the pytest.ini file is honoured, ahead of any set environment variables:

If you want to use the highest precedence in the configuration file, you can use addopts = --ds=yourtestsettings.

@mrchrisadams mrchrisadams added bug Something isn't working Severity: high Bug severity level Severity: medium Bug severity level Severity: low Bug severity level labels May 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Severity: high Bug severity level Severity: low Bug severity level Severity: medium Bug severity level
Projects
None yet
Development

No branches or pull requests

1 participant