Installing the PyTest HTML plugin

To generate HTML reports with the Pytest framework we have to install a plugin. For the installation, we shall execute the command pip install pytest-html. Next to generate the report we shall run the command pytest –html=report.html.

The output shows four test cases got passed, one test case skipped, one test case xpassed and one warning. Also a new file named report.html got generated inside the project folder.

Next if we copy the report.html file path and open it in a browser, we shall get the below report:

The report contains information on the Environment on which the execution has taken place with details on the path of the JAVA_HOME, packages we have installed, platform, plugins and the Python version.

It contains the test Summary information like total test cases, execution duration, and number of test cases passed, failed, errors and so on. It also contains the test Result information in details.

Conclusion: Thus we have discussed how to generate HTML reports with Pytest. For more details, you can refer to the link:

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

In the next section we shall discuss how to pass command line options in Pytest.

Passing run time data to the test method from the command line in PyTest

We can pass run time data to the test method from the command line. We have to add a hook in the conftest file which contains the key value pairs to pass options from the command line.

Let us see how to pass command line options in key value pairs in conftest.py.

import pytest

           # declare runtime variable browser_name and storing it

def pytest_addoption(parser):

    parser.addoption(

        "--browser_name", action="store", default="chrome"

    )   

# fixture method

@pytest.fixture()

def setup(request):

    browser_name = request.config.getoption("browser_name")

    if browser_name == "chrome":

         print("Browser value passed from command line: " + browser_name)

    elif browser_name == "firefox":

         print("Browser value passed from command line: " + browser_name)

    yield

         print("Postcondition")

Implementation of actual test scenario in test_fixture.py.

import pytest

# actual test method with fixture method name passed as parameter

def test_commanlinefixture(setup):

    print("Actual scenario on command line options")

To declare and initialize a runtime variable from the command line, we have sent the command line key browser_name from the conftest.py file. Also we have set action="store", since the key browser_name is being stored. In case, we don't send any option from the command line, it shall take the value set in the default parameter. Here we have set the default as chrome.

In this way whatever value we pass from the command line has been registered within the addoption method. To retrieve the command line key value in the fixture test method we have to use the request instance and use the method request.config.getoption("browser_name"). Please note we have passed the key browser_name as a parameter to that method.

Now let us run the command py.test test_fixture.py –browser_name firefox –s.

The output shows that at first Browser value passed from command line: firefox got printed in the console since it is part of the fixture setup method. Please note, we have passed the command line option as firefox hence it got reflected in the console log. Then the Actual test scenario on command line options statement got printed as it is a part of the actual test method test_commanlinefixture. Finally Postcondition got printed as it appears after the yield keyword.

Now let us run the command py.test test_fixture.py –browser_name chrome –s.

The output shows that at first Browser value passed from command line: chrome got printed in the console since it is part of the fixture setup method. Please note, we have passed the command line option as chrome hence it got reflected in the console log. Then the Actual test scenario on command line options statement got printed as it is a part of the actual test method test_commanlinefixture. Finally Postcondition got printed as it appears after the yield keyword.

Conclusion: 

Thus we have discussed how to pass command line options in Pytest. For more details, you can refer to the link:

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

Thus we have come to an end of blog series on Selenium with Python and PyTest framework.


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"}