Thursday, October 25, 2012

ScalaTest is Awesome

I've been test-driving Java with JUnit and TestNG for about four years.  I've always hated that your test names are dictated by the rules of naming functions.  This yields some long and awkward function names such as:

@Test public void translatingDocumentToSpanishShouldConvertCatToGato() {...}
@Test public void givenTextIsCatAndLangIsSpanish_whenTranslate_thenReturnGato() {...}
Since Scala allows you to have anonymous functions and allows you to create Domain-Specific Languages, the makers of ScalaTest have come up with multiple solutions to cleaning up your ugly xUnit test suites.
Here is an example using Jasmine-style syntax:
describe("Translating from English to Spanish") {

    it("should translate [Cat] to [Gato]") {...}

  }
 
Here is an example using Cucmber-sytle syntax:


feature("Translate text from English to Spanish") {
 
    info("As a student who speaks English as a 2nd language")
    info("I want to be able to translate English to Spanish")
    info("So that I can make sure I understand my reading")
 
    scenario("translate simple single word") {
 
      given("[cat] in my English text") {...}
      when("I choose to translate to Spanish") {...}
      then("I see [gato]"){...}
    }
 
  }

You may ask "Why should we care if a test is pretty or ugly? Our customers are not asking for pretty tests!". The reason is that if you employ software developers, you are paying 80% of their salary for reading and understanding code, and only 20% for writing code. If you have a developer that can type 40 words-per-minute, and they are giving you 8 hours a day writing code at 40 WPM, that's not the developer you want in your shop.

There is much more. Note that ScalaTest can test Java code as well as Scala code. Visit http://scalatest.org/ to learn more.

No comments:

Post a Comment