Skip to content
MohamedRaslan edited this page Feb 14, 2023 · 20 revisions

Pytest-qatouch is a pytest plugin that automatically reports your pytest execution to your qatouch project.

Hey! 👋 You can check out this video to get started or read the following sections Qatouch integration with pytest

Get started

Let's create a simple pytest project

  • Create a new folder/directory and open your favorite editor "here I am using VSCode"
mkdir pytest-qatouch-example
code ./pytest-qatouch-example
  • [Optional step] Create a new python virtual environment, activate it and update pip with other tools or use your current environment and skip this step
python -m venv env
env/Scripts/activate.bat # For windows or env/Scripts/activate.sh for Linux
python -m pip install -U pip wheel setuptool
pip install pytest pytest-qatouch
  • Create a pytest configuration file with your configurations "Here I will use a pytest.ini file, so create one and add the following configurations"
[pytest]


addopts =
    --verbose


testpaths =
    tests

It just tells pytest to be verbose "log more info" and to look for the tests under the ./tests folder

  • Create a test file "test_pytest_qatuch_plugin.py" under the ./tests folder and add the following code
import pytest

def test_for_testcase_0001():
    # Whatever the things you do here, in the end, it will be a passed test
    assert 1+1+1 == 3


def test_for_testcase_0002():
    # Whatever the things you do here, in the end, it will be a failed test
    assert 1+1+1 == 1


@pytest.mark.parametrize(
    "num1,num2",
    [
        pytest.param(8, 10),
        pytest.param(0, 10)),
        pytest.param(1, 4),
    ],
)
def test_sum_greater_than10(num1, num2):
    assert num1+num2 >= 10
  • Runpytest to make sure everything work fine
pytest

Create a testrun in your qatouch project, and save the needed configurations

  • Log to your qatouch portal and open one of your projects or create a dummy one
  • Create or open a test run with all or some of your test cases
  • In the test run header press the "Config" button test run header
  • Save the configuration values that appear in the webdriver.io or cypress tab as they will be used in the following steps the configurations

ℹ️ Note At this point you know the values for

  • qatouch-subdomain: which is the domain you retrieved for webdriver.io or cypress tab
  • qatouch-api-token: which is the apiToken you retrieved for webdriver.io or cypress tab
  • qatouch-project-key: which is the projectKey you retrieved for webdriver.io or cypress tab
  • qatouch-testrun-key: which is the testRunId you retrieved for webdriver.io or cypress tab

Link your pytest project by your qatouch testrun

  • Open the previously created test file "test_pytest_qatuch_plugin.py" and mark each test by the corresponding test case in your qatouch portal like int he code below
import pytest
from pytest_qatouch import qatouch # need to import this to mark each test and let pytest know each test corresponds to what in qatouch

@qatouch.TR(1) # Which corresponds to the test case TC0001/TR0001 
def test_for_testcase_0001():
    # Whatever the things you do here in the end it will be a passed test
    assert 1+1+1 == 3

@qatouch.TR(2) # Which corresponds to the test case TC0002/TR0002 
def test_for_testcase_0002():
    # Whatever the things you do here in the end it will be a failed test
    assert 1+1+1 == 1


@pytest.mark.parametrize(
    "num1,num2",
    [
        pytest.param(8, 10,marks=qatouch.TR(3)), # Which corresponds to the test case TC0003/TR0003 
        pytest.param(0, 10,marks=qatouch.TR(4)), # Which corresponds to the test case TC0004/TR0004 
        pytest.param(1, 4,marks=qatouch.TR(5)),  # Which corresponds to the test case TC0005/TR0005 
    ],
)
def test_sum_greater_than10(num1, num2):
    assert num1+num2 >= 10

ℹ️ Note In my case I will link the following test cases from my qatouch portal the test cases

Create a pytest configuration file with your configurations "Here I will use a pytest.ini file, so create one and add the following configurations"

[pytest]

addopts =
    --verbose
    --qatouch= <True/False>
    --qatouch-subdomain= <YourDomain>
    --qatouch-api-token= <YourToken>
    --qatouch-project-key= <YourProjectKey>
    --qatouch-testrun-key= <YourTestRunKey>


testpaths =
    tests

Or you can do the following

[pytest]

addopts =
    --verbose
    --qatouch= <True/False>
    --qatouch-subdomain= <YourDomain>
    --qatouch-api-token= <YourToken>
    --qatouch-project-key= <YourProjectKey>
    --qatouch-testrun-key= <YourTestRunKey>

testpaths =
    tests

ℹ️ Note You can override any value in the configuration file while running pytest from the cli like for example If enabled qatouch and want to disable it or change the default testrun-key you add in the configuration file you can do like the following

  • pytest --qatouch=False
  • pytest --qatouch-testrun-key=<AnotherTestRunKey>

Run pytest 🎉

Just run pytest in your terminal, and after it finishes it should report your execution to your qatouch portal

Some final notes

  • If you want to have multiple configurations (different combinations of configurations for different purposes ), you need to create differently file for each one and pass that file to pytest while running from the cli, like the following
pytest -c .\qatouch-config.ini # or any-custom-file.ini