在功能之前是否有一种用于初始化的 ScalaTest 方法? (featureSpec套装)
Is there a method of ScalaTest for initialisation before a feature? (featureSpec suit)
我刚开始使用 ScalaTest,我想在我的功能测试之前做一些初始化。
我的测试看起来像这样:
class InvoiceTests extends FeatureSpec with GivenWhenThen with ShouldMatchers {
feature("Traffic Light Test") {
//this is where I want to initialise
scenario("test 1") {
val something = Invoice(bla bla bla)
Given("A connection to the system")
login.isConnected shouldBe true
When("an invoice that was uploaded to the system")
val invoiceAction = new InvoiceActions(driver,conf.getString("web.environment"))
invoiceAction.uploadInvoice(something)
And("the invoice is processed with information to be ready to reclaim")
invoiceAction.fillInvoice(something)
Then("Inovice status should be ready to reclaim")
invoiceAction shouldBe 'isReadyToReclaim
}
scenario("test 2") {
val something = Invoice(bla bla bla)
Given("A connection to the system")
login.isConnected shouldBe true
When("an invoice that was uploaded to the system")
val invoiceAction = new InvoiceActions(driver,conf.getString("web.environment"))
invoiceAction.uploadInvoice(something)
And("the invoice is processed with information to be ready to reclaim")
invoiceAction.fillInvoice(something)
Then("Inovice status should be ready to reclaim")
invoiceAction shouldBe 'isReadyToReclaim
}
}
所以我这里有 2 个场景,测试 1 和测试 2,但我想让它们拥有相同的驱动程序对象并在整个功能启动之前登录一次。那么我应该把这个初始化代码放在哪里:
val driver = new FirefoxDriver()
val conf = ConfigFactory.load()
val loginObj: LoginActions = new LoginActions(driver, conf.getString("web.environment"))
loginObj.login(conf.getString("user.nir.username"),conf.getString("user.nir.pass"))
因为我不能把它放在功能和第一个场景之间。
谢谢!
根据 FeatureSpec
http://doc.scalatest.org/2.2.6/#org.scalatest.FeatureSpec 的文档
假设提供此类功能有两个特征:
BeforeAndAftereEach
& BeforeAndAfterAll
但是它们在您的情况下不起作用,因为其中一个在每个场景之前执行,而另一个在测试中只执行一次 class。
在同一页面上,他们建议实施方法(withDriver
适合您的情况,并在功能的第一行显式调用它)。
你可以把它放在功能和场景之间,就在你想要的地方,但要让它成为 lazy val
。这样它将在两种情况下都在范围内,但仅在执行第一个场景时首次使用时初始化。
麻烦的是你不需要在最后关闭它吗?如果是这样,你可以把它移到顶层,保持惰性,然后使用 BeforeAndAfterAll
的 afterAll()
方法关闭它。 (如果您有其他延迟初始化资源的功能,您的 afterAll()
方法也可以关闭它们。)
我刚开始使用 ScalaTest,我想在我的功能测试之前做一些初始化。
我的测试看起来像这样:
class InvoiceTests extends FeatureSpec with GivenWhenThen with ShouldMatchers {
feature("Traffic Light Test") {
//this is where I want to initialise
scenario("test 1") {
val something = Invoice(bla bla bla)
Given("A connection to the system")
login.isConnected shouldBe true
When("an invoice that was uploaded to the system")
val invoiceAction = new InvoiceActions(driver,conf.getString("web.environment"))
invoiceAction.uploadInvoice(something)
And("the invoice is processed with information to be ready to reclaim")
invoiceAction.fillInvoice(something)
Then("Inovice status should be ready to reclaim")
invoiceAction shouldBe 'isReadyToReclaim
}
scenario("test 2") {
val something = Invoice(bla bla bla)
Given("A connection to the system")
login.isConnected shouldBe true
When("an invoice that was uploaded to the system")
val invoiceAction = new InvoiceActions(driver,conf.getString("web.environment"))
invoiceAction.uploadInvoice(something)
And("the invoice is processed with information to be ready to reclaim")
invoiceAction.fillInvoice(something)
Then("Inovice status should be ready to reclaim")
invoiceAction shouldBe 'isReadyToReclaim
}
}
所以我这里有 2 个场景,测试 1 和测试 2,但我想让它们拥有相同的驱动程序对象并在整个功能启动之前登录一次。那么我应该把这个初始化代码放在哪里:
val driver = new FirefoxDriver()
val conf = ConfigFactory.load()
val loginObj: LoginActions = new LoginActions(driver, conf.getString("web.environment"))
loginObj.login(conf.getString("user.nir.username"),conf.getString("user.nir.pass"))
因为我不能把它放在功能和第一个场景之间。
谢谢!
根据 FeatureSpec
http://doc.scalatest.org/2.2.6/#org.scalatest.FeatureSpec 的文档
假设提供此类功能有两个特征:
BeforeAndAftereEach
& BeforeAndAfterAll
但是它们在您的情况下不起作用,因为其中一个在每个场景之前执行,而另一个在测试中只执行一次 class。
在同一页面上,他们建议实施方法(withDriver
适合您的情况,并在功能的第一行显式调用它)。
你可以把它放在功能和场景之间,就在你想要的地方,但要让它成为 lazy val
。这样它将在两种情况下都在范围内,但仅在执行第一个场景时首次使用时初始化。
麻烦的是你不需要在最后关闭它吗?如果是这样,你可以把它移到顶层,保持惰性,然后使用 BeforeAndAfterAll
的 afterAll()
方法关闭它。 (如果您有其他延迟初始化资源的功能,您的 afterAll()
方法也可以关闭它们。)