Testing your application
Play Scala comes with ScalaTest, that natively provide different styles of testing.
As in Play Java, you must place your test dedicated Scala sources into the test directory, and run the application using play test
to enable the integrated Test runner.
Please check the complete ScalaTest reference for more information.
JUnit Style
In this style you need to annotate any test method with the @Test
annotation.
class JUnitStyle extends UnitTestCase with AssertionsForJUnit {
@Before def setUp = Fixtures.deleteAll()
@Test def verifyEasy {
assert("A" == "A")
intercept[StringIndexOutOfBoundsException] {
"concise".charAt(-1)
}
}
}
JUnit Style with Should matchers
The ShouldMatchers
trait provides a domain specific language (DSL) for expressing assertions in tests using the word should.
class JUnitStyleWithShould extends UnitTestCase with ShouldMatchersForJUnit {
@Before def setUp = Fixtures.deleteAll()
@Test def verifyEasy {
val name = "Guillaume"
name should be ("Guillaume")
evaluating {
"name".charAt(-1)
} should produce [StringIndexOutOfBoundsException]
name should have length (9)
name should include ("i")
name.length should not be < (8)
name should not startWith ("Hello")
}
}
Functional suite Style
Here you will create a function for each test.
class FunctionsSuiteStyle extends UnitFunSuite with ShouldMatchers {
Fixtures.deleteAll()
test("Hello...") (pending)
test("1 + 1") {
(1 + 1) should be (2)
}
test("Something") {
"Guillaume" should not include ("X")
}
test("1 + 1 again") {
(1 + 1) should be (2)
}
}
Specification Style
This style provides a “behavior-driven” style of development (BDD), in which tests are combined with text that specifies the behavior the tests verify.
class SpecStyle extends UnitFlatSpec with ShouldMatchers {
val name = "Hello World"
"'Hello World'" should "not contain the X letter" in {
name should not include ("X")
}
it should "have 11 chars" in {
name should have length (11)
}
}
Features list Style
A suite of tests in which each test represents one scenario of a feature.
class FeatureStyle extends UnitFeatureSpec {
feature("The user can pop an element off the top of the stack") {
scenario("pop is invoked on a non-empty stack") (pending)
scenario("pop is invoked on an empty stack") (pending)
}
}