From dc530400db7f443e946ddae84519375d78bfc4ef Mon Sep 17 00:00:00 2001 From: Dor Harpaz Date: Thu, 14 Nov 2024 16:52:10 +0200 Subject: [PATCH] Move skip to the test ; add caching --- .../algorithms/test_bernstein_vazirani.py | 3 +++ tests/utils/utils_for_testbook.py | 6 +----- tests/utils/utils_for_tests.py | 19 +++++++++++++------ 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/tests/notebooks/algorithms/test_bernstein_vazirani.py b/tests/notebooks/algorithms/test_bernstein_vazirani.py index e8373d14..0007492d 100644 --- a/tests/notebooks/algorithms/test_bernstein_vazirani.py +++ b/tests/notebooks/algorithms/test_bernstein_vazirani.py @@ -1,11 +1,14 @@ +import pytest from tests.utils.utils_for_testbook import ( assert_unchanged_size, assert_valid_model, execute_testbook_with_timeout, ) +from tests.utils.utils_for_tests import should_skip_notebook from testbook.client import TestbookNotebookClient # type: ignore[import] +@pytest.mark.skipif(should_skip_notebook("bernstein_vazirani"), reason="Didn't change") @execute_testbook_with_timeout("bernstein_vazirani", timeout_seconds=20) def test_bernstein_vazirani(tb: TestbookNotebookClient) -> None: # test models diff --git a/tests/utils/utils_for_testbook.py b/tests/utils/utils_for_testbook.py index 20194ef3..766bed9c 100644 --- a/tests/utils/utils_for_testbook.py +++ b/tests/utils/utils_for_testbook.py @@ -1,14 +1,13 @@ import base64 import json import os -import pytest import pickle import re from typing import Any, Callable from testbook import testbook # type: ignore[import] from testbook.client import TestbookNotebookClient # type: ignore[import] -from tests.utils.utils_for_tests import resolve_notebook_path, should_test_notebook +from tests.utils.utils_for_tests import resolve_notebook_path from classiq.interface.generator.quantum_program import QuantumProgram @@ -19,9 +18,6 @@ def execute_testbook_with_timeout( def inner_decorator(func: Callable) -> Any: notebook_path = resolve_notebook_path(notebook_name) - if not should_test_notebook(notebook_path): - pytest.skip("Skipped") - for decorator in [ testbook(notebook_path, execute=True, timeout=timeout_seconds), _build_cd_decorator(notebook_path), diff --git a/tests/utils/utils_for_tests.py b/tests/utils/utils_for_tests.py index 596e1b26..9682d334 100644 --- a/tests/utils/utils_for_tests.py +++ b/tests/utils/utils_for_tests.py @@ -1,8 +1,9 @@ import os +from functools import lru_cache from collections.abc import Iterable from pathlib import Path -ROOT_DIRECTORY = Path(__file__).parents[1] +ROOT_DIRECTORY = Path(__file__).parents[2] def iterate_notebooks() -> Iterable[str]: @@ -14,17 +15,22 @@ def iterate_notebooks() -> Iterable[str]: return notebooks_to_test +@lru_cache def _get_all_notebooks(directory: Path = ROOT_DIRECTORY) -> Iterable[str]: - for root, _, files in os.walk(directory): - for file in files: - if file.endswith(".ipynb"): - yield os.path.join(root, file) + return [ + file + for root, _, files in os.walk(directory) + for file in files + if file.endswith(".ipynb") + ] -def should_test_notebook(notebook_path: str) -> bool: +def should_skip_notebook(notebook_name: str) -> bool: + notebook_path = resolve_notebook_path(notebook_name) return notebook_path in list(iterate_notebooks()) +@lru_cache def resolve_notebook_path(notebook_name: str) -> str: notebook_name_lower = notebook_name.lower() if not notebook_name_lower.endswith(".ipynb"): @@ -34,3 +40,4 @@ def resolve_notebook_path(notebook_name: str) -> str: for file in files: if file.lower() == notebook_name_lower: return os.path.join(root, file) + raise LookupError(f"Notebook not found: {notebook_name}")