Code Examples for

Programming in Scala, Second Edition

Return to chapter index

14 Assertions and Unit Testing

  • 14.1 Assertions
  • 14.2 Unit testing in Scala
  • 14.3 Informative failure reports
  • 14.4 Using JUnit and TestNG
  • 14.5 Tests as specifications
  • 14.6 Property-based testing
  • 14.7 Organizing and running tests
  • 14.8 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 Unit testing in Scala


    import org.scalatest.Suite import Element.elem class ElementSuite extends Suite { def testUniformElement() { val ele = elem('x', 2, 3) assert(ele.width == 2) } }
    scala> (new ElementSuite).execute() Test Starting - ElementSuite.testUniformElement Test Succeeded - ElementSuite.testUniformElement
    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) } }

    14.3 Informative failure reports


    assert(ele.width === 2)
    expect(2) { ele.width }
    intercept[IllegalArgumentException] { elem('x', -2, 3) }
    Expected IllegalArgumentException to be thrown, but NegativeArraySizeException was thrown.

    14.4 Using JUnit and TestNG


    import junit.framework.TestCase import junit.framework.Assert.assertEquals import junit.framework.Assert.fail import Element.elem class ElementTestCase extends TestCase { def testUniformElement() { val ele = elem('x', 2, 3) assertEquals(2, ele.width) assertEquals(3, ele.height) try { elem('x', -2, 3) fail() } catch { case e: IllegalArgumentException => // expected } } }
    import org.scalatest.junit.JUnit3Suite import Element.elem class ElementSuite extends JUnit3Suite { def testUniformElement() { val ele = elem('x', 2, 3) assert(ele.width === 2) expect(3) { ele.height } intercept[IllegalArgumentException] { elem('x', -2, 3) } } }
    import org.testng.annotations.Test import org.testng.Assert.assertEquals import Element.elem class ElementTests { @Test def verifyUniformElement() { val ele = elem('x', 2, 3) assertEquals(ele.width, 2) assertEquals(ele.height, 3) } @Test( expectedExceptions = Array(classOf[IllegalArgumentException]) ) def elemShouldThrowIAE() { elem('x', -2, 3) } }
    import org.scalatest.testng.TestNGSuite import org.testng.annotations.Test import Element.elem class ElementSuite extends TestNGSuite { @Test def verifyUniformElement() { val ele = elem('x', 2, 3) assert(ele.width === 2) expect(3) { ele.height } intercept[IllegalArgumentException] { elem('x', -2, 3) } } }

    14.5 Tests as specifications


    import org.scalatest.FlatSpec import org.scalatest.matchers.ShouldMatchers import Element.elem class ElementSpec extends FlatSpec with ShouldMatchers { "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 { evaluating { elem('x', -2, 3) } should produce [IllegalArgumentException] } }
    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 array must have length 3 map must contain key 'c'
    Map('a' -> 1, 'b' -> 2) did not contain key 'c'
    import org.specs._ 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] } } }

    14.6 Property-based testing


    import org.scalatest.WordSpec import org.scalatest.prop.Checkers import org.scalacheck.Prop._ import Element.elem class ElementSpec extends WordSpec with Checkers { "elem result" must { "have passed width" in { check((w: Int) => w > 0 ==> (elem('x', w, 3).width == w)) } "have passed height" in { check((h: Int) => h > 0 ==> (elem('x', 2, h).height == h)) } } }
    w > 0 ==> (elem('x', w, 3).width == w)

    14.7 Organizing and running tests


    $ scala -cp scalatest-1.2.jar org.scalatest.tools.Runner -p "scalatest-1.2-tests.jar" -s org.scalatest.SuiteSuite

    14.8 Conclusion

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

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

    and:

    http://booksites.artima.com/programming_in_scala_2ed

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