pytest-pytorch

pytest plugin for a better developer experience when working with the PyTorch test suite

Why do I need it?

Some testcases in the PyTorch test suite are only used as templates and will be instantiated at runtime. Unfortunately, [PyTorch]'s naming scheme for parametrizations differs from [pytest]'s. As a consequence, these tests cannot be selected by their names as written and one has to remember [PyTorch]'s naming scheme. This can be especially disrupting to your workflow if your IDE (PyCharm, VSCode) relies on [pytest]'s default selection syntax.

If this has ever been a source of frustration for you, worry no longer. pytest-pytorch was made for you.

How do I install it?

You can install pytest-pytorch with pip

$ pip install pytest-pytorch

or with conda:

$ conda install -c conda-forge pytest-pytorch

How do I use it?

With pytest-pytorch installed you can select test cases and tests by their names as written:

Use case Command
Run a test case against all devices pytest test_foo.py::TestBar
Run a test against all devices pytest test_foo.py::TestBar::test_baz

Similar to a parametrization by @pytest.mark.parametrize you can use the -k flag to select a specific set of parameters:

Use case Command
Run a test case against one device pytest test_foo.py::TestBar -k "$DEVICE"
Run a test against one device pytest test_foo.py::TestBar::test_baz -k "$DEVICE"

Can I have a little more background?

Sure, we have written a blog post about pytest-pytorch that goes into details.

GitHub

https://github.com/Quansight/pytest-pytorch