Testing in Scala (I)

One of the elements that we haven’t spoken about before, and however, it turns to be one of the essentials, is testing in Scala and its frameworks.

Although JVM integration allows the use of testing frameworks based on Java, like JUnit, we will highlight two based on Scala: Scalatest y Scalacheck.

As you will see, there’s no need to be an expert to use them:

In this post, we will focus on the first of them.

ScalaTest

It’s a versatile testing framework that allows testing both Java and Scala code. It’s also remarkable its integration with powerful tools such as JUnit, TestNG, Ant, Maven, sbt, ScalaCheck, JMock, EasyMock, Mockito, ScalaMock, Selenium (browser testing automation), …

We suggest you to follow the next step to start using ScalaTest:

0. Create your flashing domain class.

case class Foo(n: Int){
  def +(m: Int): Foo = Foo(n+m)
  def -(m: Int): Foo = Foo(n-m)
}

1. Add Scalatest dependency to your SBT project.

libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.4" % "test"

…or the equivalent in Maven:

<dependency>
  <groupId>org.scalatest</groupId>
  <artifactId>scalatest_2.11</artifactId>
  <version>2.2.4</version>
  <scope>test</scope>
</dependency>

2. Choose your favourite testing stile…and create a base class for testing.

Scalatest offers several testing styles: property-based tests (PropSpec), descripted-language-based testing (WordSpec), simple styles (FunSpec), …
For this example, we will choose a pretty legible testing style like FlatSpec.
It’s quite recommendable to define a base class that gather all traits that we will use for our test classes, just in order to reuse that base class without too much boilerplate.

abstract class MyTestClass(
  entityToTest: String) extends FlatSpec
  with Matchers {

  behavior of entityToTest

}

The line

behavior of entityToTest

sets the name of the behavior that we are defining in our test class. Most of all, it’s due to pretty-printing.

Besides all this stuff, if we extend trait Matchers, we are able to express assertions to check like:

val n = 2
n should equal(1+1)
n should be(3-1)
n must be(0+2)
//...

3. Define the suitable test for your domain class.

We will check the test cases like this:

class FooTest extends MyTestClass("A Foo") {

  it should "add some value to its attribute N" in {
    val myFoo = Foo(0)
    (myFoo + 2) should equal(Foo(2))
    val anotherFoo = Foo(5)
    (anotherFoo + 3) should equal(Foo(8))
  }

  it should "substract some value to N" in {
    val myFoo = Foo(3)
    (myFoo - 2) should equal(Foo(1))
    (Foo(0) - 5) should equal(Foo(-5))
  }

}

Notice that test cases’ definitions are descriptive enough and provide such an ease to understand the behavior of Foo only by reading them.

4. Launch your test

From SBT:

test

Output should look like this:

Discovery starting.
Discovery completed in 34 milliseconds.
Run starting. Expected test count is: 2
FooTest:
A Foo
- should add some value to its attribute N
- should substract some value to N
Run completed in 61 milliseconds.
Total number of tests run: 2
Suites: completed 1, aborted 0
Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
All tests passed.

5. Rock and roll

b784c8abf46a00e87ee8365535645ba0

thisPost.isAwesome should be(true)
Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s