如何实现Spock参数化测试最佳实践?

问题描述 投票:0回答:1

我有一个测试规范,可以使用唯一的数据集运行。最好的做法是有点不清楚。如何修改以下代码以运行:

@Stepwise
class marktest extends ShopBootStrap  {

   private boolean useProductionUrl = false

   def "Can Access Shop DevLogin page"() {
       // DevStartLogin: 'New OE Start' button click
       setup:
           println System.getProperty("webdriver.chrome.driver")
       when:
           to ShopDevStartPage
       then:
           at ShopDevStartPage
   }

   def "on start enrollment, select 'United States' and click 'continue' button"() {
       when: "enter Sponsor ID and click New OE Start"
           to ShopDevStartPage
           sponsorId.value(ShopDevStartPage.SPONSORID)
           NewOEButton.click()
       then:
           waitFor { NewEnrollmentPage }
   }
}

1)数据集1

private boolean useProductionUrl = false
protocol = System.getProperty("protocol") ?: "https"
baseDomain = System.getProperty("base.url") ?: "beta.com"
testPassword = System.getProperty("test.password") ?: "dontyouwish"

2)数据集2

private boolean useProductionUrl = true
protocol = System.getProperty("protocol") ?: "https"
baseDomain = System.getProperty("base.url") ?: "production.com"
testPassword = System.getProperty("test.password") ?: "dywyk"
spock shared unroll
1个回答
0
投票

通常,要进行测试取决于数据,请使用where块,可能与@Unroll注释一起使用。

但是,您的情况根本不是数据驱动测试的最佳示例。 baseDomainprotocol应该放在GebConfig.groovy中,类似于你提供的片段。请参阅this section in the Book of Geb,因为这是您正在使用的。

简单的例子(在GebConfig.groovy中):

environments {
  production {
    baseUrl = "https://production.com"
  }
  beta {
    baseUrl = "https://beta.com"
  }
}

如果以这种方式完成,您的个人测试不需要关心环境,因为这已经内置到Geb中。例如,当导航到页面时,会自动设置其基本URL。您没有在示例中提供该部分代码(如何定义页面),因此我无法直接帮助您。

现在,在您的情况下,就“密码”而言,您可以从环境变量或系统属性中读取,您可以使用geb.envgeb.build.baseUrl系统属性设置接近您配置Geb的位置。 注意我只是出于实际原因考虑这一点,而不考虑密码的保密性。

您将在使用它的页面类中拾取变量。 页面类中的示例代码:

static content = {
    //...
    passwordInput = { $('input[type="password"]') }
    //...
}

void enterPassword() {
    passwordInput.value(System.getProperty('test.password'))
}

要使其工作,您需要在系统属性设置为正确值的情况下开始测试。

例如。如果直接从命令行开始,您将添加参数-Dgeb.env=beta -Dtest.password=dontyouwish。如果从Gradle任务运行,则需要向该任务添加适当的systemProperty键和值。如果从IDE运行,请参阅IDE文档,了解如何在运行程序时设置Java系统属性。

© www.soinside.com 2019 - 2024. All rights reserved.