Setting up a Pytest automated tests project

Setting up a Pytest automated tests project

This article is part of a series of blog posts for Streamlining test automation with TestRail, which aim to guide you through centralizing your test automation efforts in TestRail, as per the diagram below.

yRKAcljIqrwgc8 Fn7B8jGcBRmAT0dzaZN7bg9Jed9viBqDcvEzUoKe2Sa2O4wQ sazU5dP5ii4kfg 5iUHj8zi2pSEgTVAiSbO4TSrLG4Bq XzbEw097KJvl79uHOPZuPcvNQuwZ5RyuBQAstb06gQ

You can use dozens of different test automation frameworks to automate your test cases and each framework has its own characteristics and can be specialized in one or more types of testing. For this particular workflow, our only requirement is that the framework can generate a JUnit style report that we can use to upload our test results to TestRail. In our example, we’ll be using Pytest, a Python testing framework that makes it easy to write small, readable tests, and can scale to support complex functional testing for applications and libraries.

By the end of this article, you should be able to achieve steps 2 and 3 on the diagram, with the caveat of being on your local machine instead of Jenkins, but that is part of the process to achieve our end goal, so let’s keep it simple for now.

Setting up your local environment

To install and run a simple Pytest project, all you need is Python to get started. To install Python on your local machine, we recommend you download the Python 3.10.x distribution that matches your operating system and follow the install wizard instructions. To make sure the install was successful, try executing the commands python --version and pip --version from your command line and they should output their versions.

lbBiRRLqlHzsLFLMttm7ZQKd5gJdQICzKUEzHbZE4 h4kcEM aQew38ECWk7ZteeF8YpgFSNi6QVcr5UhPH7T6ddRqIRbrf9CBwIhiRS4LQ6gbErrqsgHaEB5KfgReVHX9hkDiGd3IRDKMrw4drbODQ

Installing and running the sample Pytest project

We have a simple sample project you can clone or download from our Automation Frameworks Integration GitHub repository. The test files live in the tests folder and, by opening the test_sum.py file, you can see very simple tests that add numbers and assert the expected result.

import pytest

def test_sum_two_numbers():
    assert 1 + 1 == 2

def test_sum_two_decimals():
    assert 0.8 + 0.3 == 1.2

@pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6)])
def test_sum_multiple_numbers(test_input, expected):
    assert eval(test_input) == expected

To install the project, you simply need to run the command below on your preferred terminal. If you open the requirements.txt file, you’ll notice that the only dependency on this project is actually Pytest, so that’s all you will be installing.

$ pip install -r "requirements.txt"

Finally, to run the tests, execute the command below on your terminal and you should see the execution output in real-time, similar to the image below.

$ pytest "./tests"

vQey2Dwea0Ed87m6xdZjTRFJQIKpQ1KMLNP377i3im56WQi49krfDhVZIRdHZq DQ0ee3I6lq pxmuuCjzgw5W9TVqF9vwderJF Xkvajp148hZfB8DE9maXfOsddL5Q nOOVpL2walhZQS9vDTLQOM

Final result

You have just set up and executed your automated tests project on your local machine and you can execute it as often as you wish and customize it to your needs. The image below depicts the workflow you have achieved so far.

OjLr0YF P9hbmvXIfiAyzgmJit05e5Utrne2b9KCH4pM jR6Grsb7luRotfFbjoQOP2yaAJfnB5i DcFqZtiCpyYMT6FiI0DobQyViyD5sDzcB3rJraMKHCS3P9JOKbyvl45PELajtE1ovmBl5vjrIs

There are a few downsides to only running automated tests on your local machine. One of them is that they may become dependent on your own system configuration and the same execution may be difficult to reproduce on a different machine. Another downside is that the results won’t be visible to others in your organization. To circumvent these two pitfalls, you should use a CI tool, such as Jenkins, to orchestrate the execution of your automated tests in a consistent fashion and publish your test results.

To get to know how to configure Jenkins to execute your automated tests, please see the article referring to step 2 in this series of blog posts – Orchestrating an automated tests project with Jenkins as your CI tool.

In This Article:

Try a 30-day trial of TestRail today!

Share this article

Other Blogs

integration testing
Agile, Automation, Integrations, Software Quality

Integration Testing: How to Get it Right

When different software modules come together, things don’t always go smoothly. Miscommunication, data mismatches, and other issues can creep in, making the application unreliable and harder to debug. That’s where integration testing steps in. It ensures that ...
Automated Testing: When, Why, and How
Automation

Automated Testing: When, Why & How 

Modern software development demands speed and reliability, and testing is at the heart of delivering high-quality applications. As software scales in complexity, ensuring functionality across different environments, configurations, and updates becomes increasi...
Top QA Automation Tools for 2025
Software Quality, Agile, Automation

Top QA Automation Tools for 2025

In today’s fast-paced development world, QA automation tools are invaluable for delivering high-quality software quickly and efficiently. These tools streamline repetitive tasks, enhance test coverage, and provide actionable insights for developers and QA engi...