【问题标题】:How do implement step definition in multiple lines?如何在多行中实现步骤定义?
【发布时间】:2018-08-07 21:23:02
【问题描述】:

我正在使用 Scala、Cucumber、Gherkin 语言进行自动化测试。我的功能文件中有以下场景,用 Gherkin 语言编写:

Scenario Outline: Do something
  When I do something  
  Then result should have field1 as "<filed_1>", field2 as "<filed_2>" and filed3 as "<filed_3>"

而生成的stepdef是:

When("""^I do something$""") {
        // do operation
}
Then("""^result should haveresult should have field1 as "([^"]*)", 
    field2 as "([^"]*)" and filed3 as "([^"]*)"$""") {
     // do operation
}

这里 Then 语句在一行中有超过 120 个字符,我必须将其分成两部分。我不应该更改功能文件。

当把那么分成两行

Then("""^result should haveresult should have field1 as "([^"]*)", 
      | field2 as "([^"]*)" and filed3 as "([^"]*)"$""".stripMargin)

它说您可以使用下面的 sn-ps 实现缺少的步骤:

Then("""^result should have field1 as "([^"]*)", field2 as "([^"]*)" and filed3 as "([^"]*)"$"""){ (arg0:String, arg1:String, arg2:String) =>
  //// Write code here that turns the phrase above into concrete actions
  throw new PendingException()
} 

我不确定应该采用哪种方法将 然后 分成多行。感谢任何帮助。

【问题讨论】:

    标签: scala cucumber gherkin


    【解决方案1】:

    您可以将String 拆分为两个并将它们相加:

    Then("""^result should have field1 as "(.*)", field2 as "(.*)" """ + 
      """and filed3 as "(.*)"$""") { (arg0:String, arg1:String, arg2:String) =>
    
      //etc
    }
    

    这与您要拆分然后相加的任何String 相同:

    """1234567890""" == """12345""" + """67890""" // true
    

    【讨论】:

    • 好吧,我也试过了,得到 “你可以实现缺少的步骤”
    • Gherkin/Cucumber 测试使用基本的正则表达式检查运行(因此 ^$(.*) 等)。听起来您的 StepDef 与 Feature 文件不匹配。在您最初的问题中,您声明Then 块是Then("""^result should haveresult should have field1 ...。重复的result should haveresult should have 是错字吗?
    • 我的错。更新了有问题的然后
    猜你喜欢
    • 1970-01-01
    • 2023-01-20
    • 2017-07-23
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多