Behave 简明教程
Behave - Reports
报告生成是朝着测试自动化框架迈出的最重要的步骤之一。在执行结束时,我们无法依赖控制台输出,而是应该提供一份详细的报告。
Report generation is one of the most important steps towards the test automation framework. At the end of the execution, we cannot rely on the console output rather we should have a detailed report.
它应包括通过、失败、跳过的测试数量、特性和场景细分信息。Behave 不会生成内置报告,但可以以多种格式输出,我们可以利用第三方工具生成报告。
It should have the information on the count of tests that passed, failed, skipped, feature and scenario breakdown. Behave does not produce an in-built report but it can output in multiple formats and we can utilize the third-party tools to generate a report.
使用命令显示 Behave 中所有可用格式化程序:
All the available formatters in Behave are displayed with the command −
behave --format help
当您使用该命令时,您的电脑上将出现以下屏幕:
When you use the command, the following screen will appear on your computer −

一些常见的 Behave 报告为:
Some of the common Behave reports are −
-
Allure Report.
-
Output JSON Report.
-
JUnit Report
JUnit Report
让我们执行一个包含两个特性文件的测试,其测试结果如下:
Let us execute a test having two feature files with the below test results −

上述测试的项目文件夹结构如下:
Project folder structure for the above test will be as follows −

Step 1 − Execute the command
Step 1 − Execute the command
要创建一个 JUnit 报告,请运行下面给出的命令:
To create a JUnit report, run the command given below −
behave --junit
Step 2 − Report folder generation
Step 2 − Report folder generation
将在项目中生成一个名为 reports 的文件夹,名称为 TESTS-<feature file name>.xml 。
A folder called as the reports gets generated within the project, having the name TESTS-<feature file name>.xml.

此处,Payment 和 Payment1 是特性文件名称。
Here, Payment and Payment1 are the names of the feature files.
Step 3 − Report generation to a specific folder
Step 3 − Report generation to a specific folder
要将报告生成到一个特定文件夹(比如 my_reports),我们需要运行下面提到的命令:
To generate the reports to a specific folder, say my_reports. We have to run the below mentioned command −
behave --junit --junit-directory my_reports

一个名为 my_reports 的文件夹将在项目中生成,其中包含该报告。
A folder called the my_reports gets generated within the project which contains the reports.
JSON Report
我们可以创建行为 JSON 报告。JSON 实际上是一种格式化器。
We can create the Behave JSON report. The JSON is actually a formatter.
让我们执行一个包含两个特性文件的测试,其测试结果如下:
Let us execute a test having two feature files with the below test results −

上述测试的项目文件夹结构如下−
Project folder structure for the above test is as follows −

Step 1 − Execute the command
Step 1 − Execute the command
要在控制台中创建 JSON 输出,运行命令−
To create a JSON output in console, run the command −
behave -f json
将出现以下屏幕−
The following screen will appear −

Step 2 − Output in readable format
Step 2 − Output in readable format
要以更易读的格式创建 JSON 输出,运行以下命令−
To create a JSON output in a more readable format, run the following command −
behave -f json.pretty
以下是捕获在下图中一部分输出−
Some portion of the output captured in the below image −

Step 3 − Report generation to a specific folder
Step 3 − Report generation to a specific folder
要将报告生成到特定文件夹(例如 my_reports.json),我们必须运行以下命令−
To generate the reports to a specific folder say, my_reports.json, we have to run the following command −
behave –f json.pretty –o my_reports.json
下图表示将出现在您计算机上的屏幕。
The following image represents the screen that will appear on your computer.

一个名为 my_reports.json 的文件夹会在项目中生成,其中包含所有已执行特征的详细信息。
A folder called the my_reports.json gets generated within the project, having details of all the features which are executed.
Allure Report
要在 Behave 中生成 Allure 报告,我们首先必须在系统中安装 Allure。如需从 Linux 命令行安装,依次运行以下命令−
To generate Allure reports in Behave, first we have to install Allure in the system. For installation from the command line in Linux, run the following commands one after the other −
sudo apt-add-repository ppa:qameta/allure
sudo apt-get update
sudo apt-get install allure
对于 Mac 用户,安装使用 Homebrew 完成,且使用以下命令−
For Mac users, installation is done with the Homebrew with the following command −
brew install allure
对于 Windows,Allure 从 Scoop 安装程序安装。运行以下命令下载并安装 Scoop,最终在 PowerShell 中执行−
For Windows, Allure is installed from the Scoop installer. Run the below command to download and install Scoop and finally, execute it in the PowerShell −
scoop install allure
要从 Scoop 更新 Allure 分发安装,从 Scoop 安装目录运行以下命令−
To update Allure distribution installations from Scoop, run the below command from the installation directory of Scoop −
\bin\checkver.ps1 allure -u
最后,运行以下给出的命令−
Finally, run the command given below −
scoop update allure
安装 Allure 后,我们必须为 Python 获取 Allure-Behave 集成插件。为此,运行以下命令−
After Allure has been installed, we have to get the Allure-Behave integration plugin for Python. For this, run the following command −
pip install allure-behave
要验证是否已成功安装 Allure,请运行以下说明的命令−
To verify if Allure has been installed successfully, run the command stated below −
allure
让我们执行一个包含两个特性文件的测试,其测试结果如下:
Let us execute a test having two feature files with the below test results −

上述测试的项目文件夹结构如下−
Project folder structure for the above test is as follows −

Step 1 − Report generation to a specific folder
Step 1 − Report generation to a specific folder
要将报告生成到特定文件夹(例如 my_allure),我们必须运行以下命令−
To generate the reports to a specific folder, say my_allure, we have to run the following command −
behave -f allure_behave.formatter:AllureFormatter –o my_allure
你会得到如下所示的屏幕——
You will get the screen as shown below −

一个名为 my_allure 的文件夹会在项目中生成,其中包含带有 .json 扩展名的文件。
A folder called the my_allure gets generated within the project, having files with .json extension.
Step 2 − Start the web server
Step 2 − Start the web server
要启动 Web 服务器,运行以下给出的命令:
To start the web server, run the command given below −
allure serve my_allure
这里,my_ allure 是包含 allure JSON 文件的目录。
Here, the my_allure is the directory which contains the allure json files.

同时,打开一个浏览器,其中包含如下所示的 Allure 报告:
Simultaneously, a browser gets opened, with the Allure report as shown below −

我们还可以点击各个 feature 并找到它们的分解,如下所示:
We can also click on individual features and find their breakdowns, as shown below −

