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











Wednesday, 16 September 2009

Pirate Day! Arr

So talk like a pirate day is coming around soon and saw a brilliant method of converting web pages from boring English to cool Pirate talk. So having nothing better to do at the moment I knocked up a small plugin that will convert your blog posts to pirate talk on the 19th, to give your readers something nice to look at.

Plugin can be found at http://wordpress.org/extend/plugins/pirate-talk/

Feel free to make it better.

Monday, 14 September 2009

Epic Fail, but with a good note.


So last Sunday was the 2009 Yorkshireman Off Road Marathon. Everything was going great, even though the course was nothing like I had run on before, but it was not meant to be. I injured my knee running down a hill and had to drop out at 18 mile mark. Although I failed Tom managed to man it up and complete it with enough energy at the end to call me gay whilst running up the last hill towards the finish line. So the entire event was not a complete waste of time.


Although I didn't complete it, I did however run further and for longer than previously. If I hadn't injured myself I'm absolutely positive that I would have completed the race. The course did give me areas that I need to work on, such as running off road more. And finding some really big ass hills to run up and down.


The failure to complete this marathon has certainly given me more than enough drive to enter the Brighton marathon and fully prepare myself for it so that I will be able to complete it without issue.