Scenario Outline in PyTest – BDD

HOME

pip install pytest
pip install pytest-bdd
pip install pytest-selenium (If using Selenium)

Feature: Scenario Outline Example

    @InvalidCredentials
    Scenario Outline: Unsuccessful Application Login
        Given User is on OrangeHome page
        When User enters username as "<username>" and password as "<password>"
        Then User should be able to see error message as "Invalid credentials"

      Examples:
       | username | password    |
       | admin    | admin12345  |
       | Admin123 | admin       |
       | 123      | admin       |

import pytest

from pytest_bdd import scenarios, given, when, then, parsers
from selenium import webdriver
from selenium.webdriver.common.by import By


# Constants
TIMEOUT = 5
URL = 'https://opensource-demo.orangehrmlive.com/'


# Scenarios
scenarios('../features/ScenarioOutline.feature')


# Fixtures
@pytest.fixture
def driver():
    driver = webdriver.Firefox()
    driver.implicitly_wait(TIMEOUT)
    driver.maximize_window()
    yield driver
    driver.quit()


# Given Steps
@given('User is on OrangeHome page')
def open_browser(driver):
    driver.get(URL)


# When Steps
@when(parsers.parse('User enters username as "{username}" and password as "{password}"'))
def search_phrase(driver, username, password):
    driver.find_element(By.NAME, "username").send_keys(username)
    driver.find_element(By.NAME, "password").send_keys(password)
    driver.find_element(By.XPATH, "//*[@class='oxd-form']/div[3]/button").click()


# Then Steps
@then(parsers.parse('User should be able to see error message as "{expected_error_message}"'))
def search_results(driver, expected_error_message):
    actual_error_message = driver.find_element(By.XPATH, "//*[@id='app']/div[1]/div/div[1]/div/div[2]/div[2]/div/div[1]/div[1]/p").text
    assert actual_error_message == expected_error_message

scenarios('../features/ScenarioOutline.feature')

pytest stepdefinitions/test_scenario_outline_definitions.py -s

 pytest stepdefinitions/test_scenario_outline_definitions.py --html=Report.html 

One thought on “Scenario Outline in PyTest – BDD

  1. Great and detailed step-by-step tutorial! Scenario Outlines in PyTest-BDD are essential when you need to run data-driven tests without cluttering your feature files.

    For anyone looking to dive deeper into scaling this setup, managing step definitions across larger suites, and integrating test reporting, Testomat.io has an excellent breakdown on using pytest-bdd effectively in modern CI/CD pipelines. Thanks for sharing this guide!

    Like

Leave a comment