blob: bdd17fb7083ef06dd30ee5e3c528725f2f112096 [file] [log] [blame]
Alex Gaynor5951f462014-11-16 09:08:42 -08001# This file is dual licensed under the terms of the Apache License, Version
2# 2.0, and the BSD License. See the LICENSE file in the root of this repository
3# for complete details.
Alex Gaynorc37feed2014-03-08 08:32:56 -08004
5from __future__ import absolute_import, division, print_function
6
Paul Kehrer7e4bc6d2013-12-24 22:23:53 -06007import pytest
8
Alex Stapleton01de3ef2014-03-16 16:42:47 +00009from cryptography.hazmat.backends import _available_backends
Alex Gaynor7aab8b42014-10-23 11:01:25 -070010
Paul Kehrer902d8cf2014-10-25 12:22:10 -070011from .utils import check_backend_support, select_backends, skip_if_empty
Paul Kehrer34c075e2014-01-13 21:52:08 -050012
13
14def pytest_generate_tests(metafunc):
Paul Kehreraed9e172014-01-19 12:09:27 -060015 if "backend" in metafunc.fixturenames:
Alex Gaynor3adb8392015-07-12 11:44:04 -050016 names = metafunc.config.getoption("--backend")
17 selected_backends = select_backends(names, _available_backends())
18
Paul Kehrerf93d8242014-10-23 12:07:20 -070019 filtered_backends = []
Alex Gaynor52c15642014-12-18 08:08:36 -080020 required = metafunc.function.requires_backend_interface
Alex Gaynor9bbd4902015-07-01 22:26:28 -040021 required_interfaces = [
Alex Gaynor52c15642014-12-18 08:08:36 -080022 mark.kwargs["interface"] for mark in required
Alex Gaynor9bbd4902015-07-01 22:26:28 -040023 ]
Paul Kehrerf93d8242014-10-23 12:07:20 -070024 for backend in selected_backends:
Alex Gaynor9bbd4902015-07-01 22:26:28 -040025 if all(
26 isinstance(backend, iface) for iface in required_interfaces
27 ):
Paul Kehrerdaefd3f2014-10-24 07:48:37 -070028 filtered_backends.append(backend)
Paul Kehrerf93d8242014-10-23 12:07:20 -070029
Paul Kehrer902d8cf2014-10-25 12:22:10 -070030 # If you pass an empty list to parametrize Bad Things(tm) happen
31 # as of pytest 2.6.4 when the test also has a parametrize decorator
32 skip_if_empty(filtered_backends, required_interfaces)
33
34 metafunc.parametrize("backend", filtered_backends)
Paul Kehrer7e4bc6d2013-12-24 22:23:53 -060035
36
37@pytest.mark.trylast
38def pytest_runtest_setup(item):
Paul Kehrer60fc8da2013-12-26 20:19:34 -060039 check_backend_support(item)
Paul Kehrer34c075e2014-01-13 21:52:08 -050040
41
42def pytest_addoption(parser):
43 parser.addoption(
44 "--backend", action="store", metavar="NAME",
45 help="Only run tests matching the backend NAME."
46 )