-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Fixture autouse and module imports in test suite (question) #3597
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
Comments
GitMate.io thinks possibly related issues are #1050 (provide dependency-tested autouse fixtures), #2861 (Question re: order of execution of test fixtures), #668 (autouse fixtures break scope rules), #2992 (Already imported module), and #1057 (Pytest fail when autouse fixture is applied and --doctest-modules is set). |
Hi @BigChief45, One approach is changing your application to a fixture, specially if it provides functionality that your tests need (login, endpoints, etc). This fixture should then depend on # conftest.py
import pytest
@pytest.fixture
def app(env_config):
import app
return app.App() If your # app.py
from requests_aws4auth import AWS4Auth
def startup():
awsauth = AWS4Auth(
ACCESS_KEY_ID,
SECRET_ACCESS_KEY,
REGION,
'es'
)
def foo():
pass This way you have more control when Hope this helps! |
You may also find pytest-env useful. Here's an example where I've used it. It'll set up these environment variables before your test suite is imported |
@nicoddemus Thanks for the suggestion. This approach works very well! @asottile Looks nice, I'll be sure to try it thanks! Closing issue. |
Uh oh!
There was an error while loading. Please reload this page.
I am having a hard time writing some tests for an application. Basically I have an autouse fixture in
conftest.py
that sets up some environment variables that the application needs to initialize some objects:The problem is that this fixture is only run until each test starts. However, in a specific test file, I am importing the module that contains functions I want to test, outside the tests:
Importing
app
crashes because the environment variables are not set yet:I cannot use a blank string as a fallback for
os.getenv
sinceAWS4Auth
fails when using them.If I move
import app
inside the test, everything works perfectly since the autouse fixture has already been executed.Would appreciate some advice.
The text was updated successfully, but these errors were encountered: