### Install pytest-unmagic Source: https://github.com/dimagi/pytest-unmagic/blob/main/README.md Installs the pytest-unmagic package using pip. ```sh pip install pytest-unmagic ``` -------------------------------- ### Running pytest-unmagic Test Suite Source: https://github.com/dimagi/pytest-unmagic/blob/main/README.md Instructions for setting up and running the test suite for the pytest-unmagic project. This involves navigating to the project directory, installing the package in editable mode, and then executing pytest. ```shell cd path/to/pytest-unmagic pip install -e . pytest ``` -------------------------------- ### Install a Magic Fixture Fence Source: https://github.com/dimagi/pytest-unmagic/blob/main/README.md Shows how to use `fence.install()` to set up a warning mechanism for unintended magic fixture usage within specified package namespaces. ```python from unmagic import fence fence.install(['mypackage.tests']) ``` -------------------------------- ### Define and Use a Basic Fixture Source: https://github.com/dimagi/pytest-unmagic/blob/main/README.md Demonstrates defining a fixture with `@fixture` and applying it to a test function using `@use`. The fixture manages a list of traces, ensuring it's empty before setup and cleared after teardown. ```python from unmagic import fixture, use traces = [] @fixture def tracer(): assert not traces, f"unexpected traces before setup: {traces}" yield traces.clear() @use(tracer) def test_append(): traces.append("hello") assert traces, "expected at least one trace" ``` -------------------------------- ### Register Fixtures for Autouse Source: https://github.com/dimagi/pytest-unmagic/blob/main/README.md Demonstrates how to automatically apply fixtures to tests using `@fixture(autouse=...)` or `unmagic.autouse`. Autouse can be enabled for the entire session (`True`), a specific module/package (`__file__`), or multiple locations. ```python # tests/fixtures.py from unmagic import fixture @fixture def a_fixture(): ... # tests/test_this.py from unmagic import autouse from .fixtures import a_fixture autouse(a_fixture, __file__) ... # tests/test_that.py from unmagic import autouse from .fixtures import a_fixture autouse(a_fixture, __file__) ... ``` -------------------------------- ### Chaining Fixtures with @use in Python Source: https://github.com/dimagi/pytest-unmagic/blob/main/README.md Demonstrates how to chain pytest fixtures using the `@use` decorator. This allows fixtures to depend on and utilize the results of other fixtures, creating a clear dependency chain. ```python from unmagic import fixture, use @use("db") def parent_fixture(): daedalus = Person.objects.create(name='Daedalus') yield daedalus daedalus.delete() @use(parent_fixture) @fixture def child_fixture(): daedalus = parent_fixture() icarus = Person.objects.create(name='Icarus', father=daedalus) yield @use(child_fixture) def test_flight(): flyers = Person.objects.all() ... ``` -------------------------------- ### Apply pytest Fixtures with @use Source: https://github.com/dimagi/pytest-unmagic/blob/main/README.md Demonstrates applying fixtures defined with `@pytest.fixture` to a test or another fixture by passing the fixture name to the `@use` decorator. This is particularly useful for fixtures with side effects like database operations. ```python from unmagic import use @use("db") def test_database(): ... ``` -------------------------------- ### Shorthand for Applying a Single Fixture Source: https://github.com/dimagi/pytest-unmagic/blob/main/README.md Shows the shorthand syntax for applying a single fixture directly as a decorator to a test function, equivalent to using `@use(fixture_name)`. ```python @tracer def test_append(): traces.append("hello") assert traces, "expected at least one trace" ``` -------------------------------- ### Apply Fixtures to Test Classes Source: https://github.com/dimagi/pytest-unmagic/blob/main/README.md Illustrates how to apply one or more fixtures to all tests within a class by using the `@use` decorator on the test class itself. ```python @use(tracer) class TestClass: def test_galaxy(self): traces.append("Is anybody out there?") ``` -------------------------------- ### Access pytest Request Object Source: https://github.com/dimagi/pytest-unmagic/blob/main/README.md Illustrates how to access the pytest request object within a test using `unmagic.get_request()`. This allows retrieving other fixtures, such as 'capsys' for capturing output. ```python from unmagic import get_request def test_output(): capsys = get_request().getfixturevalue("capsys") print("hello") captured = capsys.readouterr() assert captured.out == "hello\n" ``` -------------------------------- ### Declare Fixture Scope Source: https://github.com/dimagi/pytest-unmagic/blob/main/README.md Shows how to specify the scope of a fixture using the `scope` parameter in the `@fixture` decorator. Supported scopes include 'function', 'class', 'module', 'package', and 'session'. ```python @fixture(scope="class") def tracer(): traces = [] yield traces assert traces, "expected at least one trace" ``` -------------------------------- ### Retrieve Fixture Value by Calling Source: https://github.com/dimagi/pytest-unmagic/blob/main/README.md Demonstrates how to retrieve the yielded value of a fixture within a test function or another fixture by directly calling the fixture function. This is analogous to `request.getfixturevalue()`. ```python @fixture def tracer(): assert not traces, f"unexpected traces before setup: {traces}" yield traces traces.clear() def test_append(): traces = tracer() traces.append("hello") assert traces, "expected at least one trace" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.