BDD(Behavior Driven Development),行为驱动开发, 对应自动化测试框架,python有behave,java有cucumber, 这次记录cucumber+springboot+maven的自动化测试框架。
基本结构如下:
1)POM.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.3.RELEASE
org.example
cucumberExample
1.0-SNAPSHOT
2.3.1
3.14.0
3.7.0
1.8
org.springframework.boot
spring-boot-starter-test
test
io.cucumber
cucumber-java8
${cucumber.version}
io.cucumber
cucumber-spring
${cucumber.version}
io.cucumber
cucumber-junit
${cucumber.version}
net.masterthought
cucumber-reporting
${cucumber-reporting.version}
org.apache.maven.plugins
maven-compiler-plugin
${maven.compiler.version}
${java.compiler.version}
${java.compiler.version}
org.apache.maven.plugins
maven-surefire-plugin
3.0.0-M5
true
org.apache.maven.surefire
surefire-junit4
3.0.0-M5
-Xmx1024m
-Xms1024m
3.0c
true
true
**/demoRunner.class
2)features: 最重要的环节,用户场景
@login
Feature: login function
Scenario Outline: password checking
Given I have open the login page
And input account=And input password=When I click the LOGIN button
Then itreturnlogin success to me
Examples:|account|password|
|user1 |password1|
|user2 |password2|
3)steps: 实现feature 里面每一个步骤的逻辑,一句话对应一个step一个方法
备注:这里我还没有加上方法体,以后再加
packagesteps;/** @author Helen Lee
* @create 2020/9/11
* @description*/
importcucumber.api.java.en.Given;importcucumber.api.java.en.Then;importcucumber.api.java.en.When;importorg.junit.Assert;public classlogin_steps {
@Given("I have open the login page")public voidiHaveOpenTheLoginPage() {
}
@Given("input account=(.*)")public voidinputAccountAccount(String account) {
}
@Given("input password=(.*)")public voidinputPasswordPassword(String password) {
}
@When("I click the LOGIN button")public voidiClickTheLOGINButton() {
}
@Then("it return login success to me")public voiditReturnLoginSuccessToMe() {
}
}
4)runner: 执行testcases
/** @author Helen Lee
* @create 2020/9/11
* @description*/
importcucumber.api.CucumberOptions;importcucumber.api.junit.Cucumber;importorg.junit.runner.RunWith;importorg.springframework.boot.test.context.SpringBootTest;
@RunWith(Cucumber.class)
@SpringBootTest
@CucumberOptions(
tags= {"@login"},
features= {"src/main/resources/features"},
glue= {"steps"},
plugin={"pretty","html:target/cucumber","json:target/cucumberReportJsonFiles/cucumber-report.json"}
)public classdemoRunner {public voidtest() {
}
}
完成这四个之后,就可以run demoRunner了,结果如下:跑了user1/password1 和user2/passwords两个test cases