Selenium 简明教程

Generating HTML Test Reports in Python

我们可以使用 Pytest 测试框架生成 HTML 报告,其中包含 Selenium 测试。要配置 Pytest,我们必须运行以下命令 −

We can generate HTML reports with our Selenium test using the Pytest Testing Framework. To configure Pytest, we have to run the following command −

pip install pytest.

安装完成后,我们可以运行以下命令来检查已安装的 Pytest 版本 −

Once the installation is done, we can run the command to check the Pytest version installed −

pytest –version

作为 Pytest 标准,包含 Pytest 的 Python 文件应以 test_ 开头或以 test. Also, all the test steps should be within a method whose name should start with test 结尾。

As a Pytest standard, the Python file containing the Pytest should start with test_ or end with test. Also, all the test steps should be within a method whose name should start with test.

要运行 Pytest 文件,我们可以打开终端并从当前目录移至我们要执行的 Pytest 文件的目录。然后,运行下面提到的命令 −

To run a Pytest file, we can open the terminal and move from the current directory to the directory of the Pytest file that we want to execute. Then, run the command mentioned below −

py.test -v -s.

让我们看一下遵循 Pytest 测试框架的项目结构。

Let us look at a project structure following the Pytest Test Framework.

pytest test framework

在上图中,它表明 Pytest 文件的名称为 test_p.py,并且它包含一个名为 test_SeleniumTest 的测试方法。

In the above image, it shows that the Pytest file has the name test_p.py and it contains a test method with the name test_SeleniumTest.

要为 Selenium 测试生成 HTML 报告,我们必须使用以下命令安装插件:pip install pytest-html。要生成报告,我们必须从当前目录移至我们要执行的 Pytest 文件的目录。然后运行以下命令:pytest --html=report.html。

To generate a HTML report for a Selenium test, we have to install a plugin with the command: pip install pytest-html. To generate the report, we have to move from the current directory to the directory of the Pytest file that we want to execute. Then run the command: pytest --html=report.html.

成功执行此命令后,将在项目中生成一个名为 report.html 的新文件。

After this command is successfully executed, a new file called the report.html gets generated within the project.

python project test

右键单击 report.html 并选择“复制路径”选项。

Right-click on the report.html and select the option Copy Path.

report html

在浏览器中打开复制的路径,以获取 HTML 报告。

Open the path of the file copied in a browser, to get the HTML report.

report

HTML 报告提供了执行测试的环境信息。它还包括测试摘要和结果的信息。

The HTML report gives information of the Environment on which the test is executed. It also contains the information on test Summary and Results.