- Lab
- Core Tech

Guided: Acceptance Testing
This Code Lab guides learners through the process of writing, automating, and executing acceptance tests for a login feature using Python, Selenium, and Behave. Through hands-on tasks, learners will define acceptance criteria, translate them into Gherkin scenarios, and implement the corresponding test automation in Python. By the end of this Code Lab, learners will have gained practical experience in writing and running automated acceptance tests aligned with ISTQB® principles, preparing them to handle real-world testing scenarios.

Path Info
Table of Contents
-
Challenge
Introduction
In this Lab, you will take on the role of a software tester responsible for automating acceptance tests for a web application’s login feature. This lab focuses on guiding you through writing and automating acceptance tests using Python, Selenium, and Behave. You will define acceptance criteria using the Gherkin language, implement test scenarios in Python, and automate browser interactions with Selenium WebDriver.
Throughout the lab, you will set up a Python testing environment, define test cases for both successful and unsuccessful login attempts, and execute the tests to verify outcomes. The goal is to provide hands-on experience with Behavior-Driven Development (BDD) principles and automated acceptance testing.
This lab is structured to simulate real-world scenarios, helping you gain practical experience that will prepare you for similar tasks in a professional environment.
Tip: If you encounter any issues or feel stuck at any point during the lab, solution files are available in the Solutions folder for reference. You can review these files to better understand the expected implementation and correct your work if needed.
-
Challenge
Step 1: Setting Up the Python Test Environment
The first task is to ensure that your Python environment is properly configured to run and automate acceptance tests. You will confirm the installation of Python 3.12.7 as the default version and verify the presence of the necessary Python packages, including Selenium and Behave. This setup will enable you to automate browser interactions and define your acceptance test scenarios using Gherkin syntax.
The required Python files (Python-3.12.7 directory and its source archive, Python-3.12.7.tgz) are already downloaded and can be found in the workspace directory. You will navigate to these files and follow the steps to upgrade Python to version 3.12.7. This approach eliminates the need for additional downloads, allowing you to focus directly on the installation and configuration steps.
Additionally, you will organize the project by setting up the required directory structure and initializing essential files for your test cases. By the end of this step, your environment will be fully prepared for seamless test automation with all dependencies correctly installed. In this task, you will organize your project by creating a directory structure that will help keep your files organized as you implement and automate tests.
-
Challenge
Step 2: Writing the Gherkin Feature File
In this step, you will define the acceptance criteria for the login feature by writing a Gherkin feature file. Gherkin is a plain-text language that describes software behaviors without detailing their implementation. It allows you to create scenarios in a way that is easy for both technical and non-technical stakeholders to understand.
The feature file will outline different test scenarios, including successful and unsuccessful login attempts, which will guide the implementation of your automated tests. By the end of this step, you will have a structured
.feature
file that serves as the foundation for writing and automating acceptance tests.This is an essential step in Behavior-Driven Development (BDD) as it bridges the gap between product requirements and technical implementation.
-
Challenge
Step 3: Implementing Step Definitions in Python
In this step, you will implement the step definitions corresponding to each Gherkin step defined in the login.feature file. Step definitions are written in Python and serve as the bridge between the human-readable Gherkin scenarios and the code that performs the described actions.
Using Selenium, you will automate interactions with the browser, such as entering credentials and verifying login outcomes. Each step definition will map directly to a Gherkin step, allowing you to execute the tests and see the expected behavior.
By the end of this step, you’ll have a set of Python functions that interpret and carry out the scenarios defined in Gherkin, bringing your acceptance tests closer to full automation.
-
Challenge
Step 4: Running the Acceptance Tests
In this step, you will execute the acceptance tests defined in your
login.feature
file using the behave framework. Running these tests will allow you to verify that the application behaves as expected according to the scenarios described in Gherkin syntax. Each scenario will simulate a user interaction with the login feature, and the automated tests will validate whether the outcomes match the acceptance criteria.This step is essential in the test automation process, providing feedback on whether the application meets the defined requirements. By the end of this step, you will be able to identify any issues or inconsistencies in the login functionality and make adjustments as needed to ensure your tests pass successfully. After you run your tests with Behave, take a moment to observe the terminal output. This output provides valuable information about each test step, showing whether it passed, failed, or was skipped. At the end, Behave will display a summary indicating the total number of steps and their statuses, giving you a clear view of the test results.
Example Output:
Here’s an example of what you might see in the terminal when running tests on a real-world application:
Feature: Login functionality # login.feature:1
As a user, I want to be able to log in to the application so that I can access my account. Scenario: Successful login # login.feature:4
Given that the user is on the login page # login_steps.py:3 When the user enters valid credentials # login_steps.py:7 Then the user is redirected to the dashboard # login_steps.py:13
Scenario: Unsuccessful login with incorrect password # login.feature:8
Given that the user is on the login page # login_steps.py:3 When the user enters an incorrect password # login_steps.py:17 Then an error message is displayed indicating invalid credentials # login_steps.py:23
1 feature passed, 0 failed
1 scenario passed, 1 failed
4 steps passed, 2 failed, 0 skipped
Understanding the Output
- Feature and Scenario Names: The output shows the feature (Login functionality) and each scenario (Successful login and Unsuccessful login with incorrect password). This helps you see which functionality is being tested.
- Summary: The summary at the end provides a quick overview: It tells you how many features, scenarios, and steps passed, failed, or skipped. In this example, we have 1 feature that passed, 1 scenario that passed, and 1 that failed. Additionally, 4 steps passed, and 2 failed.
-
Challenge
Step 5: Debugging and Improving Test Stability
In this step, you will focus on troubleshooting any issues identified during test execution and enhancing the stability of your tests. Automated tests can sometimes be unreliable due to factors such as timing issues, dynamic content, or intermittent network delays. By adding error handling and waiting mechanisms, you can improve test reliability and ensure consistent results.
By the end of this step, your tests will be more robust and less likely to fail due to timing or other transient issues, resulting in a more stable test suite. ## Lab Conclusion
With all tasks successfully completed, you have now implemented a functional acceptance testing framework for the login feature using Python and Selenium. By setting up
WebDriverWait
, importing necessary libraries, writing Gherkin scenarios, and implementing step definitions, you've built a robust foundation for automated acceptance testing.Great job! You've developed an understanding of Gherkin syntax and step definitions and gained hands-on experience with Python's Selenium integration for browser automation.
This skill set will be invaluable as you continue to build and test applications. Keep practicing to solidify your understanding further, and don't hesitate to expand upon this foundation with more complex test scenarios.
Congratulations on completing the lab!
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.