Skip to content

Modus-Logo-Long-BlackCreated with Sketch.

  • Services
  • Work
  • Blog
  • Resources

    OUR RESOURCES

    Innovation Podcast

    Explore transformative innovation with industry leaders.

    Guides & Playbooks

    Implement leading digital innovation with our strategic guides.

    Practical guide to building an effective AI strategy
  • Who we are

    Our story

    Learn about our values, vision, and commitment to client success.

    Open Source

    Discover how we contribute to and benefit from the global open source ecosystem.

    Careers

    Join our dynamic team and shape the future of digital transformation.

    How we built our unique culture
  • Let's talk
  • EN
  • FR

Automation Testing with Pytest-BDD and Python3

Published on May 9, 2019
Last Updated on April 23, 2021
Quality Assurance

This is part of the Python Automation Testing blog series. You can review the code from this article on the Python Automation Git repo.

A while back I started working on an Automation Project using Python. Due to some environment constraints I got stuck with Python 2.7.6 and so, all the other libraries were old versions.

A few weeks ago I convinced the client to remove those constraints by choosing another cloud testing platform and switching to the latest Python 3 version and compatible test tools.

In the following post I’ll describe all the information you need to successfully setup Pytest-bdd with Python3. A full tutorial on how to install all dependencies can be found here.

Project Structure

The automation can be set as a folder within the project or as a separate repository. The structure is detailed below.

The test project entry point is conftest.py. This is the pytest configuration file and here you can set directory scope fixtures, external plugins, hooks. I strongly recommend to debug this file and understand the code flow within it. It is important to know how it works in order to improve and adapt this boilerplate proposal to your specific needs.

All project dependencies are listed under requirements.txt file. At the moment of implementation all dependency versions were at their latest. If any new dependency is needed it should be added here and pip install -r requirements.txt should be run.

If you use Testrail or Browserstack please update the .testrailapi and .browserstack files. Also see README.md for details on how they were integrated and how to use them.

Tests are written with governance of BDD principles and are placed under the features folder. It’s important to understand these tags: @automated and @nondestructive. The first marks the tests that are automated and the latter marks that the tests are safe to run in Sensitive Environments.

Page Objects are structured under the page_objects folder. Base_page.py defines the base class from which all the other page object classes are derived from. Another file here is locators.py that stores, as its name suggests, all the locators you use within the app. I recommend this approach because it is easier to manage and scale for big projects and you can also identify duplicate locators and use common locators.

All the tests are placed under the tests folder. There are two files test_assertions.py and test_common.py which are declared as pytest plugins. The reasons behind this is to have better code reusability and to avoid having all the step implementations within the conftest.py. Each pytest_bdd scenario needs to be declared within a pytest test file. Since you can have common steps for different scenarios these have to be accessible and the two easy ways to achieve this are to have them in conftest.py file or in a shared fixture / hooks / plugins file.

Two custom plugins are used, pytest_selenium and pytest_testrail.
Pytest-selenium is a modification of the official pytest-selenium plugin. The main differences are that my custom implementation has support for Appium and for one custom driver implementation. The custom driver implementation comes handy when, for example, you have a project in which the clear() command does not work as expected so you have to override the default behavior. Within the custom_webdriver.py file you can find an example of method overrides that helped me in one of my hybrid testing projects.
Pytest-testrail plugin implements the TestRail API v2 and integrates your tests with TestRail.

Here is an image of the test folder structure:

Test Folder Structure

The full code can be accessed in the github repository here.

Conclusion

From my experience I encourage Test Automation using free and open source Python tools. It can be applied to a large variety of products and technologies. I successfully used it on web, hybrid and mobile web projects and I found it easy and handy. I also found it easy to integrate with a large number of cloud service, cloud testing or cloud reporting providers. But what really made the difference for me was the ease of debugging and the stability of the tests.

I hope I have persuaded you to walk my path and enjoy automation with Python. Stay tuned for more awesome Python automation testing posts.

Ensure your software is secure, reliable, and ready to scale—explore our expert testing and QA services today!

Posted in Quality Assurance
Share this

Sergiu Popescu

Sergiu Popescu is a QA Engineer at Modus Create. He specializes in automating test processes for web and hybrid apps using Java, JS, and a wide range of tools and libraries like TestNG, jUnit, Webdriver, WebdriverJS, Protractor and Siesta. When he is not hunting bugs in apps, he enjoys spending time with his lovely wife and son.

Related Posts

  • Python Automation Testing: Pytest and Pytest-bdd

    This is part of the Python Automation Testing blog series. You can review the code…

  • Test Automation with Python
    Python Automation Testing - innerText vs textContent

    Moving too fast can cause issues in your test automation process, and without attention to…

Want more insights to fuel your innovation efforts?

Sign up to receive our monthly newsletter and exclusive content about digital transformation and product development.

What we do

Our services
AI and data
Product development
Design and UX
IT modernization
Platform and MLOps
Developer experience
Security

Our partners
Atlassian
AWS
GitHub
Other partners

Who we are

Our story
Careers
Open source

Our work

Our case studies

Our resources

Blog
Innovation podcast
Guides & playbooks

Connect with us

Get monthly insights on AI adoption

© 2025 Modus Create, LLC

Privacy PolicySitemap
Scroll To Top
  • Services
  • Work
  • Blog
  • Resources
    • Innovation Podcast
    • Guides & Playbooks
  • Who we are
    • Our story
    • Careers
  • Let’s talk
  • EN
  • FR