diff options
-rw-r--r-- | tests/conftest.py | 33 | ||||
-rw-r--r-- | tests/test_utils.py | 9 | ||||
-rw-r--r-- | tests/utils.py | 11 |
3 files changed, 37 insertions, 16 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index 20926024..9dc37d38 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -17,7 +17,7 @@ import pytest from cryptography.hazmat.backends import _available_backends -from .utils import check_backend_support, select_backends +from .utils import check_backend_support, select_backends, skip_if_empty def pytest_generate_tests(metafunc): @@ -25,22 +25,29 @@ def pytest_generate_tests(metafunc): selected_backends = select_backends(names, _available_backends()) if "backend" in metafunc.fixturenames: - metafunc.parametrize("backend", selected_backends) + filtered_backends = [] + for backend in selected_backends: + try: + required = metafunc.function.requires_backend_interface + except AttributeError: + # function does not have requires_backend_interface decorator + filtered_backends.append(backend) + else: + required_interfaces = tuple( + mark.kwargs["interface"] for mark in required + ) + if isinstance(backend, required_interfaces): + filtered_backends.append(backend) + + # If you pass an empty list to parametrize Bad Things(tm) happen + # as of pytest 2.6.4 when the test also has a parametrize decorator + skip_if_empty(filtered_backends, required_interfaces) + + metafunc.parametrize("backend", filtered_backends) @pytest.mark.trylast def pytest_runtest_setup(item): - required = item.keywords.get("requires_backend_interface") - if required is not None and "backend" in item.funcargs: - required_interfaces = tuple( - mark.kwargs["interface"] for mark in required - ) - if not isinstance(item.funcargs["backend"], required_interfaces): - pytest.skip("{0} backend does not support {1}".format( - item.funcargs["backend"], - ", ".join(iface.__name__ for iface in required_interfaces) - )) - check_backend_support(item) diff --git a/tests/test_utils.py b/tests/test_utils.py index 6c8d088b..8bb8422c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -32,7 +32,7 @@ from .utils import ( load_fips_ecdsa_key_pair_vectors, load_fips_ecdsa_signing_vectors, load_hash_vectors, load_kasvs_dh_vectors, load_nist_vectors, load_pkcs1_vectors, load_rsa_nist_vectors, load_vectors_from_file, - raises_unsupported_algorithm, select_backends + raises_unsupported_algorithm, select_backends, skip_if_empty ) @@ -82,6 +82,13 @@ def test_select_two_backends(): assert selected_backends == [b1, b2] +def test_skip_if_empty(): + with pytest.raises(pytest.skip.Exception): + skip_if_empty([], [FakeInterface]) + + skip_if_empty(["notempty"], [FakeInterface]) + + def test_check_backend_support_skip(): supported = pretend.stub( kwargs={"only_if": lambda backend: False, "skip_message": "Nope"} diff --git a/tests/utils.py b/tests/utils.py index bc5bc1ea..acc6c141 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -40,8 +40,6 @@ def select_backends(names, backend_list): if names is None: return backend_list split_names = [x.strip() for x in names.split(',')] - # this must be duplicated and then removed to preserve the metadata - # pytest associates. Appending backends to a new list doesn't seem to work selected_backends = [] for backend in backend_list: if backend.name in split_names: @@ -55,6 +53,15 @@ def select_backends(names, backend_list): ) +def skip_if_empty(backend_list, required_interfaces): + if not backend_list: + pytest.skip( + "No backends provided supply the interface: {0}".format( + ", ".join(iface.__name__ for iface in required_interfaces) + ) + ) + + def check_backend_support(item): supported = item.keywords.get("supported") if supported and "backend" in item.funcargs: |