BDD tools--Basic use of Cucumber

1. BDD's tool, Cucumber, uses the java language to write the framework, and the language that Cucumber can use for text is called Gherkin
ideas and principles
Define the behavior of the user, i.e. acceptance test conditions
· Define steps
Execute acceptance tests, when all tests fail because the content of the step definition is not implemented
Write the content of the step definition, that is, implement the product code
Run the acceptance tests, if the acceptance tests pass, the production code passes
Repeat steps 2-5 until all production code is complete and the entire acceptance test is passed
behavior description
Feature, its size is equivalent to a Test Suite (test case set), and a feature represents a function.
Scenario, equivalent to test case (test case).
Step contains the phrases Given, When, Then, And and But. Although Cucumber itself does not technically distinguish these three phrases, it is recommended to use them according to the meaning of the phrases.
Given (given) is the environment required for scene testing, which is equivalent to a precondition;
When (when) is to trigger the event under test, similar to the click event, etc.;
Then (then) is the verification result, similar to the verification steps in the usual use case;
And (and) if there are multiple Given operations, the latter Given can be replaced by And;
But (but) If there are multiple Then operations, the Then after the second can be replaced by But.
step definition
A complete step definition is a function that contains: annotation, method name, parameters, and method content
Regular expressions are usually included in comments

^ is the start of the matched string in the regular expression, $ is the end of the matched string, (.*) means match any character except the newline,
This matching value will be passed to the function parameter.
@When("^Enter keyword: (.*)$")
publicvoidinput(String keyword){
result= search.find(keyword);
}
Cucumber-java测试环境准备

1.下载 Cucumber-jvm 相关 jar 包,
Cucumber-core.jar 核心包
Cucumber-java.jar 通过 java 编写需要下载这个包
Cucumber-html.jar 生成结果为 html 文件需要下载这个包
Cucumber-junit.jar 生成结果为 junit 格式需要这个包
Cucumber-testng.jar 使用testng执行测试并生成报告
Junit.jar 如果代码中使用 junit 相关的类就需要这个包
testng.jar 如果代码中使用 testng 相关的类就需要这个包
Gherkin 步骤定义时需要这个包

BDD JAVA项目演练
1.新建maven项目




2.添加项目依赖
3.创建行为描述文件(Describe Behavior)
删除该文件
4.然后在cucumber包下建立一个子包(resources)用于存储.feature(行为描述文件)文件,如图:
4.步骤定义类(Step_Definition)
SearchStepdefs.java
5.执行测试者》以Junit执行或者是TestNG
想要运行 Cumcumber, 还需要一些简单的代码来驱动 Cucumber,
这些驱动 Cucumber 的代码单独放在一个 java 文件中,命名为 TestRunnerWithXXX.java,也放在cucumber包中。
6.书写实现步骤对象代码
cucumber包下新建一个Search.java类

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325763512&siteId=291194637