Activate your free membership today | Log-in

Monday, March 30th, 2009

Improved Readability for JavaScript Unit Tests

Category: JavaScript, Testing

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:

JavaScript:
new Test.Unit.Runner({
    "A new User": JsContext({
        "with clown shoes": JsContext({

            "should squeak": function() { with(this) {

                // ...
            }},

            // ...more tests and/or contexts
        }),

        "without clown shoes": JsContext({

            "should not squeak": function() { with(this) {

                // ...
            }},

            // ...more tests and/or contexts
        }),

        // ...more tests and/or contexts

    }),

    // ...more tests and/or contexts
}, { 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!

Posted by Ben Galbraith at 9:07 am 2 Comments

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.