8 Useful pytest Plugins for Python Automation

Paulo Oliveira

Posted On: November 5, 2024

view count125395 Views

Read time14 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.

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:

  1. Find the plugin you want on PyPI or in the pytest plugin directory.
  2. Use pip to install it directly in your environment:
  3. For example, to install pytest-check (a popular plugin for implementing assertions), run the below-given command:

After that, you can import the installed plugin into your test suites to perform pytest testing.

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:

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.

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

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:

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:

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-bdd

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:

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.

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.

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:

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.

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:

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:

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.

 pytest-randomly

If you run the same tests again, the execution order will be the same.

pytest-randomly plugin

Now, let’s run the same tests using the pytest-randomly plugin. The below command will run the tests:

test_basic

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.

test_basic_1

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:

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:

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:

pytest-html

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:

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:

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-datafiles

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:

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.

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.

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:

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.

coverage report

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:

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:

pytest -s

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.

Author Profile Author Profile Author Profile

Author’s Profile

Paulo Oliveira

Paulo is a Quality Assurance Engineer with more than 15 years of experience in Software Testing. He loves to automate tests for all kind of applications (both backend and frontend) in order to improve the team’s workflow, product quality, and customer satisfaction. Even though his main roles were hands-on testing applications, he also worked as QA Lead, planning and coordinating activities, as well as coaching and contributing to team member’s development. Sharing knowledge and mentoring people to achieve their goals make his eyes shine.

Blogs: 15



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free