Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass tags from CLI to Pytest-bdd and execute specific scenarios

Tags(smoke/regression) to be passed from CLI and to be interpreted in conftest.py to execute those scenarios satisfying the tags.

I've looked into the pytest-bdd document here and failed to find the connection.

Scenario Outline has: (as python decorators can be stacked)

@pytest.mark.smoke
Scenario Outline: "VALID" Test

@pytest.mark.smoke
@pytest.mark.regression
  Scenario Outline: "INVALID" Test

@pytest.mark.regression
  Scenario Outline: "MIXED" Test

conftest.py

def pytest_bdd_apply_tag(tag, function):    
    if 'smoke' not in tag:  #what should I use to take values from CLI and execute those
        marker = pytest.mark.skip # skips scenario where 'smoke' is not marked
        marker(function)
        return True
    return None

The above code in conftest.py skips all scenarios. CLI Input:

pytest --env='qa' -m 'smoke'

where pytest_addoption is used for --env='qa' and pytest_bdd_apply_tag for -m.

We want to execute only scenarios that have marked smoke(VALID & INVALID) when I pass smoke; scenarios that have marked regression(INVALID & MIXED) when I pass regression and default smoke when I don't pass any parameter in CLI via -m option.

like image 452
Y5288 Avatar asked Sep 01 '25 11:09

Y5288


1 Answers

My bad, I was wondering how I got mislead by the pytest markers with the scenario tag lines as mentioned in the documentation.

The changes I made on feature file is

@smoke
Scenario Outline: "VALID" Test

@smoke @regression
  Scenario Outline: "INVALID" Test

@regression
  Scenario Outline: "MIXED" Test

I've eliminated the pytest_bdd_apply_tag method from conftest.py.

Giving this in the command line works for me

pytest -m "regression" --env="uat"
like image 166
Y5288 Avatar answered Sep 04 '25 04:09

Y5288