blob: 778ee85007811674f0fc3af05b6a15b779b47194 [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 Kehrerc421e632014-01-18 09:22:21 -060015 names = metafunc.config.getoption("--backend")
Alex Stapleton01de3ef2014-03-16 16:42:47 +000016 selected_backends = select_backends(names, _available_backends())
Paul Kehrer7e4bc6d2013-12-24 22:23:53 -060017
Paul Kehreraed9e172014-01-19 12:09:27 -060018 if "backend" in metafunc.fixturenames:
Paul Kehrerf93d8242014-10-23 12:07:20 -070019 filtered_backends = []
20 for backend in selected_backends:
Paul Kehrer8d10a132014-10-25 21:39:35 -070021 required = metafunc.function.requires_backend_interface
22 required_interfaces = tuple(
23 mark.kwargs["interface"] for mark in required
24 )
25 if isinstance(backend, required_interfaces):
Paul Kehrerdaefd3f2014-10-24 07:48:37 -070026 filtered_backends.append(backend)
Paul Kehrerf93d8242014-10-23 12:07:20 -070027
Paul Kehrer902d8cf2014-10-25 12:22:10 -070028 # If you pass an empty list to parametrize Bad Things(tm) happen
29 # as of pytest 2.6.4 when the test also has a parametrize decorator
30 skip_if_empty(filtered_backends, required_interfaces)
31
32 metafunc.parametrize("backend", filtered_backends)
Paul Kehrer7e4bc6d2013-12-24 22:23:53 -060033
34
35@pytest.mark.trylast
36def pytest_runtest_setup(item):
Paul Kehrer60fc8da2013-12-26 20:19:34 -060037 check_backend_support(item)
Paul Kehrer34c075e2014-01-13 21:52:08 -050038
39
40def pytest_addoption(parser):
41 parser.addoption(
42 "--backend", action="store", metavar="NAME",
43 help="Only run tests matching the backend NAME."
44 )