pytest +Selenium+Python unittest.TestCase — (1)

Jen
3 min readMay 30, 2021

Source:

In this practice, I used Python and Selenium framework to handle the interaction of test automation, and the target website for the test is pytest tutorial. Finally, the test report was generated by using pytest-html. I generated two test reports to demonstrate how the pytest report looks like when the test fails and the test passes. My project can be accessed via the Github link: https://github.com/yujhenchen/pytest-works

Here are some code snapshots and corresponding explanations:

In the setUp function, I customized webdriver with “options.headless = False”, which is to let the interaction be visible during the runtime of webdriver.

The main test is to click all the items in the Table Of Contents of pytest tutorial website (https://docs.pytest.org/en/6.2.x/contributing.html) by order. One second waiting time per click is included. Also, WebDriverWait is to make sure element appears on the page within a certain time. As for finding elements, I used XPath to locate each element.

pytest-html collects information of test environment, such as Python version and operating system. This helps when there is a need for test representation. The progress of running the test can also be viewed via terminal output. The command to execute pytest and generate test report is as following:

How the test reports look like when the test fails and test passes:

In short, it is difficult to read and understand the information in the default style of the pytest report, so in order for most users (project managers, quality assurance teams, software engineers, etc.) to understand the test results, it needs to customize the report with a certain style.

In addition, how to design a test by breaking the function into small pieces is very important, which helps to address and to solve problems when a single test failure happens, not just to obtain general test pass or fail information.

--

--

Jen

SQA engineer specializing in test automation. Spending free time reading software testing articles and learning tools, bouldering, biking, or playing squash.