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:

Sign up for our newsletter

Share this article

Other Blogs

Automation, Programming

How to Report On Test Automation (Tools + Metrics)

Test automation streamlines repetitive tasks, accelerates testing processes, and ensures consistent, reliable results in software testing. Reporting on test automation provides actionable insights and visibility into the test outcomes, enabling teams to mak...

Agile, Integrations, Jira

Jira for Test Management: Options, Challenges, and Solutions

Learn about the pros/cons of using Jira as a test management solution, alternatives, & how teams that use Jira manage their testing. 

Software Quality, Integrations, TestRail

How a Document Management Company Streamlined Testing and Boosted Efficiency with TestRail and Reflect

A leading document management company in the document management industry has made significant strides in streamlining digital content access and organization with its cloud-based document management platform. Their suite of software products enables compan...