Code Examples for

Programming in Scala

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)
    java.lang.IllegalArgumentException: assumption failed: must be non-zero

    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(classOf[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(classOf[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 { val 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(classOf[IllegalArgumentException]) { elem('x', -2, 3) } } }

    14.5 Tests as specifications


    import org.scalatest.Spec class ElementSpec extends Spec { "A UniformElement" -- { "should have a width equal to the passed value" - { val ele = elem('x', 2, 3) assert(ele.width === 2) } "should have a height equal to the passed value" - { val ele = elem('x', 2, 3) assert(ele.height === 3) } "should throw an IAE if passed a negative width" - { intercept(classOf[IllegalArgumentException]) { 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
    import org.specs._ 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(new IllegalArgumentException) } } }

    14.6 Property-based testing


    import org.scalatest.prop.FunSuite import org.scalacheck.Prop._ import Element.elem class ElementSuite extends FunSuite { test("elem result should have passed width", (w: Int) => w > 0 ==> (elem('x', w, 3).width == w) ) test("elem result should have passed height", (h: Int) => h > 0 ==> (elem('x', 2, h).height == h) ) }
    w > 0 ==> (elem('x', w, 3).width == w)
    import org.scalatest.junit.JUnit3Suite import org.scalatest.prop.Checkers import org.scalacheck.Prop._ import Element.elem class ElementSuite extends JUnit3Suite with Checkers { def testUniformElement() { check((w: Int) => w > 0 ==> (elem('x', w, 3).width == w)) check((h: Int) => h > 0 ==> (elem('x', 2, h).height == h)) } }

    14.7 Organizing and running tests


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

    14.8 Conclusion

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

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

    and:

    http://booksites.artima.com/programming_in_scala

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