8 Useful pytest Plugins for Python Automation
Paulo Oliveira
Posted On: November 5, 2024
169868 Views
14 Min Read
When test cases get complex, or there’s a need to handle a lot of test data, plugins come in handy by making tests more concise and structured. Python, with its simplicity and versatility, is a popular choice for test automation, and pytest is one of the most widely used testing frameworks. While pytest is powerful on its own, its true potential is unlocked through plugins.
pytest plugins can help by providing focused solutions for specific needs—like running parallel tests, managing databases, improving error reporting, and integrating with CI/CD pipelines. They simplify your test process, making it faster and more reliable while also reducing manual efforts and the chance for errors.
In this blog, let’s explore some of the useful pytest plugins for Python automation testing.
TABLE OF CONTENTS
How to Install pytest Plugins?
To install pytest plugins, you can use pip to add plugins from the Python Package Index (PyPI). Here are the following steps:
- Find the plugin you want on PyPI or in the pytest plugin directory.
- Use pip to install it directly in your environment:
- For example, to install pytest-check (a popular plugin for implementing assertions), run the below-given command:
1 |
pip install <plugin-name> |
1 |
pip install pytest-check |
After that, you can import the installed plugin into your test suites to perform pytest testing.
Run pytest tests across 3000+ real browsers. Try LambdaTest Now!
8 Top pytest Plugins
Let’s explore some of the top pytest plugins along with examples.
pytest-check
pytest-check is a plugin that introduces advanced assertion capabilities. While pytest provides a robust set of assertions out of the box, the pytest-check plugin goes above and beyond by enabling parameterized testing and enhancing the reporting of failed assertions.
With pytest-check, you can dynamically generate and execute multiple test cases from a single test function. This is particularly valuable for testing a range of input values or scenarios without the need for repetitive test scripts.
To install this pytest plugin, run the following command:
1 |
pip install pytest-check |
Example:
In this test_pytest_check.py file, we have implemented assertions with the pytest-check plugin. This focus is on using the pytest-check plugin to enhance assertion handling within Selenium tests.
The test function, named test_1, showcases how dynamic assertions can be implemented for a range of user messages. To begin, a list of messages is defined, representing different scenarios that the test will simulate. The WebDriver locates specific elements on the web page, including an input field, a button, and a message element.
During the iteration through the list of messages, the input field is cleared and populated with each message. The button is then clicked to trigger an action, and the pytest-check plugin is used to perform dynamic assertions.
1 2 3 4 |
with check: assert len(message_element.text) > 50 assert message_element.text == message time.sleep(3) |
Here, the test ensures that the content of the displayed message is more than 50 characters and that it matches the input message. The addition of time.sleep(3) introduces a brief pause to allow for the response to be rendered on the web page before moving to the next iteration.
The validation of displayed message size is intentionally used here to demonstrate how the pytest-check plugin works in case of failures. When running the above code, if we use a common assert, the test will fail in the first interaction. However, using the pytest-check plugin, the test will never fail, and an insightful report is provided in order to help you understand all the failures.
In the below output, the pytest-check report is generated, containing the following details:
- The test did not pass.
- Three checks encountered failures.
- Details of each failed check.
pytest-bdd
pytest-bdd is a plugin that seamlessly integrates Behavior-Driven Development (BDD) principles into your test automation workflow. BDD is an approach to software development that emphasizes collaboration and communication between stakeholders.
To install this pytest plugin, run the following command:
1 |
pip install pytest-bdd |
Example:
To automate interactions with a simple form using the pytest-bdd plugin, you’ll need to create a feature file that outlines the specific scenarios you want to test. In this case, the feature file should be structured as follows:
1 2 3 4 5 6 |
Feature: Simple Form Demo Scenario: Entering a message Given the user is on the Simple Form Demo page When the user enters the message "My pytest-bdd message" in the input field And clicks the Get button Then the user should see the message "My pytest-bdd message" |
Ensure that you organize this feature file within a directory named features in your project’s root directory.
In this test_simple_form_demo.py file, we define step functions using the @given, @when, and @then decorators, which are provided by the pytest-bdd plugin. Each step function corresponds to a line in your Gherkin scenario, making it easy to map your feature file’s actions to actual Python code.
Additionally, the step definitions leverage powerful parsing capabilities using the parsers.parse() method to dynamically extract values from the Gherkin scenarios.
In this code snippet, we begin by importing the necessary modules, including By for element location strategies and webdriver for browser automation. The @given, @when, and @then decorators are used to define step functions.
The scenarios() function is used to link the step definitions with the feature file, enabling the pytest-bdd plugin to automatically recognize and execute the defined scenarios. A fixture named driver is set up using pytest.fixture, providing a ChromeDriver.
The step functions correspond to the actions described in the Gherkin scenarios. They interact with elements on the web page using the find_element() method and perform actions like sending keys and clicking buttons.
Finally, assertions are made to validate that the expected behavior matches the actual outcome.
pytest-xdist
pytest-xdist is a plugin that provides the capability to run tests in parallel across multiple machines. This enables a substantial reduction in test execution time, making it an invaluable pytest plugin for projects with large test suites.
To install this pytest plugin, run the following command:
1 |
pip install pytest-xdist |
Once installed, the pytest-xdist plugin seamlessly integrates with your existing pytest setup. To execute tests in parallel, you can use the -n flag followed by the number of parallel processes you’d like to run.
1 |
pytest -n 2 |
Example:
In this test_basic.py file, we have a suite of Selenium tests. We’ll create a sample test suite and showcase how the pytest-xdist plugin can reduce the overall execution time.
Other than this Python file we will create two more files exactly with the same content, just changing the name. Our three Python automation scripts are test_basic_1.py, test_basic_2.py, and test_basic_3.py
Let’s run these test scripts using the pytest command.
In the above terminal output, you can see that the three tests were run and passed, and the execution time was 16.68 seconds.
Now, let’s run the same tests using pytest-xdist capabilities. The below command will run tests in three separate processes concurrently:
1 |
pytest -n 3 |
In the above terminal output, you can see that, as before, the three tests were run and passed. However, the execution time was 7.08 seconds, a reduction of almost 58% in the test suite execution time.
While parallel test execution significantly speeds up the testing process, it’s important to monitor and manage the results effectively. The pytest-xdist plugin provides clear and concise reports that aggregate the outcomes of all parallel processes.
Additionally, you can leverage extensive reporting options of pytest, such as pytest-html, in conjunction with the pytest-xdist plugin. It allows you to generate comprehensive reports that offer insights into test coverage, performance, and any failures encountered during parallel execution.
For more details, refer to this blog on how to run parallel pytest tests.
pytest-randomly
The pytest-randomly plugin introduces randomness into the execution order of your tests, providing a fresh perspective on the behavior of the web application. Running tests in random order helps uncover hidden dependencies or state issues in your test suite.
To install this pytest plugin, run the following command:
1 |
pip install pytest-randomly |
Once installed, the pytest-randomly plugin seamlessly integrates with your existing pytest setup. You can enable random test order by including the –randomly flag when running the following command:
1 |
pytest --randomly-seed=1234 |
The command initiates pytest with the pytest-randomly plugin, using a specified seed value of 1234. This seed value ensures consistent randomization results across multiple test runs, facilitating reproducibility for debugging and result sharing.
So, if you want to reproduce the same execution order, you can use the same seed. If you want to change the order, just change the seed on each run so the tests will be executed in a different sequence.
Example:
To showcase how the pytest-randomly plugin works, we will use the same code and the same project structure presented in the pytest-xdist section.
Let’s run these tests: test_basic_1.py, test_basic_2.py, and test_basic_3.py using the pytest command.
In the below terminal output, you can see that the three tests were run and passed, and the execution order was test_basic_1, test_basic_2, and test_basic_3.
If you run the same tests again, the execution order will be the same.
Now, let’s run the same tests using the pytest-randomly plugin. The below command will run the tests:
1 |
pytest --randomly-seed=1234 |
In the above terminal output, you can see that the three tests were run and passed as before. However, the execution order was test_basic_1, test_basic_3, and test_basic_2.
If you run the same tests again, changing the seed to 9999, the execution order will change.
In the above terminal output, you can see that, as before, the three tests were run and passed. However, the execution order was test_basic_2, test_basic_1, and test_basic_3.
pytest-html
The pytest-html plugin provides a powerful solution for creating interactive HTML reports that enhance the visibility of your test results.
To install this pytest plugin, run the following command:
1 |
pip install pytest-html |
Once installed, the pytest-html plugin seamlessly integrates with your existing pytest setup. After running your tests with pytest, the plugin automatically generates HTML reports, providing a detailed summary of the test outcomes.
Example:
To showcase how the pytest-html works, we will use the same code and the same project structure presented in the pytest-xdist section.
Let’s now run these tests test_basic_1.py, test_basic_2.py, test_basic_3.py using the below command:
1 |
pytest --html=report.html |
If you check the project folder, you will notice that the report.html file was created, and an assets folder with a style.css file was also added to the project folder.
If you open the report.html file in the browser, you can see:
The pytest-html plugin also offers a wealth of customization options that empower you to finely tailor the appearance and content of the generated HTML reports, aligning them with your unique testing needs and preferences.
pytest-datafiles
The pytest-datafiles plugin simplifies the test management process by seamlessly integrating external data files, such as JSON, YAML, or text files, into your Selenium tests.
To install this pytest plugin, run the following command:
1 |
pip install pytest-datafiles |
Example:
To demonstrate how the pytest-datafiles plugin works, let’s create a sample data file named test_data.json file, with some messages that will be used in our demonstration:
1 2 3 4 5 |
[ {"message": "Test Case 1"}, {"message": "Test Case 2"}, {"message": "Test Case 3"} ] |
In the test_with_datafiles.py file, the structure of the test script largely remains the same as the previous sections, focusing on automating a simple form demo. However, the key transformation lies within the test_simple_form_demo function.
With the integration of pytest-datafiles, we’re now able to dynamically iterate through a set of scenarios defined in an external JSON file named test_data.json file. This file holds various test cases, each containing a distinct message. For each scenario, we ensure the input field is cleared before typing the new message, enhancing test accuracy.
Let’s now run these tests using the pytest command:
pytest-cov
The pytest-cov plugin seamlessly integrates with pytest, providing an easy way to measure test coverage in your Selenium automation projects.
To install this pytest plugin, run the following command:
1 |
pip install pytest-cov |
Example:
Create the project structure as defined in this pytest-cov-sample repository. Inside the calculator.py file, we will create two simple functions to calculate the addition and subtraction of two numbers.
1 2 3 4 |
def sum(a, b): return a + b def diff(a, b): return a - b |
In the test_calculator.py, we will create a simple test to check some sums. We will intentionally not add any tests for the diff() function.
1 2 3 4 5 |
from myproject.calculator import sum, diff def test_sum(): assert sum(2, 3) == 5 assert sum(-1, 1) == 0 assert sum(0, 0) == 0 |
The __init__.py file is just to tell the pytest-cov plugin where the tests are to be run. Running your tests with coverage analysis is simple. Simply run your tests as you normally will using pytest, but with the additional –cov option and the name of the test folder that is tests:
1 |
pytest --cov=myproject tests |
This command runs your tests and generates a coverage report for your software project. The report will indicate which lines of code have been covered by your tests and which remain untested.
For more information, head over to this blog on how to generate a pytest code coverage report.
pytest-mock
pytest-mock is a plugin that integrates seamlessly with pytest, providing a simple yet robust way to create and manage mocks. Mocks are used in a variety of scenarios, such as when dealing with external dependencies like databases, APIs, or third-party services.
To install this pytest plugin, run the following command:
1 |
pip install pytest-mock |
Example:
In this test_sample.py file, we have a test_network_error_handling(driver, mocker) function that tests the behavior of a Selenium WebDriver when a network error occurs during a navigation attempt.
We use mocker to mock the driver.get method. mocker is an instance of the MockFixture class provided by the pytest-mock plugin. It allows you to create and configure mock objects for testing.
We configure the mock to raise an exception when called, simulating a network error condition. When we attempt to navigate to a web page using the driver.get(“https://www.lambdatest.com/selenium-playground/simple-form-demo”), the mocked driver.get method will raise an exception, simulating a network error. We catch the exception, print it, and assert that the error message contains Network Error.
Let’s now run these tests using the pytest -s command, and you can see the output below:
The tests we ran above are on the local Selenium Grid. However, for large test suites where you need scalability and reliability, you can leverage pytest plugins with cloud-based testing platforms such as LambdaTest.
LambdaTest is an AI-powered test execution platform that enables seamless Python testing on the test automation cloud of 3000+ real browser and OS combinations.
Check out this documentation to get started with pytest testing on LambdaTest.
Conclusion
Harnessing the power of pytest plugins in your test automation process helps you achieve robust, maintainable, and efficient test suites. When pytest is combined with its extensive plugin ecosystem, it empowers testers and developers to tailor their testing frameworks to exact specifications.
By leveraging pytest plugins, you can extend the capabilities of your test automation projects in ways that may not be readily achievable with standard testing frameworks. From specialized assertions to comprehensive reporting and data-driven testing, the pytest plugin ecosystem offers various ways to enhance your testing efforts.
Frequently Asked Questions (FAQs)
What is a plugin in pytest?
A plugin in pytest is an add-on that extends its core functionality, allowing you to add or customize features like new hooks, markers, or fixtures for specific test needs.
What is pytest used for in Python?
pytest is a popular Python testing framework used to write simple to complex test cases, making testing easier with a focus on readability and rich functionality.
What is pytest pluggy?
pluggy is a plugin management library used by pytest to handle plugins, enabling flexible and modular extensions to pytest’s capabilities.
Got Questions? Drop them on LambdaTest Community. Visit now