【问题标题】:How can I search and return the values and pass it to the method from spock table如何搜索并返回值并将其传递给 spock 表中的方法
【发布时间】:2017-02-07 09:17:39
【问题描述】:

目前正在实施 GEB、Spock、Groovy。我遇到了这样的场景

spock 表中有一组数据。我必须将模块名作为参数传递,从 spock 表中搜索,然后返回两个值用户 ID 和密码。下面的代码是骨架代码

我的问题是如何根据参数搜索模块名称? 如何返回两个数据?

Class Password_Collection extends Specification {

def "Secure password for search and Data Driven"(String ModuleName) {

   expect:
           // Search based on modulename in where
           // pick the values and return the picked data


            where:
            Module              | User_Name     | Pass_word
            login_Pass          | cqauthor1     | SGVsbG8gV29ybGQ =
            AuthorPageTest_Pass | cqauthor2     | DOIaRTd35f3y4De =
            PublisherPage_pass  | cqaauthor3    | iFK95JKasdfdO5 ==

}
        }

如果您提供代码,将对学习和实施有很大帮助。

【问题讨论】:

    标签: groovy spock geb


    【解决方案1】:

    您无需自己搜索表格或选择该数据。 Spock 会自动为您完成这项工作

    expect: 块中,只需编写使用 ModuleUser_NamePass_word 的单元测试。 Spock 将自动运行测试 3 次(与表中的行一样多),将每一行依次传递给您的测试。

    从测试方法中删除参数 ModuleName。不需要。

    我建议您多阅读有关数据驱动测试的 Spock 文档。

    【讨论】:

      【解决方案2】:
      class YourSpec extends Specification {
          def "Secure password for search and Data Driven"(Module, User_Name, Pass_Word) {
              expect:
              classUnderTest.getUserNameForModule(Module) == User_Name
              classUnderTest.getPasswordForModule(Module) == Pass_Word
      
              where:
              Module              | User_Name     | Pass_word
              login_Pass          | cqauthor1     | SGVsbG8gV29ybGQ =
              AuthorPageTest_Pass | cqauthor2     | DOIaRTd35f3y4De =
              PublisherPage_pass  | cqaauthor3    | iFK95JKasdfdO5 ==
      
          }
      }
      

      Spock 将做的是对“where”块中的数据表中的每一行运行一次测试,将 Module、User_Name、Pass_Word 作为参数传递,并在“expect”块中断言您的期望。

      请参阅Spock Data Driven Testing 文档了解更多详情。

      【讨论】:

      • 场景为:传入模块名,匹配后在where块中搜索,获取user_name和pass_word并返回到另一个方法/类密码解密,发送userid和password它作为凭证到另一个模块。但在代码中它返回 true 或通过。
      • 请根据上述情况更正以下代码。类 PasswordCollection 扩展规范 { def “数据驱动的安全密码”(字符串关键字){ 期望:如果(关键字 ==模块)返回 encryptPassPass_word 其中:模块 |用户名 |密码
      猜你喜欢
      • 2020-02-02
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 2015-03-04
      • 2014-07-27
      • 2016-02-13
      • 1970-01-01
      相关资源
      最近更新 更多