diff --git a/setup.cfg b/setup.cfg index 93d4d43..2cb9718 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [coverage:report] -fail_under = 90 +fail_under = 95.77 show_missing = True [coverage:run] diff --git a/testinfra_bdd/when.py b/testinfra_bdd/when.py index af19b69..1545304 100644 --- a/testinfra_bdd/when.py +++ b/testinfra_bdd/when.py @@ -1,4 +1,5 @@ """The when steps of testinfra-bdd.""" +import os import pytest from pytest_bdd import parsers from pytest_bdd import when @@ -38,3 +39,19 @@ def skip_tests_if_system_info_does_not_match(property_name, expected_value, test actual_value = testinfra_bdd_host.get_host_property(property_name) if actual_value != expected_value: pytest.skip(f'System {property_name} is {actual_value} which is not {expected_value}.') + + +@when(parsers.parse('the environment variable {key} is {value} skip tests')) +def skip_tests_if_env_key_is(key, value): + """ + Skip tests if an environment variable is set to a particular value. + + Parameters + ---------- + key : str + The name of the environment variable. + value : str + The value the environment variable must be for the tests to be skipped. + """ + if key in os.environ and os.environ[key] == value: + pytest.skip(f'Environment variable {key} is set to {value}.') diff --git a/tests/features/example.feature b/tests/features/example.feature index d5900c3..b98bdb2 100644 --- a/tests/features/example.feature +++ b/tests/features/example.feature @@ -128,17 +128,21 @@ Feature: Example of Testinfra BDD | udp://123 | listening | | tcp://22 | not listening | + Scenario: Skip Tests Due to Environment Variable + Given the host with URL "docker://java11" is ready + When the environment variable PYTHONPATH is .:.. skip tests + Scenario: Check Network Address Given the host with URL "docker://sut" is ready within 10 seconds - And on GitHub Actions we skip tests - When the address is www.google.com + When the environment variable GITHUB_ACTIONS is true skip tests + And the address is www.google.com Then the address is resolvable And the address is reachable Scenario: Check Network Address With Port Given the host with URL "docker://sut" is ready within 10 seconds - And on GitHub Actions we skip tests - When the address and port is www.google.com:443 + When the environment variable GITHUB_ACTIONS is true skip tests + And the address and port is www.google.com:443 Then the address is resolvable And the address is reachable And the port is reachable diff --git a/tests/step_defs/test_example.py b/tests/step_defs/test_example.py index a94d269..beb8d08 100644 --- a/tests/step_defs/test_example.py +++ b/tests/step_defs/test_example.py @@ -1,10 +1,5 @@ """Examples of step definitions for Testinfra BDD feature tests.""" -import os -import pytest - import testinfra_bdd - -from pytest_bdd import given from pytest_bdd import scenarios scenarios('../features/example.feature') @@ -13,10 +8,3 @@ # Ensure that the PyTest fixtures provided in testinfra-bdd are available to # your test suite. pytest_plugins = testinfra_bdd.PYTEST_MODULES - - -@given('on GitHub Actions we skip tests') -def on_github_actions_we_skip_tests(): - """on GitHub Actions we skip tests.""" - if 'GITHUB_ACTIONS' in os.environ and os.environ['GITHUB_ACTIONS'] == 'true': - pytest.skip('GitHub Actions does not support Ping/ICMP.')