Code Examples for

Programming in Scala, Third Edition

Return to chapter index

14 Assertions and Tests

  • 14.1 Assertions
  • 14.2 Testing in Scala
  • 14.3 Informative failure reports
  • 14.4 Tests as specifications
  • 14.5 Property-based testing
  • 14.6 Organizing and running tests
  • 14.7 Conclusion
  • 14.1 Assertions


    def above(that: Element): Element = { val this1 = this widen that.width val that1 = that widen this.width assert(this1.width == that1.width) elem(this1.contents ++ that1.contents) }
    private def widen(w: Int): Element = if (w <= width) this else { val left = elem(' ', (w - width) / 2, height) var right = elem(' ', w - width - left.width, height) left beside this beside right } ensuring (w <= _.width)

    14.2 Testing in Scala


    import org.scalatest.FunSuite import Element.elem class ElementSuite extends FunSuite { test("elem result should have passed width") { val ele = elem('x', 2, 3) assert(ele.width == 2) } }
    scala> (new ElementSuite).execute() ElementSuite: - elem result should have passed width

    14.3 Informative failure reports


    scala> val width = 3 width: Int = 3 scala> assert(width == 2) org.scalatest.exceptions.TestFailedException: 3 did not equal 2
    scala> assert(List(1, 2, 3).contains(4)) org.scalatest.exceptions.TestFailedException: assert(List(1, 2, 3).contains(4)) | | | | | | | 1 2 3 false 4 List(1, 2, 3)
    assertResult(2) { ele.width }
    assertThrows[IllegalArgumentException] { elem('x', -2, 3) }
    Expected IllegalArgumentException to be thrown, but NegativeArraySizeException was thrown.
    val caught = intercept[ArithmeticException] { 1 / 0 } assert(caught.getMessage == "/ by zero")

    14.4 Tests as specifications


    import org.scalatest.FlatSpec import org.scalatest.Matchers import Element.elem class ElementSpec extends FlatSpec with Matchers { "A UniformElement" should "have a width equal to the passed value" in { val ele = elem('x', 2, 3) ele.width should be (2) } it should "have a height equal to the passed value" in { val ele = elem('x', 2, 3) ele.height should be (3) } it should "throw an IAE if passed a negative width" in { an [IllegalArgumentException] should be thrownBy { elem('x', -2, 3) } } }
    scala> (new ElementSpec).execute() A UniformElement - should have a width equal to the passed value - should have a height equal to the passed value - should throw an IAE if passed a negative width
    result must be >= 0 map must contain key 'c'
    Map('a' -> 1, 'b' -> 2) did not contain key 'c'
    import org.specs2._ import Element.elem object ElementSpecification extends Specification { "A UniformElement" should { "have a width equal to the passed value" in { val ele = elem('x', 2, 3) ele.width must be_==(2) } "have a height equal to the passed value" in { val ele = elem('x', 2, 3) ele.height must be_==(3) } "throw an IAE if passed a negative width" in { elem('x', -2, 3) must throwA[IllegalArgumentException] } } }
    import org.scalatest._ class TVSetSpec extends FeatureSpec with GivenWhenThen { feature("TV power button") { scenario("User presses power button when TV is off") { Given("a TV set that is switched off") When("the power button is pressed") Then("the TV should switch on") pending } } }

    14.5 Property-based testing


    import org.scalatest.WordSpec import org.scalatest.prop.PropertyChecks import org.scalatest.MustMatchers._ import Element.elem class ElementSpec extends WordSpec with PropertyChecks { "elem result" must { "have passed width" in { forAll { (w: Int) => whenever (w > 0) { elem('x', w, 3).width must equal (w) } } } } }
    whenever (w > 0) { elem('x', w, 3).width must equal (w) }

    14.6 Organizing and running tests


    $ scalac -cp scalatest.jar TVSetSpec.scala
    $ scala -cp scalatest.jar org.scalatest.run TVSetSpec

    14.7 Conclusion

    For more information about Programming in Scala, Third Edition (the "Stairway Book"), please visit:

    http://www.artima.com/shop/programming_in_scala_3ed

    and:

    http://booksites.artima.com/programming_in_scala_3ed

    Copyright © 2007-2016 Artima, Inc. All rights reserved.

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.