Skip to content

Commit

Permalink
new: Add "When" step to allow skipping of tests depending on environm…
Browse files Browse the repository at this point in the history
…ent variable.
  • Loading branch information
dallinb committed Jul 21, 2022
1 parent 4db4c5e commit 8a9a3d3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
17 changes: 17 additions & 0 deletions testinfra_bdd/when.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""The when steps of testinfra-bdd."""
import os
import pytest
from pytest_bdd import parsers
from pytest_bdd import when
Expand Down Expand Up @@ -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}.')
12 changes: 8 additions & 4 deletions tests/features/example.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions tests/step_defs/test_example.py
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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.')

0 comments on commit 8a9a3d3

Please sign in to comment.