Chapter 9. Testing

Testing is the process of cross-checking the implementation of an application/process. It brings its shortcomings out into the open. It can be extremely handy when you are upgrading/downgrading one or more dependencies. Tests can be classified into various categories based on different programming practices, but in this chapter, we will only discuss two types of tests:

  • Unit tests: These are tests that check the functionality of a specific section of code
  • Functional tests: These are tests that check a specific action, mostly written to verify working code with regard to a use case or scenario

In the following sections, we will see the different ways in which we can test a Play application using Specs2 and ScalaTest.

Note

The tests using either of the Specs2 and ScalaTest libraries are similar. The major difference is in the keywords, syntax, and style. Since different developers can have different preferences, in this chapter, tests are defined using both libraries and for convenience. Most of the tests written using Specs2 have names ending with 'Spec', while those using ScalaTest end with 'Test'.

The setup for writing tests

Play is packaged with Specs2, since this is the library used internally for testing it. It provides support to test applications using Specs2 by default, that is, no additional library dependency is required.

Using ScalaTest earlier was difficult but now, Play also provides helpers for using ScalaTest. Although it is picked up from transitive dependencies, we need to add a library dependency to use the helper methods:

val appDependencies = Seq(
  "org.scalatestplus" %% "play" % "1.1.0" % "test"
)

Note

The 1.1.0 version of org.scalatestplus.play is compatible with Play 2.3.x. It is better to check the compatibility when working with another version of Play at http://www.scalatest.org/plus/play/versions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.205.235