【问题标题】:Cucumber JUNIT Step Execution Ignored黄瓜 JUNIT 步骤执行被忽略
【发布时间】:2017-11-28 02:02:35
【问题描述】:

我一直在拼命尝试解决 Cucumber Junit 步骤执行问题。

我只是按照一个简单的例子来定义一个特性,测试和步骤如下:

Feature: Campaign Budget Calculation

Scenario: Valid Input Parameters
  Given campaign budget as 100 and campaign amount spent as 120
  When the campaign budget is less than campaign amount spent
  Then throw an Error

测试:

@RunWith(Cucumber.class)
@Cucumber.Options(glue = { "com.reachlocal.opt.dbas" })
public class CampaignTest {

}

步骤:

public class CampaignTestStepDefinitions {

    private Campaign campaign;

    @Given("^a campaign with (\\d+) of budget and (\\d+) of amount spent$")
    public void createCampaign(int arg1, int arg2) throws Throwable{
        CurrencyUnit usd = CurrencyUnit.of("USD");
        campaign = new Campaign();
        campaign.setCampaignBudget(Money.of(usd, arg1));
        campaign.setCampaignAmountSpent(Money.of(usd, arg2));
    }

    @When("^compare the budget and the amount spent$")
    public void checkCampaignBudget() throws Throwable{
        if (campaign.getCampaignBudget().isLessThan(campaign.getCampaignAmountSpent())) {
            campaign.setExceptionFlag(new Boolean(false));
        }
    }

    @Then("^check campaign exception$")
    public void checkCampaignException() throws Throwable{
        if (campaign.getExceptionFlag()) {
            assertEquals(new Boolean(true), campaign.getExceptionFlag());
        }
    }
}

当我运行 junit 时,这些步骤被跳过,结果显示它们都被忽略了。我以前也试过不用胶水,但没有帮助。 不知道为什么。来自 Internet 的简单示例代码(例如添加 2 个数字)工作正常。 我正在使用 STS 在 Maven/Spring 项目中运行它。

【问题讨论】:

    标签: cucumber


    【解决方案1】:

    @Given、@When 和 @Then 表达式与功能文件不匹配。它们是需要匹配特征文件中的行的正则表达式。

    例如,对于特征线:

    假设活动预算为 100,活动金额为 120

    在您获得的步骤文件中:

    @Given("^一个预算为 (\d+) 且金额为 (\d+) 的广告系列 花了$")

    但应该是:

    @Given("^campaign 预算为 (\d+),活动金额为 (\d+)$")

    那么它应该匹配而不是忽略该步骤。

    刚刚遇到同样的问题,在 Eclipse 中很容易错过,因为您仍然会看到一个绿色的勾号,尽管它确实表示它们被忽略了。

    【讨论】:

      【解决方案2】:

      试试这个,

      import org.junit.runner.RunWith;
      
      import cucumber.api.junit.Cucumber;
      
      @RunWith(Cucumber.class)
      @Cucumber.Options(format = { "json:target/REPORT_NAME.json", "pretty",
          "html:target/HTML_REPORT_NAME" }, features = { "src/test/resources/PATH_TO_FEATURE_FILE/NAME_OF_FEATURE.feature" })
      public class Run_Cukes_Test {
      
      }
      

      这一直对我有用。

      【讨论】:

        【解决方案3】:

        我也遇到了同样的错误,结果发现步骤定义下的所有方法都是 private 而不是 public

        【讨论】:

          猜你喜欢
          • 2020-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-27
          • 1970-01-01
          • 1970-01-01
          • 2018-05-20
          相关资源
          最近更新 更多