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.