Code Examples for

Programming in Scala, Fifth Edition

Return to chapter index

25 Assertions and Tests

  • 25.1 Assertions
  • 25.2 Testing in Scala
  • 25.3 Informative failure reports
  • 25.4 Tests as specifications
  • 25.5 Property-based testing
  • 25.6 Organizing and running tests
  • 25.7 Conclusion
  • 25.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 then 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)

    25.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

    25.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")

    25.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, 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, 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 } }

    25.5 Property-based testing


    // In file assertions-and-tests/ex2/ElementSpec.scala import org.scalatest.wordspec.AnyWordSpec import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks import org.scalatest.matchers.must.Matchers.* import Element.elem class ElementSpec extends AnyWordSpec, 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) }

    25.6 Organizing and running tests


    name := "ThankYouReader!" scalaVersion := "3.0.0" libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.9" % "test"
    $ sbt [info] welcome to sbt 1.5.2 (AdoptOpenJDK Java 1.8.0_262) ... sbt:ThankYouReader!>

    25.7 Conclusion

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

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

    and:

    http://booksites.artima.com/programming_in_scala_5ed

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