Scala

 

Test meetup Houten 

June 29, 2015

An introduction for technical testers

Test

Joost van Dieten

Ka Yip Cheung

Vision on testing

Tester

Developer

Setup CI

Review Unit Tests

Exploratory Testing

Defect management

New Test Cases

Write scripts

Setup Test tools

New Code

Fix defects

Write Unit Tests

Review System Tests

Automate tests

Scala explained with Java :-)

public class Person {

    private int age;
    private String name;

    public Person(int age, String name){
        this.age = age;
        this.name = name;
    }

    public int getAge(){
        return this.age;
    }

    public void setAge(int age){
        this.age = age;
    }
    
    public String getName(){
        return this.name;
    }

    public void setName(String name){
        this.name = name;
    }

}
class Person(var age: Int, var name: String)

Let's test the Person class

class PersonSpec {

}

What ScalaTest has to offer...

Designed to increase team productivity through simple clear tests and executable specifications 

Suitable for all test types unit test, system test, integration test, acceptance test 

7 style traits to pick from 

Style traits

  • ScalaTest supports different styles of testing
  • Each style addresses a particular set of needs
  • Team can define a style or set of styles to use
  • Only determines test specifications format

The style traits to pick from

FunSuite

 
test("An empty Set should have size 0") {
    assert(Set.empty.size == 0)
}

The style traits to pick from

FlatSpec

 
"An empty Set" should "have size 0" in {
    assert(Set.empty.size == 0)
  }

The style traits to pick from

FunSpec

 
  describe("A Set") {
    describe("when empty") {
      it("should have size 0") {
        assert(Set.empty.size == 0)
      }

      it("should produce NoSuchElementException when head is invoked") {
        intercept[NoSuchElementException] {
          Set.empty.head
        }
      }
    }
  }

The style traits to pick from

WordSpec

 
 "A Set" when {
    "empty" should {
      "have size 0" in {
        assert(Set.empty.size == 0)
      }

      "produce NoSuchElementException when head is invoked" in {
        intercept[NoSuchElementException] {
          Set.empty.head
        }
      }
    }
  }

The style traits to pick from

FreeSpec

 
  "A Set" - {
    "when empty" - {
      "should have size 0" in {
        assert(Set.empty.size == 0)
      }

      "should produce NoSuchElementException when head is invoked" in {
        intercept[NoSuchElementException] {
          Set.empty.head
        }
      }
    }
  }

The style traits to pick from

Spec

 
  object "A Set" {
    object "when empty" {
      def "should have size 0" {
        assert(Set.empty.size == 0)
      }

      def "should produce NoSuchElementException when head is invoked" {
        intercept[NoSuchElementException] {
          Set.empty.head
        }
      }
    }
  }

The style traits to pick from

  val examples =
    Table(
      "set",
      BitSet.empty,
      HashSet.empty[Int],
      TreeSet.empty[Int]
    )

  property("an empty Set should have size 0") {
    forAll(examples) { set =>
      set.size should be (0)
    }
  }

PropSpec

The style traits to pick from

info("As a TV set owner")
  info("I want to be able to turn the TV on and off")
  info("So I can watch TV when I want")
  info("And save energy when I'm not watching TV")

  feature("TV power button") {
    scenario("User presses power button when TV is off") {

      Given("a TV set that is switched off")
      val tv = new TVSet
      assert(!tv.isOn)

      When("the power button is pressed")
      tv.pressPowerButton()

      Then("the TV should switch on")
      assert(tv.isOn)
    }
}

FeatureSpec

Our PersonSpec test

class PersonSpec extends FlatSpec {

    
}

We have Matchers!

ScalaTest provides a domain specific language (DSL) for expressing assertions in tests using the word should

result should equal (3) // can customize equality
result should === (3)   // can customize equality and enforce type constraints
result should be (3)    // cannot customize equality, so fastest to compile
result shouldEqual 3    // can customize equality, no parentheses required
result shouldBe 3       // cannot customize equality, so fastest to compile, no parentheses required

Finish our first unit test

class PersonSpec extends FlatSpec with Matchers {

    it should "init new Person with age and name" in {
       val joost = new Person(28,"Joost")
    }
}

Functional testing

We work in a scrum project with user stories. How cool would it be te be able to map those stories 1 on 1 to functional acceptance tests. 

We can do it with ScalaTest!

  • Besides matchers we can use scenario based testing template
  • GivenWhenThen structure used in our demo project

What we learned

  • Entire team can collaborate easily
  • Parellel testing supported out of the box
  • Integration with Selenium
  • Learning curve higher compared to xebium/fitnesse
  • ScalaTest runs tests in parallel default (in our project compared to Xebium/FitNesse)
  • Everything in ONE file :-).
  • Integrates with Java/Maven and Scala/SBT

 

Questions?

....

ScalaTest

By Joost van Dieten

ScalaTest

Introduction for Technical testers

  • 381