我刚刚开始使用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
}
}
所以我在这里有两个场景,测试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"))
因为我不能把它放在功能和第一个场景之间。
谢谢!
答案 0 :(得分:1)
您可以将它放在功能和方案之间,就在您想要的位置,但将其设为lazy val
。这样,它将在两种情况下都在范围内,但仅在第一次使用时初始化,第一种情况执行时。
麻烦的是,你不需要在最后关闭它吗?如果是这样,您可以将其移至顶层,保持懒惰,然后使用BeforeAndAfterAll
的{{1}}方法关闭它。 (如果您有其他功能可以懒惰地初始化资源,那么您的afterAll()
方法也可以关闭它们。)
答案 1 :(得分:0)
根据FeatureSpec
http://doc.scalatest.org/2.2.6/#org.scalatest.FeatureSpec的文档
假设提供此类功能有两个特征:
BeforeAndAftereEach
& BeforeAndAfterAll
但是它们不会在你的情况下工作,因为其中一个在每个场景之前执行,而另一个在测试类中只执行一次。
在同一页面上,他们为您建议实现方法(withDriver
,并在第一行功能中明确调用它。)