Last active
September 8, 2021 19:11
-
-
Save valeriykurdyayev/270cf708246b6c67f50b03a6fde5e2e0 to your computer and use it in GitHub Desktop.
Skip tests depends of env - CI solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from enumerators import * | |
def pytest_addoption(parser): | |
""" | |
Pytest Hook to capture cmd argument | |
""" | |
parser.addoption( | |
"--env", | |
action="store", | |
dest="environment", | |
choices=( | |
"STAGE2", | |
"DEV2", | |
), | |
default="DEV2", | |
help="", | |
) | |
def pytest_collection_modifyitems(config, items): | |
# get value from argument of --env | |
env = config.getoption("--env") | |
# init a skip mark with description | |
skip_custom_mark = pytest.mark.skip(reason="Env for this test out of scope for this execution") | |
# iterate for all tests | |
for count, item in enumerate(items): | |
# iterate for parametrize makr object | |
for x in item.iter_markers(name="parametrize"): | |
# for env we need only `environment` to avoid parsing another parametrize like `section` etc | |
if x.args[0] == "environment": | |
# env_list is a list with Enums | |
env_list = x.args[1] | |
# env_list[count].title to get a string from enum and compare with cmd argument str | |
if env_list[count].title != env: | |
# assign marker for test | |
item.add_marker(skip_custom_marl) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ENV(Enum): | |
""" | |
This enum provides different envs | |
""" | |
DEV = (1, "DEV") | |
DEV2 = (2, "DEV2") | |
STAGE = (3, "STAGE") | |
STAGE2 = (4, "STAGE2") | |
PROD = (5, "PROD") | |
def __init__(self, id, title): | |
self.id = id | |
self.title = title |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# it is for default execution (optional file for solution | |
addopts = --env DEV2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import allure | |
import pytest | |
from enumerators import * | |
class TestDataClass: | |
@allure.title("Test cases with for {environment}") | |
@pytest.mark.parametrize("section", ["Data"]) | |
@pytest.mark.parametrize("environment", [ENV.STAGE, ENV.DEV]) | |
def test_skip_by_argument(self, section, environment): | |
assert True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment