What is Pytest Testing Framework?

The Pytest testing framework is used to create a robust automation framework in Python. Just like TestNG and Junit frameworks in Java, the Pytest framework gives a lot of features.

First of all we shall install Pytest in our machine. To install we have to run the command pip install pytest from the command line to configure Pytest in the Python virtual environment. Once done, we can confirm Pytest successful installation by running the command pytest –version command.

 

The Pytest version that gets installed shall be displayed.

Now let us create a new package for our Pytest framework then create a Python file inside that package. As a Pytest convention, the test file should have a name starting with test_ or ending with _test keyword to consider it under the Pytest test framework.

As per Pytest framework standards all the test cases should be written inside a method and the method name should start with test_ keyword.

Conclusion: Thus we have seen what is a Pytest and its installation. For more details, you can refer to the link:

https://courses.rahulshettyacademy.com/courses/learn-selenium-automation-in-easy-python-language/lectures/13248476

In the next section we shall discuss how to execute a Pytest file from the PyCharm editor.

To run Pytest file from PyCharm editor

In the PyCharm editor, click on Edit Configurations, Run/Debug configurations window shall open. Then click on + button.

After selecting pytest from Python tests, browse the path of the Pytest file we have just created under the Target field and then click on Apply.

Now the Pytest test runner will be ready and we can run it by clicking on the green icon.

Let us write our first code following Pytest framework standards.

def test_p():

    print("Hello")

Output:

The output has Hello printed along with test coverage of 100% and test result as PASSED.

Conclusion: Thus we have seen how to run Pytest from PyCharm editor. For more details, you can refer to the link:

https://courses.rahulshettyacademy.com/courses/learn-selenium-automation-in-easy-python-language/lectures/13248476

In the next section, we shall discuss how to execute a Pytest file from Terminal with different flags.

To run Pytest file from command line

For running the Pytest file from the command line, we should first copy the path of the Pytest file and navigate to that path and then run the command py.test. This command shall execute all the Pytest files that are created under that package following the Pytest framework standards.

Here, we don’t find much of the information as we get in the console. To get more details we shall run the command py.test –v, the letter v stands for metadata information.

The output shows the test coverage of 100%, test result as PASSED, cached directory information and so on. However it does not still show the console logs.

To get the console logs we shall run the command py.test –v –s, the letter s stands for logs in output.

The output has Hello printed along with the test result as PASSED.

Let us add one test method in the test_p.py file.

# test method 1

def test_p():

    print("Hello")

# test method 2

def test_p():

    print("Good Morning")

Let us create another test_p1.py file and add the below code.

# test method

def test_p():

    msg "Hello"

    # assertion validation

    assert msg == "Tests fail because did not string match"

Let us execute three test methods inside two Pytest files with the command py.test –v –s.

The output shows two test cases got executed but there are actually three test cases. This is because the test_p.py file contains two methods having the same name as test_p. As per Pytest framework, multiple methods should not have the same name. If there are multiple methods with the same name, then the previous result gets overridden.

Now let us rename the methods in the test_p.py Pytest file.

# test method 1

def test_p():

    print("Hello")

# test method 2 with different name

def test_project():

    print("Good Morning")

The test_p1.py file contains the below code.

 # test method

def test_p():

    msg "Hello"

    # assertion validation

    assert msg == "Tests fail because did not string match"

Let us again execute three test methods inside two Pytest files with the command py.test –v –s.

The output now shows three test cases got executed along with the console output. Thus all the test cases within the same package got executed in one short.

Conclusion: Thus we have seen how to execute Pytest files from Terminal with different flags. For more details, you can refer to the link:

https://courses.rahulshettyacademy.com/courses/learn-selenium-automation-in-easy-python-language/lectures/13248478

In the next section we shall discuss how to execute selected Pytest files from the set of Tests.

To run selected test files using Pytest

Let us add one more test method in the test_p1.py file.

           # test method 1

def test_p():

    msg "Hello"

    # assertion validation

    assert msg == "Tests fail because did not string match"

           # test method 2

def test_secondp():

    a = 2

    # assertion validation

    assert a+2 == 4, "Addition pass"

Now if we want to run a specific file we have to mention that file name. The command is py.test <filename>. So here we should run the command py.test test_p1.py –v –s.

The output shows two test cases got executed along with the console output.

Now let us consider that our Pytest files contain test cases belonging to a particular module called CreditCard and accordingly we will give meaningful names to the corresponding test methods.

The test_p1.py file now contains the below methods.

           # test method 1

def test_firstprogram():

    msg "Hello"

    # assertion validation

    assert msg == "Tests fail because did not string match"

# test method 2 belong to Credit Card module

