问题描述
我的目标是使一个应用程序运行并在该应用程序的同一实例上执行多个测试.
My goal is to get an application running and execute multiple tests on the same instance of the app.
我尝试了此解决方案运气不好.这是我的测试:
I have tried this solution without much luck. Here is my test:
class ApplicationSpec extends Specification { sequential
object AppWithTestDb2 extends FakeApplication(additionalConfiguration =
Map(
("db.default.driver") -> "org.h2.Driver",
("db.default.url") -> (
// "jdbc:h2:mem:play-test-" + scala.util.Random.nextInt + // in memory database
"jdbc:h2:/tmp/play-test-" + scala.util.Random.nextInt + // file based db that can be accessed using h2-browse in activator
";MODE=PostgreSQL;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1")
))
running(AppWithTestDb2) {
"Application" should {
"send 404 on a bad request" in {
route(FakeRequest(GET, "/boum")) must beNone
}
"post signup request" in {
val res = route(FakeRequest(POST, "/user", FakeHeaders(), TestData.newUser)).get
status(res) must equalTo(OK)
contentType(res) must beSome("application/json")
contentAsJson(res) mustEqual TestData.newUser
}
"fail signup request for existing user" in {
val res1 = route(FakeRequest(POST, "/user", FakeHeaders(), TestData.newUser)).get
Await.result(res1, Duration.Inf)
val res = route(FakeRequest(POST, "/user", FakeHeaders(), TestData.newUser)).get
Await.result(res, Duration.Inf)
status(res) must equalTo(CONFLICT)
contentType(res) must beSome("application/json")
contentAsJson(res) mustEqual TestData.newUser
}
}
}
}
这里的问题是应用程序立即启动和停止,并且在没有运行应用程序的情况下执行测试:
The problem here is that application starts and stops immediately and tests are executed without a running application:
[debug] c.j.b.BoneCPDataSource - JDBC URL = jdbc:h2:/tmp/play-test--437407884;MODE=PostgreSQL;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1, Username = zalando, partitions = 1, max (per partition) = 30, min (per partition) = 5, idle max age = 10 min, idle test period = 1 min, strategy = DEFAULT
[info] application - Application has started
[debug] application - Binding to Slick DAO implementations.
[info] application - Application shutdown...
[debug] c.j.b.BoneCPDataSource - Connection pool has been shut down
[info] ApplicationSpec
[info]
[info] Application should
[info] ! send 404 on a bad request
[error] RuntimeException: : There is no started application (Play.scala:71)
[error] play.api.Play$$anonfun$current$1.apply(Play.scala:71)
[error] play.api.Play$$anonfun$current$1.apply(Play.scala:71)
[error] play.api.Play$.current(Play.scala:71)
[error] play.api.test.RouteInvokers$class.route(Helpers.scala:305)
[error] play.api.test.Helpers$.route(Helpers.scala:403)
[error] ApplicationSpec$$anonfun$1$$anonfun$apply$7$$anonfun$apply$8$$anonfun$apply$9.apply(ApplicationSpec.scala:76)
[error] ApplicationSpec$$anonfun$1$$anonfun$apply$7$$anonfun$apply$8$$anonfun$apply$9.apply(ApplicationSpec.scala:76)
[error] ApplicationSpec$$anonfun$1$$anonfun$apply$7$$anonfun$apply$8.apply(ApplicationSpec.scala:76)
[error] ApplicationSpec$$anonfun$1$$anonfun$apply$7$$anonfun$apply$8.apply(ApplicationSpec.scala:76)
推荐答案
在specs2中,测试声明和测试执行之间存在区别.编写"application" should ...
时,您只需声明测试.可执行部分是... in ...
部分中包含的内容.
In specs2 there is a distinction between test declaration and test execution. When you write "application" should ...
you just declare tests. The executable part is what is enclosed in the ... in ...
part.
因此,当您声明running(AppWithTestDb2) { ... }
时,您只需在AppTestDb2
应用程序的上下文中创建一些测试.
So when you declare running(AppWithTestDb2) { ... }
you just create some tests inside the context of an AppTestDb2
application.
要在specs2中实现的一般解决方案是使用Steps
这样:
The general solution for what you want to achieve in specs2 is to use Steps
like this:
class ApplicationSpec extends Specification { sequential
step("start application")
"Application" should {
"send 404 on a bad request" in { ... }
"post signup request" in { ... }
}
step("stop application")
}
然后,按照specs2执行模型的工作方式,您将在所有测试开始之前启动伪造的应用程序,并在所有测试完成后终止(无论是否使用sequential
)
Then, the way the specs2 execution model works, you will get your fake application started before all the tests start and terminated when all the tests are finished (whether you use sequential
or not)
我不是Play用户,但我怀疑您应该能够重用 WithApplication
类或类似的东西来创建您的开始/停止步骤.否则,有一个博客帖子此处探索相同问题的解决方案.
I am not a Play user but I suspect that you should be able to reuse the WithApplication
class or something similar to create your start/stop steps. Otherwise there is a blog post here exploring a solution for the same problem.
这篇关于如何在测试前启动Play应用程序,然后在specs2中将其关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!