Titles are an always food...
So I've got a bit of time on my hands to pursue some nice side (though valuable) projects. The main one involves some JavaScript and I'm painfully aware that I don't have any tests on the code I've written. So, I've gone and downloaded JSUnit which I've used on one other protect.
The first thing I've done is run the TestSuite for JSUnit, which seems like a reasonable thing that should just work. But it fails! Grrr, what is going on here. I've run it in FireFox, so, just for completeness, I run in it IE. Guess what? That's right, it passes. Closer investigation reveals that the setUp and tearDown tests are the two culprits.
Time to dive a bit deeper. First of all, here's the offending code:
// JsUnit setUp/tearDown/test order tests
var aVar=null;
var anotherVar=null;
function setUp() {
aVar="foo";
}
function tearDown() {
anotherVar="bar";
}
function testSetUp() {
assertEquals("foo", aVar);
assertEquals(null, anotherVar);
}
function testTearDown() {
assertEquals("foo", aVar);
assertEquals("bar", anotherVar);
}
It looks fairly reasonable until you realize that these tests are dependent on the order in which they run. No prizes for guessing which order they are run on FireFox. A quick search on Google didn't reveal this as a problem that anyone else has run into, hence this post.
Here, for all its worth, is my fix:
// JsUnit setUp/tearDown/test order tests
var aVar=null;
var anotherVar=null;
var testSetup = true;
function setUp() {
aVar="foo";
}
function tearDown() {
anotherVar="bar";
}
function testOne() {
helper();
}
function testTwo() {
helper();
}
function helper() {
if (testSetup) {
testSetup = false;
setUpTest();
} else {
tearDownTest();
}
}
function setUpTest() {
assertEquals("foo", aVar);
assertEquals(null, anotherVar);
}
function tearDownTest() {
assertEquals("foo", aVar);
assertEquals("bar", anotherVar);
}
While a bit more complicated, these tests don't depend on which order they are run. I've moved the original tests to be assertion functions. Then I've created two simple tests that both call a helper function. The helper function determines when it is called whether it is the first or second invocation and calls the appropriate assertion function with the right assertions.
What's this all mean
- If you ship code with test, please make sure the tests pass. (In all fairness, FireFox may have changed since this was released more than two years ago).
- Your tests really shouldn't ever ever ever ever rely on the order they are run. I don't care what you think, they shouldn't. Each test should be able to run independently and be valid. My modified tests do this, though to get the full intent of the test, both tests need to be run.
Now, back to my regularly unscheduled development...

0 Comments:
Post a Comment
Links to this post:
Create a Link
<< Home