The Artima Developer Community
Sponsored Link

Angle Brackets and Curly Braces
ScalaTest 0.9 Released
by Bill Venners
January 15, 2008
Summary
Today I released a testing tool written in Scala, which can be used to test Scala or Java code. It is intended primarily as a tool for testing Scala code, but can also be used as a low-risk way to get started programming in Scala, by writing tests in Scala for production code written in Java.

Advertisement

The last several months I've been working part time on a testing tool for Scala named ScalaTest. I did this for a few reasons. First, I wanted to start using Scala internally at Artima, and I felt the good place to start would be in writing tests. I looked around and didn't see a production-ready testing framework for Scala that took advantage of what Scala has to offer, so I felt there was a need. I also figured it would be a good project for me to start with, because I could get real experience in Scala by writing something “on the side”—in other words, something I didn't need to put into production right away. I wanted some experience in Scala before starting to try and integrate Scala into our web framework, in the hopes that I'd have a better chance of getting that right.

Some friends and I had written a test framework called SuiteRunner, starting in 2001. I released this tool open source in 2003, but never released it 1.0 because I realized that the Java community had settled on JUnit. Over time many of the problems that inspired me to create SuiteRunner were fixed in later versions of JUnit, and I took to recommending people use either JUnit or TestNG. Internally at Artima, however, we have used SuiteRunner for our unit testing since 2003, and it has worked quite nicely. Thus, I decided to try and “port” SuiteRunner to Scala as my first Scala project. The result is ScalaTest.

ScalaTest is not SuiteRunner code written in Scala syntax, however. Early on, when I was less experienced, I spent lengthy swaths of time staring at my Scala code trying to figure out how to write it without any “vars,” which are reassignable variables (like normal variables in Java). Over time I became better at it, and now programming with “vals,” which are like final variables in Java, feels very natural to me. I also used Option everywhere instead of null, pattern matching where useful, and even made a few new control structures using by-name parameters to get rid of code duplication. All these things were pretty straightforward for me coming from a Java background.

I also tried to eliminate mutable state. In SuiteRunner, Suite was a class that had quite a few instance variables. In ScalaTest, Suite is a trait, which by definition has no instance variables. In SuiteRunner, interface Reporter had a setConfiguration method, which pretty much pushes any class implementing that interface to maintain the most recent configuration as mutable state. ScalaTest's Reporter trait has no such method. Instead of configuring reporters directly, I wrap them internally in a FilterReporter whose constructor takes the configuration state.

Because I had a production-ready testing tool written in Java as my starting point, the result of my exercise is a testing tool written in Scala whose features rival those of JUnit 3, JUnit 4, and TestNG. ScalaTest seems to work just fine already, but I want to keep it under 1.0 (right now it is at release 0.9) for a few months of beta testing. My plan is to release it 1.0 in time for JavaOne in May.

So I encourage you to try out ScalaTest and give me feedback. I worked hard to document it thoroughly, so it should be pretty easy for you to figure out how to use. To satisfy your thirst for code, here's a simple example:

import org.scalatest.Suite

 class MySuite extends Suite {

   def testAddition() {
     val sum = 1 + 1
     assert(sum === 2)
     assert(sum + 2 === 4)
   }

   def testSubtraction() {
     val diff = 4 - 1
     assert(diff === 3)
     assert(diff - 2 === 1)
   }
 }

To figure out what that all means, the best place to start is the ScalaDoc documentation for Suite itself.

I have created a project for ScalaTest at java.net, but have not yet gotten it rolling. For the time being, please submit feedback to the ScalaTest Forum.

Talk Back!

Have an opinion? Readers have already posted 58 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Bill Venners adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Bill Venners is president of Artima, Inc., publisher of Artima Developer (www.artima.com). He is author of the book, Inside the Java Virtual Machine, a programmer-oriented survey of the Java platform's architecture and internals. His popular columns in JavaWorld magazine covered Java internals, object-oriented design, and Jini. Active in the Jini Community since its inception, Bill led the Jini Community's ServiceUI project, whose ServiceUI API became the de facto standard way to associate user interfaces to Jini services. Bill is also the lead developer and designer of ScalaTest, an open source testing tool for Scala and Java developers, and coauthor with Martin Odersky and Lex Spoon of the book, Programming in Scala.

This weblog entry is Copyright © 2008 Bill Venners. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use