0

I am trying to run only unit tests in my Play application. I came across multiple posts and links that with example but none seem to work. I tried this and this. Has something changed in 2.2.1?

Community
  • 1
  • 1
Prasanna
  • 3,483
  • 7
  • 41
  • 71

1 Answers1

0

Those posts are a little dated, and while I can't speak to the accepted answers, some of the other answers will still work. Play and specs2 have had some relatively major changes since 2012 though, so I'll provide an example that I know will work today (and I use daily).

I do two things to make it easier to isolate tests. First, I namespace all of my test classes. Integration tests under the package test.integration, and unit tests under test.unit. I actually go a little beyond that, e.g.: test.unit.models, test.unit.controllers.users, etc.

I can then run all of my model tests with: test-only test.unit.models.*

I also find it helpful to tag each individual test case, either by the name of the function I'm testing, or some other helpful descriptor.

package test.unit.models

import org.specs2.mutable._
import play.api.test._
import play.api.test.Helpers._
import models.User

object UserSpec extends Specification {

     "The user model" should {

         tag("create")
         "successfully create a new user" in new WithApplication {

               ....

         }

         ...
     }

 }

Now I can have much more fine grained control over which tests are run with:

 test-only test.unit.models.UserSpec -- include create

Or perhaps I want to run all unit tests except those tagged "create":

 test-only test.unit.* -- exclude create

The wild-card can also be placed anywhere in the full package name. For example, if you have two model specs: test.unit.models.UserSpec and test.unit.models.UserCountrySpec, test-only test.unit.models.User* would run both.

Michael Zajac
  • 53,182
  • 7
  • 105
  • 130