blob: fd61803939a5f808ec3c391ddc777ed5e101ab2f [file] [log] [blame]
Alex Gaynorc37feed2014-03-08 08:32:56 -08001# Licensed under the Apache License, Version 2.0 (the "License");
2# you may not use this file except in compliance with the License.
3# You may obtain a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS,
9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
10# implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14from __future__ import absolute_import, division, print_function
15
Paul Kehrer7e4bc6d2013-12-24 22:23:53 -060016import pytest
17
Alex Stapleton01de3ef2014-03-16 16:42:47 +000018from cryptography.hazmat.backends import _available_backends
Alex Gaynor7aab8b42014-10-23 11:01:25 -070019
20from .utils import check_backend_support, select_backends
Paul Kehrer34c075e2014-01-13 21:52:08 -050021
22
23def pytest_generate_tests(metafunc):
Paul Kehrerc421e632014-01-18 09:22:21 -060024 names = metafunc.config.getoption("--backend")
Alex Stapleton01de3ef2014-03-16 16:42:47 +000025 selected_backends = select_backends(names, _available_backends())
Paul Kehrer7e4bc6d2013-12-24 22:23:53 -060026
Paul Kehreraed9e172014-01-19 12:09:27 -060027 if "backend" in metafunc.fixturenames:
Paul Kehrerf93d8242014-10-23 12:07:20 -070028 filtered_backends = []
29 for backend in selected_backends:
30 try:
31 required = metafunc.function.requires_backend_interface
Paul Kehrerdaefd3f2014-10-24 07:48:37 -070032 except AttributeError:
33 # function does not have requires_backend_interface decorator
34 filtered_backends.append(backend)
35 else:
Paul Kehrerf93d8242014-10-23 12:07:20 -070036 required_interfaces = tuple(
37 mark.kwargs["interface"] for mark in required
38 )
39 if isinstance(backend, required_interfaces):
40 filtered_backends.append(backend)
Paul Kehrerf93d8242014-10-23 12:07:20 -070041
42 if not filtered_backends:
43 pytest.skip(
44 "No backends provided supply the interface: {0}".format(
45 ", ".join(iface.__name__ for iface in required_interfaces)
46 )
47 )
48 else:
49 metafunc.parametrize("backend", filtered_backends)
Paul Kehrer7e4bc6d2013-12-24 22:23:53 -060050
51
52@pytest.mark.trylast
53def pytest_runtest_setup(item):
Paul Kehrer60fc8da2013-12-26 20:19:34 -060054 check_backend_support(item)
Paul Kehrer34c075e2014-01-13 21:52:08 -050055
56
57def pytest_addoption(parser):
58 parser.addoption(
59 "--backend", action="store", metavar="NAME",
60 help="Only run tests matching the backend NAME."
61 )