Wednesday, March 18, 2009

Some cool new(-ish) NUnit features

I haven't looked at NUnit for a long time, and things have moved on a lot since I last used it. I particularly like the support for parameterised tests. For example, the TestCase attribute can be used to specify multiple iterations for a particular test, e.g.:


[TestCase(CalculatorMode.Standard)]
[TestCase(CalculatorMode.Scientific)]
public void TestAdd(CalculatorMode mode)
{
Calculator calc = new Calculator(mode);
int res = calc.Add(10,20);
Assert.That(res, Is.EqualTo(30));
}


This test will be executed twice, once with the calculator in 'standard' mode, and once with the calculator in 'scientific' mode.

We can take this a step further and use the Values and Range attributes to expand our set of tests:


[Test]
public void TestAdd(
[Values(CalculatorMode.Standard, CalculatorMode.Scientific)] CalculatorMode mode,
[Range(0,5,1)] int x,
[Range(0,5,1)] int y
)
{
Calculator calc = new Calculator(mode);
int res = calc.Add(x,y);
Assert.That(res, Is.EqualTo(x+y));
}


This test will run a total of 72 times. The range of x and y are both 0 to 5, with an interval of 1 (i.e. the values 0,1,2,3,4,5), giving 6x6=36 tests, which are then combined with our two modes to give a total of 72 iterations.

The multiple iterations show up as separate tests in your test runner, with the names derived from the arguments used for each run, so it's very easy to see what's going on.

Also you may have noticed the constraint-based Assert syntax used in the examples above. I was a little unsure of it at first, but it's growing on me. It uses a 'fluent' style to make the Assert more readable, so:


Assert.That(res, Is.EqualTo(x+y));


is very close to the English "Assert that res is equal to x+y". There is a lot of debate about the merits of fluent APIs, but in this context I like it.

One constraint that really caught my eye is 'After'. It's useful when testing asynchronous code where you need to wait for a condition to become true. For example:


Assert.That(() => Status, Is.EqualTo("Done").IgnoreCase.After(5000, 50));


What this says is "Assert that status is 'done' (case insensitive) after a maximum of 5 seconds, checking every 50 ms". The chaining of constraints like this is very readable, and the fact that relatively sophisticated constraints like After are built-in is very helpful. (I actually wrote my own version of this asynchronous wait/poll functionality recently, but I can throw it away now and use After instead.)

Finally if you derive from the AssertionHelper class, you can cut down the verbosity slightly, and re-write this Assert as:


Expect(() => Status, EqualTo("Done").IgnoreCase.After(5000,50));


Let me know if you've found any other cool NUnit features that I might have missed.

Saturday, March 10, 2007

Given up on Vista

So after a couple of months using Vista on my new laptop, I've given up and gone back to XP. The last straw was when Vista wouldn't recognise my new digital camera, even though it's just a standard USB disk device. After an hour or so of futile poking around in device manager, I finally did a google search and someone recommended telling Vista to look in c:\windows\system32 for the drivers it needs. Incredibly that worked, but by that time I was so furious I made up my mind to reinstall XP the next night.

It's *so* good to be back on XP. Everything is faster, my disk isn't constantly churning, my sound works properly (no stuttering), I can login using my fingerprint reader again, and Windows Explorer works properly. The only things I miss are the sidebar (solved by installing desktop sidebar), and the search in the start menu (which I can easily live without).

Don't get me wrong, Vista is not terrible. It's just that for me on this particular laptop, the pain outweighs the gain. Maybe I'll try it again in a year or two, when we've had a couple of service packs and the drivers are all sorted out.

Tuesday, January 09, 2007

First impressions of Zune

Got a free Microsoft Zune as part of a promotion related to my website, so I thought I'd post some thoughts.

Overall I have to say it's a big disappointment. First of all, you can't use it as an external drive, so you can't store abitrary data on it. There is a hacky workaround circulating on the net, but it's very kludgy. This limitation alone means I'd never recommend anyone buy one.

The second problem is the PC software (to sync your media with the Zune). The interface is truly awful. I found myself having to consult the help to work out how to do the most basic things. And the help isn't much good either. Explaining this interface to a non-technical relative doesn't bear thinking about. So think twice before buying one as a gift for someone.

I haven't bothered signing up for the online marketplace stuff, so I can't comment on that. But I read bad things about it, so I'm not really tempted to try.

As far the hardware goes, the screen is excellent - big and bright and very usable for looking at photos or even video. The controls are reasonably intuitive. The earphones are mediocre - not close to the quality of my 30 quid Sennheiser PX100 headphones. With decent headphones, the sound quality from the Zune seems fine - similar to my PC.

The software on the Zune itself is ok, but not great - the built-in themes are ugly (subjective, obviously), skipping between songs is slow, and the equalizer only has presets (no direct control over bass and treble). Oddly there seems to be hardly any noticable difference between the preset values - Rock has slightly more bass than Folk, for example, but it's only noticeable at very high volumes.

Add in the fact that there's no AC charger (I'm expected to take my laptop with me when travelling, obviously) and the whole Zune experience leaves a lot to be desired. I really can't see myself using it. Probably end up on ebay. Along with quite a few others, I imagine.

BTW I have no experience with any other similar devices (e.g. IPod), so maybe they all suffer from similar problems. I doubt it, though.