This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
What is one useful feature of defining fixtures in a conftest.py file?
Tests in the same directory and its subdirectories can request those fixtures by name without importing them.
Fixtures defined in conftest.py run only when the test file imports them.
Fixtures in conftest.py can only be requested by tests in that exact same file.
What makes a test a good candidate for @pytest.mark.parametrize?
@pytest.mark.parametrize
When the same test logic should run for several input values or input/expected-value pairs.
When unrelated behaviors need to be checked in one test function.
When a test needs to run in parallel.
How do you parametrize a test that needs both an input value and an expected value?
Use a comma-separated argument-name string and a list of tuples, such as @pytest.mark.parametrize("test_input, expected_value", [("3+5", 8), ("3*4", 12)]).
@pytest.mark.parametrize("test_input, expected_value", [("3+5", 8), ("3*4", 12)])
Use one list for all input values and a separate, unrelated list for all expected values.
Avoid multiple argument names because pytest doesn't support them.
What does scope="module" do for a pytest fixture?
scope="module"
Pytest creates and caches one fixture value for the module, then tears it down during module teardown after pytest finishes the last test in that module.
Pytest creates a new fixture value before every assertion in the module.
Pytest makes the fixture available without a conftest.py file.
conftest.py
In a yield-based fixture, when does the code after yield run?
yield
During teardown, after the requesting test or fixture scope finishes.
Before pytest passes the fixture value to the test.
Only when the test fails.
Why use the monkeypatch fixture when replacing a function or environment value in a test?
monkeypatch
It automatically undoes the change after the requesting test or fixture finishes.
It permanently changes the application code so later tests see the same value.
It runs parametrized tests in parallel.
You must answer all questions before checking your work.
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?