Code Examples for

Programming in Scala, Fourth 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


    // In file assertions-and-tests/ElementSuite.scala import org.scalatest.funsuite.AnyFunSuite import Element.elem class ElementSuite extends AnyFunSuite { 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


    // In file assertions-and-tests/ElementSpec.scala import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers import Element.elem class ElementSpec extends AnyFlatSpec 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] } } }
    // In file assertions-and-tests/TVSetSpec.scala import org.scalatest._ import org.scalatest.featurespec.AnyFeatureSpec class TVSetSpec extends AnyFeatureSpec 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


    // In file assertions-and-tests/ElementSpec2.scala import org.scalatest.wordspec.AnyWordSpec import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks import org.scalatest.matchers.must.Matchers._ import Element.elem class ElementSpec extends AnyWordSpec with ScalaCheckPropertyChecks { "elem result" must { "have passed width" in { forAll { (w: Int) => whenever (w > 0) { elem('x', w % 100, 3).width must equal (w % 100) } } } } }
    whenever (w > 0) { elem('x', w % 100, 3).width must equal (w % 100) }

    14.6 Organizing and running tests


    $ scalac -cp scalatest-app.jar:scala-xml.jar TVSetSpec.scala
    $ scala -cp scalatest-app.jar:scala-xml.jar org.scalatest.run TVSetSpec

    14.7 Conclusion

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

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

    and:

    http://booksites.artima.com/programming_in_scala_4ed

    Copyright © 2007-2019 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.