def pytest_runtest_setup(item: pytest.Item) -> None: """Called for each test item (class, individual tests). Ensures that altcoin tests are skipped, and that no test is skipped on both T1 and TT. """ if item.get_closest_marker("skip_t1") and item.get_closest_marker( "skip_t2"): raise RuntimeError("Don't skip tests for both trezors!") skip_altcoins = int(os.environ.get("TREZOR_PYTEST_SKIP_ALTCOINS", 0)) if item.get_closest_marker("altcoin") and skip_altcoins: pytest.skip("Skipping altcoin test")
def get_order_number(test: pytest.Item) -> int: test_cls = getattr(test, "cls", None) if test_cls: # Beware, TestCase is a subclass of TransactionTestCase if issubclass(test_cls, TestCase): return 0 if issubclass(test_cls, TransactionTestCase): return 1 marker_db = test.get_closest_marker('django_db') if not marker_db: transaction = None else: transaction = validate_django_db(marker_db)[0] if transaction is True: return 1 fixtures = getattr(test, 'fixturenames', []) if "transactional_db" in fixtures: return 1 if transaction is False: return 0 if "db" in fixtures: return 0 return 2
def get_manifest_renderer_for_item(item: pytest.Item) -> Renderer: """Return the callable for rendering a manifest template. Returns the renderer set via the closest `pytest.mark.render_manifests` marker or `kubetest.manifest.render` if no marker is found. Args: item: The pytest test item. Returns: A callable for rendering manifest templates into YAML documents. """ mark = item.get_closest_marker("render_manifests") return mark.args[0] if mark else render
def check_dcos_min_version_mark(item: pytest.Item) -> None: """Enforces the dcos_min_version pytest annotation, which should be used like this: @pytest.mark.dcos_min_version('1.10') def your_test_here(): ... In order for this annotation to take effect, this function must be called by a pytest_runtest_setup() hook. """ min_version_mark = item.get_closest_marker("dcos_min_version") if min_version_mark: min_version = min_version_mark.args[0] message = "Feature only supported in DC/OS {} and up".format(min_version) if "reason" in min_version_mark.kwargs: message += ": {}".format(min_version_mark.kwargs["reason"]) if dcos_version_less_than(min_version): pytest.skip(message)
def _whitelisted_service_names(item: pytest.Item) -> Set[str]: """Returns a set of whitelisted service names configured by pytest marker diag_service_whitelist, which should be used like this: @pytest.mark.diag_service_whitelist(set('service1', 'service2')) def your_test_here(): ... Note that the diag_service_whitelist marker can be used on function, class, or module to be able to hierarchically configure the whitelist. """ if item.get_closest_marker(name='diag_service_whitelist') is None: return set() whitelisted_service_names: Set[str] = set() for mark in item.iter_markers(name='diag_service_whitelist'): whitelisted_service_names = whitelisted_service_names.union(mark.args[0]) return whitelisted_service_names
def pytest_runtest_setup(item: pytest.Item) -> None: test_set = item.config.getoption("test_set", default=None) if "emulator" in item.fixturenames: if test_set == "parallel" or not item.config.getoption("with_emulator", default=None): pytest.skip("requires datastore emulator") else: # For tests without the emulator, prevent them from trying to create google cloud clients. item.google_auth_patcher = patch("google.auth.default") mock_google_auth = item.google_auth_patcher.start() mock_google_auth.side_effect = AssertionError( "Unit test may not instantiate a Google client. Please mock the appropriate client class inside this test " " (e.g. `patch('google.cloud.bigquery.Client')`).") if item.get_closest_marker("uses_db") is not None: if test_set == "parallel": pytest.skip( "[parallel tests] skipping because test requires database") else: if test_set == "not-parallel": pytest.skip( "[not-parallel tests] skipping because test does not require database or emulator" )