def test_secondpCreditCard():

    a = 2

    assert a+2 == 4, "Addition pass"

The test_p.py file contains the below methods.

           # test method 1

def test_firstprogram():

    print("Hello")

# test method 2 belong to Credit Card module

def test_projectCreditCard():

    print("Good Morning")

To trigger the test cases on CreditCard from both the files, we have to run the command py.test –k CreditCard –v –s, the letter k stands for method name execution.

The output shows two test cases having method names containing CreditCard got executed along with the console output. Also, it tells that the two test cases are deselected. Thus we can run only selected test methods from multiple files.

Conclusion: Thus we have seen how to execute selected Pytest files from the set of Tests. For more details, you can refer to the link:

https://courses.rahulshettyacademy.com/courses/learn-selenium-automation-in-easy-python-language/lectures/13248482

In the next section we shall discuss how to group Pytest files to run a selected group of test cases.

To group tests and run selected groups using Pytest

We can group tests and run them with the help of keyword custom mark in the Pytest framework. We have to use the expression @pytest.mark.smoke if we want to group a test method with name smoke. Moreover, we have to include the import pytest package in our code.

Let us now add a mark to our existing files.

The test_p1.py file having method with mark.

            import pytest

            # marking test method with smoke group

@pytest.mark.smoke

# test method 1 belonging to smoke group

def test_firstprogram():

                msg "Hello"

            # assertion validation

    assert msg == "Tests fail because did not string match"

# test method 2

def test_secondpCreditCard():

    a = 2

    assert a+2 == 4, "Addition pass"

The test_p.py file having method with mark.

import pytest

# test method 1 with smoke group

@pytest.mark.smoke

# test method 1 with smoke group

def test_firstprogram():

    print("Hello")

# test method 2

def test_projectCreditCard():

    print("Good Morning")

To trigger the test methods having the mark name smoke from both the files, we have to run the command py.test –m smoke –v –s, the letter m stands for marking a test.

The output shows two test methods with name test_firstprogram having mark smoke got executed along with the console output. Also, it tells that the two test cases are deselected. Thus we can run only a selected group of test methods from multiple Pytest files.

We can skip tests with the keyword skip in the Pytest framework. We have to use the expression @pytest.mark.skip if we want to skip a test method from execution. Moreover, we have to include the import pytest package in our code.

Let us now skip a test method from test_p1.py file.

            import pytest

            # test method 1 with smoke and skip mark

@pytest.mark.smoke

@pytest.mark.skip

def test_firstprogram():

                msg "Hello"

           # assertion validation

    assert msg == "Tests fail because did not string match"

# test method 2

def test_secondpCreditCard():

    a = 2

# assertion validation

    assert a+2 == 4, "Addition pass"

Implementation of test_p.py file.

import pytest

# test method 1 with smoke mark

@pytest.mark.smoke

def test_firstprogram():

    print("Hello")

# test method 2

def test_projectCreditCard():

    print("Good Morning")

Let us execute all test methods inside test_p.py and test_p1.py files with the command py.test –v –s.

The output shows three test cases got executed. Also it displays the SKIPPED for the test method test_firstprogram.

We can exclude a test from test reporting with the keyword xfail in Pytest framework. We have to use the expression @pytest.mark.xfail if we want to execute a test method but don’t want it to be a part of the test result. Moreover, we have to include the import pytest package in our code.

Let us now exclude a test method from reporting in the test_p.py file.

import pytest

           # test method 1 with smoke mark

           @pytest.mark.smoke

def test_firstprogram():

    print("Hello")

# test method 2 with xfail mark

@pytest.mark.xfail

def test_projectCreditCard():

    print("Good Morning")

Implementation of test_p1.py file.

import pytest

# test method 1 with smoke and skip mark

@pytest.mark.smoke

@pytest.mark.skip

def test_firstprogram():

                msg "Hello"

           # assertion validation

    assert msg == "Tests fail because did not string match"

# test method 2

def test_secondpCreditCard():

    a = 2

    assert a+2 == 4, "Addition pass"

Let us execute all test methods inside test_p.py and test_p1.py files with the command py.test –v –s.

The output shows two test cases got executed and one test case got skipped. Also it displays the XPASS for the test method test_projectCreditCard having xfail. That particular method got executed but it is not a part of the test execution result.

Conclusion: Thus we have seen how to group Pytest files to run a selected group of test cases. For more details, you can refer to the link:

https://courses.rahulshettyacademy.com/courses/learn-selenium-automation-in-easy-python-language/lectures/13248484

In the next post, we shall discuss how to use fixtures in Pytest.


Tags


You may also like

Groups attribute in TestNG 

Groups attribute in TestNG 
Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}