Skip to content

Test helpers

Warning: These helpers are currently an internal concern and changes will not necessarily be reflected in semver.

_pytester_helpers

Helper classes and functions to support testing this plugin with pytester.

CollectedDir dataclass

CollectedDir(
    pytester_instance: Pytester,
    dir_node: Dir,
    items: list[Item],
)

The various elements required to test directory collection.

  • pytester_instance: pytest.Pytester
  • dir_node: pytest.Dir
  • items: list[pytest.Item]

CollectionTree

CollectionTree(
    *_,
    node: Item | Collector | _DummyNode,
    children: list[CollectionTree] | None
)

A (top-down) tree of pytest collection Nodes.

Designed to enable testing the results of collection plugins via:

assert CollectionTree.from_items(pytester.genitems([...])) == CollectionTree.from_dict({...})

children instance-attribute

children = children

either:

  • if node is pytest.Collector: a list[CollectionTree] of child nodes
  • if node is pytest.Item: None

node instance-attribute

node = node

The actual collected node.

_DummyNode dataclass

_DummyNode(
    name: str, nodetype: type, parent: Self | None = None
)

A dummy node for a CollectionTree, used by CollectionTree.from_dict().

Compares equal to a genuine pytest.Node if: - type(Node) == _DummyNode.nodetype (strict, subclasses will not match) - repr(Node) == _DummyNode.name.

parent class-attribute instance-attribute
parent: Self | None = None

Always None but required to avoid attribute errors if type checking Union[pytest.Node,_DummyNode]

__eq__

__eq__(other: Self) -> bool

CollectionTrees are equal if their children and node attributes are equal.

__repr__

__repr__() -> str

Indented, multiline representation of the tree to simplify interpreting test failures.

_walk_up_tree classmethod

_walk_up_tree(branches: list[Self]) -> Self

Walk up the collection tree from a list of branches/leaves until reaching the pytest.Session.

Returns: the Session CollectionTree.

from_dict classmethod

from_dict(
    tree: dict[tuple[str, type], dict | None],
) -> Self

Create a dummy CollectionTree from a dict of dicts with following format:

{(str: name, type: Nodetype):
    (str: name, type: Nodetype): {
        (str: name, type: Nodetype): None,
        (str: name, type: Nodetype): None
        }
    }
}

For example:

tree_dict = {
    ("<Session  exitstatus='<UNSET>' testsfailed=0 testscollected=0>", pytest.Session): {
        ("<Dir tests>", pytest.Dir): {
            ("<Module test_module.py>", pytest.Module): {
                ("<Function test_adder>", pytest.Function): None,
                ("<Function test_globals>", pytest.Function): None,
            },
        },
    },
}
tree = CollectionTree.from_dict(tree_dict)

from_items classmethod

from_items(items: list[Item]) -> Self

Create a single CollectionTree from a list of collected Items.

It is intended that this function is passed the result of pytester.genitems()

Returns: a CollectionTree with the Session as the root.

ExampleDir dataclass

ExampleDir(
    files: list[Path] = list(),
    conftest: str = "",
    ini: str = "",
    notebooks: dict[str, list[str]] = dict(),
)

The various elements to set up a pytester instance.

ExampleDirRequest

Typehint to param passed to example_dir.

add_ipytest_magic

add_ipytest_magic(source: str) -> str

Add %%ipytest magic to the source code.

example_dir

example_dir(
    request: ExampleDirRequest, pytester: Pytester
) -> CollectedDir

Parameterised fixture. Requires a list of Paths to copy into a pytester instance.