12

我有以下功能文件:MacroValidation.feature

@macroFilter
Feature: Separating out errors and warnings
Scenario: No errors or warnings when separating out error list
    Given I have 0 macros
    When I filter out errors and warnings for Macros
    Then I need to have 0 errors
        And I need to have 0 warnings

我的定义文件。

package com.test.definition;

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 cucumber.runtime.java.StepDefAnnotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.powermock.api.mockito.PowerMockito.doReturn;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.spy;


@StepDefAnnotation
public class MacroValidationStepDefinitions {

private final MacroService macroService = spy(new MacroService());
private final LLRBusList busList = mock(LLRBusList.class);    
private final List<String> errorList = new ArrayList<String>();
private final List<String> warningList = new ArrayList<String>();

@Before({"@macroFilter"})
public void setUp() {
    errorList.addAll(Arrays.asList("error 1, error2, error 3"));
    warningList.addAll(Arrays.asList("warning 1, warning 2, warning 3"));
}

@After({"@macroFilter"})
public void tearDown() {
    errorList.clear();
    warningList.clear();
}

@Given("^I have (\\d+) macros$")
public void i_have_macros(int input) {
    doReturn(input).when(busList).size();
}

@When("^I filtered out errors and warnings for Macros$")
public void i_filtered_out_errors_and_warnings_for_Macros() {
    macroService.separateErrorsAndWarning(busList, errorList, warningList);
}

@Then("^I need to have (\\d+) errors$")
public void  i_need_to_have_errors(int numOfError) {
    if (numOfError == 0) {
        assertTrue(errorList.isEmpty());
    } else {
        assertEquals(errorList.size(), numOfError);
    }
}

@Then("^I need to have (\\d+) warnings$")
public void  i_need_to_have_warnings(int numOfWarnings) {
    if (numOfWarnings == 0) {
        assertTrue(warningList.isEmpty());
    } else {
        assertEquals(warningList.size(), numOfWarnings);
    }
}
}

我的单元测试课。

@CucumberOptions(features = {"classpath:testfiles/MacroValidation.feature"},
                 glue = {"com.macro.definition"},
                 dryRun = false, 
                 monochrome = true,
                 tags = "@macroFilter"
                 )
@RunWith(Cucumber.class)
public class PageMacroValidationTest {
}

当我执行测试时,我在日志中收到文件定义未实现的警告。

示例日志:

You can implement missing steps with the snippets below:

@Given("^I have (\\d+) macros$")
public void i_have_macros(int arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@When("^I filter out errors and warnings for Macros$")
public void i_filter_out_errors_and_warnings_for_Macros() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^I need to have (\\d+) errors$")
public void i_need_to_have_errors(int arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

@Then("^I need to have (\\d+) warnings$")
public void i_need_to_have_warnings(int arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

我不认为文件名应该重要吗?

4

3 回答 3

13

看起来 Cucumber 没有找到你的步骤定义类。在您的单元测试课程中,您说:

 glue = {"com.macro.definition"}

但是步骤定义类在com.test.definition

尝试将该行更改为:

 glue = {"com.test.definition"}

您可能必须重建您的项目以获取更改。

于 2014-06-19T08:25:21.540 回答
2

此外,Cucumber 对空白区域很敏感。如果您在捕获片段后尝试使您的跑步者或功能文件漂亮,您将遇到此问题。

这是一个在创建我的第一个 BDD 时让我发疯几个小时的示例。我已经创建了功能文件和一个骨架运行器,我运行并捕获了片段。然后我美化了功能文件,当我运行跑步者时,出现了错误。

当然,在我的人类大脑看来一切都很好,所以接下来的几个小时在这里进行了毫无结果的研究,检查版本和错误列表。最后我决定比较一下代码片段的前两行,看看有什么不同:

//  @Then("^the test result is = \"([^\"]*)\"$")
//  public void theTestResultIs(String ruleResult) throws Throwable {
    @Then("^the test result is         = \"([^\"]*)\"$")
    public void theTestResultIs(String arg1) throws Throwable {

嗬!

于 2017-04-05T18:41:06.250 回答
0

尝试将 POM.xml 中的所有依赖项与 io.cucumber 组 id 一起使用,这是最新的 jars 而不是 info.cukes。删除 jars 后,使用新的导入更新项目并运行项目。

请记住,您必须将 info.cukes 的所有依赖项替换为 io.cucumber

于 2020-03-10T16:24:10.550 回答