From b05565e99561dd32fdd60f10fcad240086f389bb Mon Sep 17 00:00:00 2001 From: Lee James O'Riordan Date: Thu, 23 May 2024 17:14:34 -0400 Subject: [PATCH] Add prelim support for test-level markers (#5517) ### 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> --- doc/releases/changelog-dev.md | 4 ++++ tests/numpy/test_numpy_random.py | 2 ++ tests/numpy/test_numpy_wrapper.py | 9 +++++++++ tests/pytest.ini | 3 +++ 4 files changed, 18 insertions(+) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index cfdfee22ab3..dce3788a2cd 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -9,6 +9,9 @@

Improvements 🛠

+* 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) @@ -175,5 +178,6 @@ Soran Jahangiri, Korbinian Kottmann, Christina Lee, Vincent Michaud-Rioux, +Lee James O'Riordan, Kenya Sakka, David Wierichs. diff --git a/tests/numpy/test_numpy_random.py b/tests/numpy/test_numpy_random.py index d8db4ff0e27..6cfd9e483a6 100644 --- a/tests/numpy/test_numpy_random.py +++ b/tests/numpy/test_numpy_random.py @@ -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): @@ -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 diff --git a/tests/numpy/test_numpy_wrapper.py b/tests/numpy/test_numpy_wrapper.py index 0c28828e858..f89bc715b6e 100644 --- a/tests/numpy/test_numpy_wrapper.py +++ b/tests/numpy/test_numpy_wrapper.py @@ -25,6 +25,7 @@ from pennylane.numpy.tensor import tensor_to_arraybox +@pytest.mark.unit class TestExtractTensors: """Tests for the extract_tensors function""" @@ -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""" @@ -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.""" @@ -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""" @@ -458,6 +462,7 @@ def cost(x): grad_fn(arr1) +@pytest.mark.unit class TestScalarHashing: """Test for the hashing capability of scalar arrays.""" @@ -505,6 +510,7 @@ 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) @@ -512,6 +518,7 @@ def test_convert_scalar_array(self): 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]) @@ -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""" @@ -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""" diff --git a/tests/pytest.ini b/tests/pytest.ini index 85bd26efbd6..4c2812d4a75 100644 --- a/tests/pytest.ini +++ b/tests/pytest.ini @@ -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"')