-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Update advice about _called_from_test. #6168
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -302,34 +302,31 @@ running from a test you can do something like this: | |
|
|
||
| .. code-block:: python | ||
|
|
||
| # content of conftest.py | ||
| # content of your_module.py | ||
|
|
||
|
|
||
| def pytest_configure(config): | ||
| import sys | ||
| _called_from_test = False | ||
|
|
||
| sys._called_from_test = True | ||
| .. code-block:: python | ||
|
|
||
| # content of conftest.py | ||
|
|
||
| def pytest_unconfigure(config): | ||
| import sys | ||
|
|
||
| del sys._called_from_test | ||
| def pytest_configure(config): | ||
| your_module._called_from_test = True | ||
|
|
||
| and then check for the ``sys._called_from_test`` flag: | ||
| and then check for the ``your_module._called_from_test`` flag: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| if hasattr(sys, "_called_from_test"): | ||
| if your_module._called_from_test: | ||
| # called from within a test run | ||
| ... | ||
| else: | ||
| # called "normally" | ||
| ... | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i noted that we haven't yet a actual example execution block, so this example isn't actually executed demoing the effect -
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do I do that?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TBH I think the current example is fine, it is a really contrived use case anyways. 🤷♂ |
||
| accordingly in your application. It's also a good idea | ||
| to use your own application module rather than ``sys`` | ||
| for handling flag. | ||
| accordingly in your application. | ||
|
|
||
| Adding info to test report header | ||
| -------------------------------------------------------------- | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.