Monday, March 30th, 2009

Improved Readability for JavaScript Unit Tests

Category: JavaScript, Testing

<>p>Christian Johansen is tired of:

mentally decoding test names like “testNewUserWithClownShoesShouldSqueak”, so I’ve written some code to improve readability for JavaScript unit tests written using JsUnitTest (from prototypejs). It’s heavily inspired by Ruby frameworks Shoulda and Context.

Tests with his new framework–called jscontext–look like this:

  1. new Test.Unit.Runner({
  2.     "A new User": JsContext({
  3.         "with clown shoes": JsContext({
  4.  
  5.             "should squeak": function() { with(this) {
  6.  
  7.                 // ...
  8.             }},
  9.  
  10.             // ...more tests and/or contexts
  11.         }),
  12.  
  13.         "without clown shoes": JsContext({
  14.  
  15.             "should not squeak": function() { with(this) {
  16.  
  17.                 // ...
  18.             }},
  19.  
  20.             // ...more tests and/or contexts
  21.         }),
  22.  
  23.         // ...more tests and/or contexts
  24.  
  25.     }),
  26.  
  27.     // ...more tests and/or contexts
  28. }, { testLog: "testlog" });

Christian maintains that:

Using this, the test runner will label tests a lot more readable; where you may have been used to “testNewUserWithClownShoesShouldSqueak” you now get “A new user with clown shoes should squeak”.

Christian’s introduction/announcement blog post has a lot of additional details. Looks very cool!

Related Content:

Posted by Ben Galbraith at 9:07 am
2 Comments

+++--
3.1 rating from 17 votes

2 Comments »

Comments feed TrackBack URI

Looks similar to Choan Galvez’s projects:

http://jshoulda.scriptia.net/
http://sugartest.scriptia.net/

Comment by smith — March 30, 2009

Indeed they do, hadn’t seen them before. The only difference is the syntax. I liked the JShoulda syntax, but the Sugartest syntax did not appeal to me. Way too jQuery-ish (ie long chains).

It all boils down to taste though, the important part is that you test in a environment your comfortable with :)

Comment by cjohansen — March 30, 2009

Leave a comment

You must be logged in to post a comment.