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

Update advice about _called_from_test. #6168

Merged
merged 1 commit into from
Nov 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 9 additions & 12 deletions doc/en/example/simple.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
shields-fn marked this conversation as resolved.
Show resolved Hide resolved

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"
...

Copy link
Member

Choose a reason for hiding this comment

The 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 -

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do I do that?

Copy link
Member

Choose a reason for hiding this comment

The 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
--------------------------------------------------------------
Expand Down