Play Famework 2 - Scala - Launch one test suite application

I followed Scala testing examples using Specs2 from the official Play documentation. I notice that they use WithApplication to run a fake app for testing against, with a clone, as shown below:

 "something" should { "do X" in new WithApplication { /* ... */ } "do Y" in new WithApplication { /* ... */ } "do Z" in new WithApplication { /* ... */ } } 

That’s good and that’s all, but the problem I am facing is that I take the cost of running my application every time this happens. This is not necessarily “fast” or at least not fast enough once your test suite grows to a reasonable size. I tried doing things like:

 val app = FakeApplication() "something" should { "do X" in new WithApplication(app) { /* ... */ } "do Y" in new WithApplication(app) { /* ... */ } "do Z" in new WithApplication(app) { /* ... */ } } 

and

 "something" should { val app = FakeApplication() Helpers.running(app) { "do X" in { /* ... */ } "do Y" in { /* ... */ } "do Z" in { /* ... */ } } } 

The first seems to work for the first test, and then complains about problems connecting to db in subsequent tests. I guess something is closing here or something (not sure what).

The second one does not work at all, because it complains about the lack of a running application, which I am not sure about either.

Any help is appreciated. Thanks!

+7
scala playframework specs2
source share
2 answers

Well, it depends on what you want to check. If you are just unit testing code that does not have external dependencies or dependencies that you can mock or stifle (and it would be nice to structure your code in such a way as to allow this), then you do not need to use WithApplication . This is probably the best approach.

The first solution you provided does not work, because applications can only be used once. It WithApplication starts and stops your application, so even if it works, you won’t get any performance benefit.

The second solution you provided does not work, because when the Helpers.running(app) { } code block is Helpers.running(app) { } , it is just a specification declaration. The specifications put it all on the list, and then you exit the running block and it disables the application. Then, at some point, the specifications run the tests, and then there is no application.

So, if you can’t test your code separately from the rest of your application, then you need to have the application running, and you can’t do anything about it, this is the reality of testing integration. And you probably want it to start and end between each test, otherwise your tests will not run in isolation from each other.

+4
source share

This is deprecated, but I will give my answer. Since I ran into the same problem and had a similar idea. There are signs of AfterAll and BeforeAll in spec2, maybe this was not at the time of publication, so my solution is basically:

 package com.equipx.spec.util import org.specs2.specification.{AfterAll, BeforeAll} import play.Application import play.api.Play import play.test.{Helpers, FakeApplication} /** * @author Anton Oparin ( antono@clemble.com ) */ trait WithGlobalApplication extends BeforeAll with AfterAll { protected var app: Application = null /** * Override this method to setup the application to use. * * By default this will call the old {@link #provideFakeApplication() provideFakeApplication} method. * * @return The application to use */ protected def provideApplication: Application = { return provideFakeApplication } /** * Old method - use the new {@link #provideApplication() provideApplication} method instead. * * Override this method to setup the fake application to use. * * @return The fake application to use */ protected def provideFakeApplication: FakeApplication = { return Helpers.fakeApplication } override def beforeAll { app = provideApplication Helpers.start(app) Play.current } override def afterAll { if (app != null) { Helpers.stop(app) app = null } } } 

Basically, I took the ApplicationApplication implementation and made it global.

+3
source share

All Articles