logo logo

Create Behavior-Driven Python Tests using Pytest-BDD

BDD

This tutorial will make web UI testing easy. We will build a simple yet robust web UI test solution using Python, pytest, and Selenium WebDriver. We will learn strategies for good test design as well as patterns for good automation code. By the end of the tutorial, you’ll be a web test automation champ! Your Python test project can be the foundation for your own test cases, too.

šŸ“ If you are looking for a single Python Package for Android, iOS and Web Testing – there is also an easy open source solution provided by TestProject. With a single executable, zero configurations, and familiar Selenium APIs, you can develop and execute robust Python tests and get automatic HTML test reports as a bonus! All you need is: pip install testproject-python-sdk. Simply follow this Github link to learn more about it, or read through this great tutorial to get started.

Tutorial Chapters

  1. Web UI Testing Made Easy with Python, Pytest and Selenium WebDriver (Overview)
  2. Set Your Test Automation Goals (Chapter 1)
  3. Create A Python Test Automation Project Using PytestĀ (Chapter 2)
  4. Installing Selenium WebDriver Using Python and Chrome (Chapter 3)
  5. Write Your First Web Test Using Selenium WebDriver, Python and Chrome (Chapter 4)
  6. Develop Page Object Selenium Tests Using PythonĀ (Chapter 5)
  7. How to Read Config Files in Python Selenium Tests (Chapter 6)
  8. Take Your Python Test Automation To The Next LevelĀ (Chapter 7)

Behavior-Driven Development is a popular software methodology that defines and implements software features as “behaviors”. Teams define behaviors using a specification language like Gherkin, or Given-When-Then scenarios, before writing any code. Writing behaviors in plain language with helpful examples makes the features much easier to understand. Then, the Gherkin scenarios can be automated using special test frameworks. Each step has a function or method in a language like Python to execute it. BDD helps teams develop better software through improved collaboration and automation.

Python offers several BDD test frameworks, all of which are similar to Cucumber. Personally, I recommend pytest-bdd because it integrates with pytest. We could write our DuckDuckGo search test in Gherkin like this:

Feature: DuckDuckGo Web Browsing

  Scenario: Basic DuckDuckGo Search
    Given the DuckDuckGo home page is displayed
    When the user searches for "panda"
    Then results are shown for "panda"

Please check out this guide, Python Testing 101: pytest-bdd, to learn more. I also recommend learning about theĀ Screenplay PatternĀ for behavior-driven improvements to the Page Object Pattern.

 

About the author

AutomationPanda

Andy Knight is the “Automation Panda” – an engineer, consultant, and international speaker who loves all things software. He specializes in building robust test automation systems from the ground up. Read his tech blog at AutomationPanda.com, and follow him on Twitter at @AutomationPanda.

Leave a Reply

FacebookLinkedInTwitterEmail