Skip to content

Commit

Permalink
Add marker for TF tests (#272)
Browse files Browse the repository at this point in the history
**Description of the Change:**

- Tests using the soft requirement tensorflow needs to be marked
`@pytest.mark.tf`.
- It also unlocks only running those tests (or not) `pytest -m tf
tests/` `pytest -m not tf tests/`
- If TF is not installed the tests are skipped.
  • Loading branch information
rmoyard authored Sep 5, 2023
1 parent 33e71bb commit 2eaf03a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
50 changes: 49 additions & 1 deletion conftest.py → frontend/test/pytest/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
"""Setup pytest configurations."""
# Copyright 2023 Xanadu Quantum Technologies Inc.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Pytest configuration file for Catalyst test suite.
"""
# pylint: disable=unused-import
import pytest

try:
import tensorflow as tf
except (ImportError, ModuleNotFoundError) as e:
tf_available = False
else:
tf_available = True


def pytest_addoption(parser):
"""Add pytest custom options."""
Expand Down Expand Up @@ -42,6 +65,31 @@ def pytest_configure(config):
)


def pytest_runtest_setup(item):
"""Automatically skip tests if interfaces are not installed"""
interfaces = {"tf"}
available_interfaces = {
"tf": tf_available,
}

allowed_interfaces = [
allowed_interface
for allowed_interface in interfaces
if available_interfaces[allowed_interface] is True
]

# load the marker specifying what the interface is
all_interfaces = {"tf"}
marks = {mark.name for mark in item.iter_markers() if mark.name in all_interfaces}

for b in marks:
if b not in allowed_interfaces:
pytest.skip(
f"\nTest {item.nodeid} only runs with {allowed_interfaces}"
f" interfaces(s) but {b} interface provided",
)


def pytest_collection_modifyitems(config, items):
"""A pytest items modifier method"""

Expand Down
3 changes: 3 additions & 0 deletions frontend/test/pytest/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
markers =
tf: marks tests for tf testing (select with '-m "tf"')
23 changes: 13 additions & 10 deletions frontend/test/pytest/test_autograph.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

"""PyTests for the AutoGraph source-to-source transformation feature."""

import os

import numpy as np
import pennylane as qml
Expand All @@ -27,18 +26,20 @@
# pylint: disable=unnecessary-lambda-assignment


class TestIntegration:
"""Test that the autograph transformations trigger correctly in different settings."""
def test_unavailable(mocker):
"""Check the error produced in the absence of tensorflow."""
mocker.patch.dict("sys.modules", {"tensorflow": None})

def test_unavailable(self, monkeypatch):
"""Check the error produced in the absence of tensorflow."""
monkeypatch.syspath_prepend(os.path.join(os.path.dirname(__file__), "mock"))
def fn(x):
return x**2

def fn(x):
return x**2
with pytest.raises(ImportError, match="AutoGraph feature in Catalyst requires TensorFlow"):
qjit(autograph=True)(fn)

with pytest.raises(ImportError, match="AutoGraph feature in Catalyst requires TensorFlow"):
qjit(autograph=True)(fn)

@pytest.mark.tf
class TestIntegration:
"""Test that the autograph transformations trigger correctly in different settings."""

def test_unsupported_object(self):
"""Check the error produced when attempting to convert an unsupported object (neither of
Expand Down Expand Up @@ -188,6 +189,7 @@ def fn(x: float):
assert fn(np.pi) == -1


@pytest.mark.tf
class TestCodePrinting:
"""Test that the transformed source code can be printed in different settings."""

Expand Down Expand Up @@ -317,6 +319,7 @@ def fn(x: float):
assert autograph_source(inner)


@pytest.mark.tf
class TestConditionals:
"""Test that the autograph transformations produce correct results on conditionals.
These tests are adapted from the test_conditionals.TestCond class of tests."""
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ isort

lit
pytest
pytest-mock
pytest-xdist
pytest-cov
nbmake

0 comments on commit 2eaf03a

Please sign in to comment.