Cucumber入门篇:https://blog.csdn.net/Cupupup/article/details/80152297
测试数据参数化
在cucumber入门篇的最后,有一个简单的cucumber实例,很明显的是,参数实在测试文件中写死的,并不是参数化的,这不符合实际的要求。因此需要实现测试数据的参数化。
修改stepDefinition.java文件中的注释和并将无参的方法改为有参的方法,再在方法中使用参数。参数具体的值则在calculatorAdd.feature中定义。
stepDefinition.java修改前代码:
package test.java.cucumberDefinition;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
import cucumber.api.java.en.Then;
import static org.junit.Assert.assertEquals;
import com.calculator.Calculator;
import cucumber.api.java.en.And;
public class stepDefinition {
Calculator cal;
int result;
@Given("^init the object of calculator$")
public void given() throws Throwable {
cal= new Calculator();
}
@When("^clear the result to zero$")
public void when() throws Throwable {
cal.clear();
}
@Then("^add num1 and num2$")
public void then() throws Throwable {
cal.add(2);
cal.add(3);
}
@And("^check the actual result$")
public void and() throws Throwable {
int expected = 5;
result = cal.getResult();
assertEquals(expected,result);
}
}
注意到修改前的代码中,所有的方法都是无参的,并且add( )方法中的数值是写死,接下来写在calculatorAdd.feature中定义参数的数值,再修改stepDefinition.java文件中的注释和并将无参的方法改为有参的方法,再在方法中使用参数。注释中对应参数的位置改为(.*)。
@tag
Feature: Test add function of calculator
@tag1
Scenario: Add two numbers
Given init the object of calculator
When clear the result to zero
And add 2 and 3
Then check the actual result 5
package test.java.cucumberDefinition;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
import cucumber.api.java.en.Then;
import static org.junit.Assert.assertEquals;
import com.calculator.Calculator;
import cucumber.api.java.en.And;
public class stepDefinition {
Calculator cal;
int result;
@Given("^init the object of calculator$")
public void given() throws Throwable {
cal= new Calculator();
}
@When("^clear the result to zero$")
public void when() throws Throwable {
cal.clear();
}
@Then("^add (.*) and (.*)$")
public void then(int a,int b) throws Throwable {
cal.add(a);
cal.add(b);
}
@And("^check the actual result (.*)$")
public void and(int expected) throws Throwable {
//int expected = 5;
result = cal.getResult();
assertEquals(expected,result);
}
}
多组数据测试
在实际测试中不仅需要实现测试数据的参数化,在某些测试中,还会需要对多组测试数据进行测试。
实现多组数据的测试需要用到cucumber入门篇中讲到的Scenario Outline和Examples这一对关键字。先将calculatoradd.feautre中的Scenario关键字改为Scenario Outline,再将需要填写参数的位置用<变量名>代替,最后添加Examples关键字和数据列表,并且stepDefinition.java中的修改同参数化一样。
@tag
Feature: Test add function of calculator
@tag1
Scenario Outline: Add two numbers
Given init the object of calculator
When clear the result to zero
And add <num1> and <num2>
Then check the actual result <expected>
Examples:
| num1 | num2 | expected |
| 1 | 2 | 3 |
| 2 | 3 | 5 |
添加Hook
在cucumber入门篇中讲到,在创建JUnit test case时,需要将其原先所有的注释清空,包括Test。这也就意味着,Befor和After这两个注释也被删除,那如果仍然想使用这两个注释该怎么办?Cucumber提供了Hook来实现。
在stepDefinition.java的包下,再创Before和After注释建一个java文件,本例创建名为Hook.java的文件,再添加Before和After注释。需要注意的是,这里的Before和After注释是cucumber.api.java.Afte和cucumber.api.java.Before中的注释而不是junit中的注释。最后在这两个注释下添加相应的方法。代码和运行结果如下。
package test.java.cucumberDefinition;
import cucumber.api.java.After;
import cucumber.api.java.Before;
public class Hook {
@Before
public void beforeScenario() {
System.out.println("Start Scenario");
}
@After
public void aftereScenario() {
System.out.println("Stop Scenario");
}
}
本文介绍了Cucumber框架如何实现测试数据的参数化,通过修改stepDefinition.java文件中的方法和calculatorAdd.feature中的定义。此外,还讲解了如何进行多组数据测试,利用Scenario Outline和Examples关键字完成。最后,文章提到了Cucumber的Hook功能,通过创建Hook.java文件添加Before和After注释,实现类似JUnit的测试前置和后置操作。

7417

被折叠的 条评论
为什么被折叠?



