From be3a5736f69dffa770c362c136a99d5688283f71 Mon Sep 17 00:00:00 2001 From: Stefaan Lippens Date: Mon, 22 Jan 2024 17:56:57 +0100 Subject: [PATCH] Issue #30 Centralize all run options in a plugin --- conftest.py | 2 +- .../lib/backend_under_test.py | 28 ++------ src/openeo_test_suite/lib/pytest_plugin.py | 67 +++++++++++++++++++ src/openeo_test_suite/tests/conftest.py | 34 ---------- 4 files changed, 74 insertions(+), 57 deletions(-) create mode 100644 src/openeo_test_suite/lib/pytest_plugin.py diff --git a/conftest.py b/conftest.py index fa4daa8..19c1ac7 100644 --- a/conftest.py +++ b/conftest.py @@ -1,3 +1,3 @@ pytest_plugins = [ - "openeo_test_suite.lib.backend_under_test", + "openeo_test_suite.lib.pytest_plugin", ] diff --git a/src/openeo_test_suite/lib/backend_under_test.py b/src/openeo_test_suite/lib/backend_under_test.py index fdebe2d..e092583 100644 --- a/src/openeo_test_suite/lib/backend_under_test.py +++ b/src/openeo_test_suite/lib/backend_under_test.py @@ -85,30 +85,14 @@ def get_backend_url(config: pytest.Config, required: bool = False) -> Union[str, _backend_under_test: Union[None, _BackendUnderTest] = None -def pytest_addoption(parser): - """Implementation of `pytest_addoption` hook.""" - parser.addoption( - "-U", - "--openeo-backend-url", - action="store", - default=None, - help="The openEO backend URL to connect to.", - ) - - -def pytest_configure(config): - """Implementation of `pytest_configure` hook.""" +def set_backend_under_test(backend: _BackendUnderTest): global _backend_under_test assert _backend_under_test is None - backend_url = get_backend_url(config) - if backend_url is None: - _backend_under_test = NoBackend() - else: - connection = openeo.connect(url=backend_url, auto_validate=False) - _backend_under_test = HttpBackend(connection=connection) + assert isinstance(backend, _BackendUnderTest) + _backend_under_test = backend -def _get_backend_under_test() -> _BackendUnderTest: +def get_backend_under_test() -> _BackendUnderTest: global _backend_under_test assert isinstance(_backend_under_test, _BackendUnderTest) return _backend_under_test @@ -116,9 +100,9 @@ def _get_backend_under_test() -> _BackendUnderTest: @functools.lru_cache def get_collection_ids() -> List[str]: - return _get_backend_under_test().list_collection_ids() + return get_backend_under_test().list_collection_ids() @functools.lru_cache def get_process_ids() -> List[str]: - return _get_backend_under_test().list_process_ids() + return get_backend_under_test().list_process_ids() diff --git a/src/openeo_test_suite/lib/pytest_plugin.py b/src/openeo_test_suite/lib/pytest_plugin.py new file mode 100644 index 0000000..3d414a9 --- /dev/null +++ b/src/openeo_test_suite/lib/pytest_plugin.py @@ -0,0 +1,67 @@ +import argparse + +import openeo + +from openeo_test_suite.lib.backend_under_test import ( + HttpBackend, + NoBackend, + get_backend_url, + set_backend_under_test, +) + + +def pytest_addoption(parser): + """Implementation of `pytest_addoption` hook.""" + parser.addoption( + "-U", + "--openeo-backend-url", + action="store", + default=None, + help="The openEO backend URL to connect to.", + ) + + parser.addoption( + "--process-levels", + action="store", + default="", + help="The openEO process profiles you want to test against, e.g. 'L1,L2,L2A'. Mutually exclusive with --processes.", + ) + parser.addoption( + "--processes", + action="store", + default="", + help="The openEO processes you want to test against, e.g. 'apply,reduce_dimension'. Mutually exclusive with --process-levels.", + ) + + parser.addoption( + "--experimental", + type=bool, + action=argparse.BooleanOptionalAction, + default=False, + help="Run tests for experimental functionality or not. By default the tests will be skipped.", + ) + + parser.addoption( + "--runner", + action="store", + default="skip", + help="A specific test runner to use for individual process tests. If not provided, uses a default HTTP API runner.", + ) + + parser.addoption( + "--s2-collection", + action="store", + default=None, + help="The data collection to test against. It can be either a Sentinel-2 STAC Collection or the name of an openEO Sentinel-2 Collection provided by the back-end.", + ) + + +def pytest_configure(config): + """Implementation of `pytest_configure` hook.""" + backend_url = get_backend_url(config) + if backend_url is None: + backend = NoBackend() + else: + connection = openeo.connect(url=backend_url, auto_validate=False) + backend = HttpBackend(connection=connection) + set_backend_under_test(backend) diff --git a/src/openeo_test_suite/tests/conftest.py b/src/openeo_test_suite/tests/conftest.py index d5509bf..775de9e 100644 --- a/src/openeo_test_suite/tests/conftest.py +++ b/src/openeo_test_suite/tests/conftest.py @@ -13,40 +13,6 @@ _log = logging.getLogger(__name__) -def pytest_addoption(parser): - parser.addoption( - "--experimental", - type=bool, - action=argparse.BooleanOptionalAction, - default=False, - help="Run tests for experimental functionality or not. By default the tests will be skipped.", - ) - parser.addoption( - "--process-levels", - action="store", - default="", - help="The openEO process profiles you want to test against, e.g. 'L1,L2,L2A'. Mutually exclusive with --processes.", - ) - parser.addoption( - "--processes", - action="store", - default="", - help="The openEO processes you want to test against, e.g. 'apply,reduce_dimension'. Mutually exclusive with --process-levels.", - ) - parser.addoption( - "--runner", - action="store", - default="skip", - help="A specific test runner to use for individual process tests. If not provided, uses a default HTTP API runner.", - ) - parser.addoption( - "--s2-collection", - action="store", - default=None, - help="The data collection to test against. It can be either a Sentinel-2 STAC Collection or the name of an openEO Sentinel-2 Collection provided by the back-end.", - ) - - @pytest.fixture(scope="session") def skip_experimental(request) -> bool: """