The Spec
The Language
The Practice
The Books

Writing NUnit Tests in JScript

Microsoft's JScript allows you to make objects using either the traditional prototyping method, or the new class-based method. Microsoft was a very early adopter of some of the JS2 features, including the use of the class keyword.

An easy mistake to make when first attempting to write an NUnit test using JScript is to assume that you can somehow do it by defining a constructor function for your object. This will not work. I believe that this is because the interface of a .NET dll must be strongly typed. The JS2 features allow JavaScript to have the option of strong typing. This makes it possible to write a .NET DLL in JavaScript that implements a strongly typed component interface to the outside world.

In order to write a strongly-typed external interface that NUnit can see outside of your dll, you must define your test objects in classes rather than in constructor functions.

The second thing that is mysterious about JScript that NUnit testers must overcome, is the problem of marking your class with an attribute. Since NUnit requires classes that contain tests to be marked with the attribute "TestFixture" and each test to be marked with the "Test" attribute. We know that in VB an attribute name is surrounded in angle brackets. In C# an attribute is surrounded in square brackets like this: [TestFixture]. One of many surprising and easy features of JScript is how to specify an attribute.

To specify an attribute in JScript, all you have to do is type the name of the attribute in front of the word "class" in your class definition. There is no need for brackets or parenthesis or anything. Here is an example of an NUnit test definition in a JScript file called "myTest.js":

import NUnit.Framework;
import System;

TestFixture class myNewTest {
  Test function testTwoEqualsTwo () {
    Assert.AreEqual(2,2, "Does Two Still Equal Two? ");

As you can see in this example, JScript is probably the easiest language between VB, C# and JScript to use for specifying attributes. All you have to do is type them in front of the class.

The thing that makes JScript harder to use, at least at the time of this writing, is that you must do your compiling from the command-line. Visual Studio 2005 doesn’t support JScript compilation. JScript is well supported at the Microsoft .NET Framework level.

The Microsoft .NET Framework 2.0 contains good documentation on how to compile a JScript program at the command-line. I recommend using a resource file to store the command-line arguments and a separate .bat file to execute the build. To compile a .NET file at the command-line, you have to have to first be at the command-line. "Batch Files" execute their contents automatically at the command-line when you click on them in Windows. I created a new folder and created a text file which I renamed to "build.bat". Here is a sample of the "build.bat" contents:

C:\WINNT\Microsoft.NET\Framework\v2.0.50727\jsc @build.rsp

This single line of code in the build.bat file uses the JScript compile (jsc) with the command-line arguments in the "build.rsp" file.

A "resource file" is simply a file that holds a list of the command-line arguments you want to pass to the compiler. Here is the contents of "build.rsp", a sample of a resource file for building a test dll.

# build the first output file
/r:"C:\Program Files\NUnit 2.2\bin\nunit.framework.dll"

The first line of this file is a comment line. You can use "#" in a resource file to specify some text that you want to use as a comment. The next three lines that start with "/r:" specify references to dlls who's type information is required to perform the compile. The first one is to the nunit.framework.dll. The second one remains a mystery to me. I found that I was unable to compile my file without making an explicit reference to the Accessibility.dll. The third is the a reference to the System.dll which I expected would be necessary.

The fourth line tells the compiler to emit debugging information. This makes it possible for NUnit to show the line numbers of errors that occur during the test. The "/target" option tells the compiler that we want a dll and not an exe. The "/out" option allows us to name our dll. You can use a different name than the original .js file if you want. Finally we specify the "myTest.js" file that we want to compile.

Next, all you have to do is open NUnit and point it at the new myTest.dll that you created and the test will appear, ready to run.

Here is the official documentation on the command-line options in JScript.
Here’s the documentation on the JScript resource file.
Here’s the NUnit framework.

Troy Taft is the Principal Consultant and founder of Troy Taft Consulting, a firm specializing in high value software development. He also authors a free monthly newsletter called Software Matters.

Copyright 2007 Troy Taft All rights reserved, you may print this article for your personal use.