我对自动化测试和黄瓜也很陌生。我用三种方案编写了一个简单的cucumber-Junit示例。
我期望在每个场景的开头调用@Before
,并在每个场景之后调用@After
。所以总共应该执行三次,因为有三种情况。
当我运行以下步骤定义时,首先调用@Before
,然后调用@After
,然后再调用@Before
,然后是场景1,然后调用@After
,然后{ {1}},然后是场景2,然后是@Before
,然后是场景3.因此,在场景3之前和之后分别调用@After
和@Before
。另一方面,在执行第一个场景之前,它们被调用两次。知道我在这里做错了什么吗?我的Hook项目中没有任何.rb文件。
我的@After
:@Before contains
和我的System.out.println("Setup performed");
包含@After
System.out.println("CleanUp Performed");
功能文件如下所示:
import cucumber.api.PendingException;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class CalculatorSteps {
private Calculator calculator;
@Before
public void setUp() {
calculator = new Calculator();
System.out.println("Setup Performed...");
}
//Scenario : add two numbers - With regular expression
@Given("^I have a calculator$")
public void i_have_a_calculator() {
assertNotNull(calculator);
}
@When("^I add (\\d+) and (\\d+)$")
public void i_add(int arg1, int arg2) {
calculator.add(arg1, arg2);
}
@Then("^the result should be (\\d+)$")
public void the_result_should_be(int result) {
assertEquals(result, calculator.getResult());
}
//Scenario : Subtract one number from another - With regular expression
@Given("^I have a calculatorr$")
public void i_have_a_calculator1() throws Throwable {
assertNotNull(calculator);
}
@When("^I subtract (\\d+.\\d+) from (\\d+.\\d+)$")
public void i_subtract_from(int arg1, int arg2) {
calculator.subtract(arg1, arg2);
}
@Then("^the result should be (\\d+.\\d+)$")
public void the_result_should_be1(double result1) {
assertEquals(result1, calculator.getresult1(), 0.5);
}
@After
public void cleanUp() {
System.out.println("CleanUp Performed...");
}
}
输出如下:
Feature: Calculator
I use Calculator instead of calculating myself
#Scenario: Add two numbers
#Given I have a calculator
#When I add 2 and 3
#Then the result should be 5
@smokeTest
Scenario Outline: Add two numbers
Given I have a calculator
When I add <num1> and <num2>
Then the result should be <ans>
Examples:
| num1 | num2 | ans |
| 2 | 3 | 5 |
| 4 | 5 | 9 |
@regressionTest
Scenario: Subtract one number from another
Given I have a calculator
When I subtract 2.5 from 7.5
Then the result should be 5.0
尝试 Feature: Calculator
I use Calculator instead of calculating myself
Setup Performed...
CleanUp Performed...
Setup Performed...
Scenario Outline: Add two numbers # src/test/java/cucumber/junit/maven/cucumber_jvm/maven/calculatorFeature.feature:19
Given I have a calculator # CalculatorSteps.i_have_a_calculator()
When I add 2 and 3 # CalculatorSteps.i_add(int,int)
Then the result should be 5 # CalculatorSteps.the_result_should_be(int)
CleanUp Performed...
Setup Performed...
Scenario Outline: Add two numbers # src/test/java/cucumber/junit/maven/cucumber_jvm/maven/calculatorFeature.feature:20
Given I have a calculator # CalculatorSteps.i_have_a_calculator()
When I add 4 and 5 # CalculatorSteps.i_add(int,int)
Then the result should be 9 # CalculatorSteps.the_result_should_be(int)
CleanUp Performed...
#@regressionTest
Scenario: Subtract one number from another # src/test/java/cucumber/junit/maven/cucumber_jvm/maven/calculatorFeature.feature:23
Given I have a calculator # CalculatorSteps.i_have_a_calculator()
When I subtract 2.5 from 7.5 # CalculatorSteps.i_subtract_from(int,int)
Then the result should be 5.0 # CalculatorSteps.the_result_should_be1(double)
3 Scenarios (3 passed)
9 Steps (9 passed)
0m0,074s
和@DaveyDaveDave代码后,输出如下所示:开头的计数行打印位置不正确,但至少要正确执行sems。但我仍然想知道为什么在最后一个Subtract场景中它不会执行counter
,@Before
和@Given
。
@After
答案 0 :(得分:0)
我认为功能按照您期望的顺序执行,这只是输出显示方式和时间的问题。
我刚刚复制并粘贴了你的源代码,并添加了一个counter
变量,当我运行它时,我看到以下输出:
Setup Performed... 0
In given...1
...CleanUp Performed... 2
Setup Performed... 3
In given...4
...CleanUp Performed... 5
Setup Performed... 6
In given...7
...CleanUp Performed... 8
换句话说,数字显示这些事情的顺序正如您所期望的那样发生。
如果它有助于说服你,我创建了以下RequiresSetupCalculator
,如果在执行操作之前尚未设置异常,则会抛出异常,并且如果我们尝试在没有先前调用的情况下运行安装程序cleanUp
。
public class RequiresSetupCalculator {
private static RequiresSetupCalculator instance;
private int result = 0;
private double result1 = 0;
private boolean isSetup;
private RequiresSetupCalculator() {
}
public static synchronized RequiresSetupCalculator setup() {
if (instance == null) {
instance = new RequiresSetupCalculator();
}
if (instance.isSetup) {
throw new RuntimeException("I have already been set up");
}
instance.isSetup = true;
return instance;
}
public void cleanUp() {
if (!isSetup) {
throw new RuntimeException("I wasn't set up in the first place");
}
isSetup = false;
}
public void add(final int arg1,
final int arg2) {
if (!isSetup) {
throw new RuntimeException("I haven't been set up, no calculating for you!");
}
result = arg1 + arg2;
}
public int getResult() {
if (!isSetup) {
throw new RuntimeException("I haven't been set up, no calculating for you!");
}
return result;
}
public void subtract(final int arg1,
final int arg2) {
if (!isSetup) {
throw new RuntimeException("I haven't been set up, no calculating for you!");
}
result1 = arg2 - arg1;
}
public double getresult1() {
if (!isSetup) {
throw new RuntimeException("I haven't been set up, no calculating for you!");
}
return result1;
}
}
要对此进行测试,如果您将上述内容与现有Calculator
一起复制到新课程中,请修改CalculatorSteps
以使用RequiresSetupCalculator
并调用setup
您的测试的cleanUp
和@Before
方法中的}和@After
方法如下:
public class CalculatorSteps {
private RequiresSetupCalculator calculator;
@Before
public void setUp() {
calculator = RequiresSetupCalculator.setup();
System.out.println("Setup Performed...");
}
... (existing test steps unchanged)...
@After
public void cleanUp() {
System.out.println("CleanUp Performed...");
calculator.cleanUp();
}
}
现在,当你运行测试时,如果它仍然通过,我认为我们可以确保@Before
方法在每次测试之前运行一次且仅运行一次,并且@After
方法运行每次测试后,也只进行一次。
对我来说,这是成功的。如果你在运行测试时遇到异常,那么就会发生一些奇怪的事情,但我相信你会得到与我相同的结果。