Skip to content

Commit

Permalink
Add prelim support for test-level markers (#5517)
Browse files Browse the repository at this point in the history
### Before submitting

Please complete the following checklist when submitting a PR:

- [x] All new features must include a unit test.
If you've fixed a bug or added code that should be tested, add a test to
the
      test directory!

- [x] All new functions and code must be clearly commented and
documented.
If you do make documentation changes, make sure that the docs build and
      render correctly by running `make docs`.

- [x] Ensure that the test suite passes, by running `make test`.

- [x] Add a new entry to the `doc/releases/changelog-dev.md` file,
summarizing the
      change, and including a link back to the PR.

- [x] The PennyLane source code conforms to
      [PEP8 standards](https://www.python.org/dev/peps/pep-0008/).
We check all of our code against [Pylint](https://www.pylint.org/).
      To lint modified files, simply `pip install pylint`, and then
      run `pylint pennylane/path/to/file.py`.

When all the above are checked, delete everything above the dashed
line and fill in the pull request template.


------------------------------------------------------------------------------------------------------------

**Context:** This PR introduces new test-level markers for PennyLane,
which can begin use for indication of where interactions take-place in
the given suite. This forms part of the work on improving testing across
the packages
[sc-61416]

**Description of the Change:** Add 3 new test-level markers for pytest:
`unit_test`, `integration_test` and `system_test`. Some sample tests
that fit into the 3 categories are added for examination purposes.

**Benefits:** This will help us to structure our test-suite to better
reflect localised tests from intermodule and interpackage tests.

**Possible Drawbacks:** 3 new markers to work with.

**Related GitHub Issues:**

---------

Co-authored-by: Thomas R. Bromley <49409390+trbromley@users.noreply.github.com>
  • Loading branch information
mlxd and trbromley committed May 23, 2024
1 parent 058603a commit b05565e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

<h3>Improvements 🛠</h3>

* Add support for 3 new pytest markers: `unit`, `integration` and `system`.
[(#5517)](https://github.com/PennyLaneAI/pennylane/pull/5517)

* The sorting order of parameter-shift terms is now guaranteed to resolve ties in the absolute value with the sign of the shifts.
[(#5582)](https://github.com/PennyLaneAI/pennylane/pull/5582)

Expand Down Expand Up @@ -175,5 +178,6 @@ Soran Jahangiri,
Korbinian Kottmann,
Christina Lee,
Vincent Michaud-Rioux,
Lee James O'Riordan,
Kenya Sakka,
David Wierichs.
2 changes: 2 additions & 0 deletions tests/numpy/test_numpy_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
general_gen = random.default_rng()


@pytest.mark.unit
class TestGeneratorDistributions:
@pytest.mark.parametrize("distribution", distributions_no_extra_input)
def test_generator_distributions(self, distribution):
Expand All @@ -61,6 +62,7 @@ def test_generator_distributions(self, distribution):
assert output.requires_grad is False


@pytest.mark.unit
class Test_default_rng:
def test_no_input(self):
"""Tests that np.random.default_rng() returns a generator object when
Expand Down
9 changes: 9 additions & 0 deletions tests/numpy/test_numpy_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from pennylane.numpy.tensor import tensor_to_arraybox


@pytest.mark.unit
class TestExtractTensors:
"""Tests for the extract_tensors function"""

Expand Down Expand Up @@ -56,6 +57,7 @@ def test_iterable_with_unpatched_numpy_arrays(self):
assert res[1] is arr2


@pytest.mark.unit
class TestTensor:
"""Tests for the Tensor(ndarray) subclass"""

Expand Down Expand Up @@ -141,6 +143,7 @@ def test_numpy_to_arraybox(self):
]


@pytest.mark.unit
class TestNumpyIntegration:
"""Test that the wrapped NumPy functionality integrates well
with standard NumPy functions."""
Expand Down Expand Up @@ -428,6 +431,7 @@ def __call__(self, *args, **kwargs):
assert len(res) == 2


@pytest.mark.integration
class TestAutogradIntegration:
"""Test autograd works with the new tensor subclass"""

Expand Down Expand Up @@ -458,6 +462,7 @@ def cost(x):
grad_fn(arr1)


@pytest.mark.unit
class TestScalarHashing:
"""Test for the hashing capability of scalar arrays."""

Expand Down Expand Up @@ -505,13 +510,15 @@ def test_nonzero_dim_arrays_non_hashable(self):
class TestNumpyConversion:
"""Tests for the tensor.unwrap() and tensor.numpy() methods"""

@pytest.mark.unit
def test_convert_scalar_array(self):
"""Test that a scalar array converts to a python literal"""
data = np.array(1.543)
res = data.unwrap()
assert res == data.item()
assert isinstance(res, float)

@pytest.mark.unit
def test_convert_array(self):
"""Test that a numpy array successfully converts"""
data = np.array([1, 2, 3])
Expand All @@ -522,6 +529,7 @@ def test_convert_array(self):
assert isinstance(res, np.ndarray)
assert not isinstance(res, np.tensor)

@pytest.mark.system
def test_single_gate_parameter(self):
"""Test that when supplied a PennyLane tensor, a QNode passes an
unwrapped tensor as the argument to a gate taking a single parameter"""
Expand All @@ -545,6 +553,7 @@ def circuit(phi=None):
assert op.name == "RX"
assert op.parameters == [p]

@pytest.mark.system
def test_multiple_gate_parameter(self):
"""Test that when supplied a PennyLane tensor, a QNode passes arguments
as unwrapped tensors to a gate taking multiple parameters"""
Expand Down
3 changes: 3 additions & 0 deletions tests/pytest.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[pytest]
markers =
unit: marks tests as unit-level tests (select with '-m "unit"')
integration: marks tests as integration-level tests (select with '-m "integration"')
system: marks tests as system-level tests (select with '-m "system"')
core: marks tests for core testing (select with '-m "core"')
autograd: marks tests for autograd testing (select with '-m "autograd"')
torch: marks tests for torch testing (select with '-m "core"')
Expand Down

0 comments on commit b05565e

Please sign in to comment.