Saturday, 26 September 2009

jqRunner Snapshot - Running Javascript Unit test with NUnit

One of the nice things about unit tests is that you can use continuous integration so that you don't have to run them yourself. At work we use CruiseControl.NET to automate our builds and run our unit tests. Now as a number of our projects are web based we use a fair amount of javascript, mainly jQuery, though in-house plugins. In order to test these we use jqunit, this works great and allows us to use TDD when writing javascript. However as development goes on the unit tests get forgotten about as they are not automatically ran.

In order so solve this I decided to experiment to find a method of getting cruise control to run our jqunit tests for me. To run the tests I looked at wrapping the jqunit tests with nunit tests.

jqRunner

jqRunner is designed so that you can us your existing jqunit tests in their existing location without changing them. jqRunner required that all the scripts that are needed to run are registered. The full path to the file is required. jqRunner then executes the tests and returns the results, which are then parsed by nunit as tests using the TestCaseSource attribute.

var sampleTestCase = new jqUnit.TestCase('Sample Test Case', function() {
/*setup*/
// this.yep(1);
}, function() {
/*teardown*/
// this.ok(1)
});
sampleTestCase.test('Sample Test 1', function() {
this.ok(1);
});
sampleTestCase.test('Sample Test 2', function() {
this.ok(0);
});



using System;
using System.Collections.Generic;
using NUnit.Framework;
using jqRunner;
using System.IO;
using System.Reflection;

namespace jqRunner.Tests
{
[TestFixture,RequiresSTA]
public class TestCaseSourceTests
{
[Test, TestCaseSource("GetTestResults"), RequiresSTA]
public void CheckTest(ITestResult result)
{
Assert.IsTrue(result.Pass, result.Name);
}

private static ITestResult[] GetTestResults()
{
string jsFile = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase) + @"\jqUnitTests\SampleUnitTest.js";
TestBed target = new TestBed();
target.RegisterScript(jsFile);
return target.Execute().ToArray();
}
}
}



This is an initial development snapshot and is bound to have plenty of problems.

Download
http://static.yeticode.co.uk/blog/downloads/jqRunner-snapshot.zip

Related Reading











No comments:

Post a Comment