False Positive Unit Test

If you are writing Unit or Integration test, then you are more than halfway there. There are situations however where the unit test you write can produce false positives.
False positives are when your test passes your initial smoke test but in actuality the test are failing. A good example is if you are testing and using the length property to make sure a returned value is great than 1.
For these examples, we are using http://chaijs.com/ and http://mochajs.org/.

Describe(“List – ”, function () {
    var list = [0,1,2],
        lists = “List Name”;
    it(“Returns length greater than 1 ”, function () {

At first glance, this test looks fine, actually it’s great and will always pass because the value is constant. But can you see what the problem is?
We’re testing for the length value for the lists String and not the array. A better test would be to, test the data type as well as the result. In our case, we can update our code by adding the line.
By adding a test for the correct data type, we would automatically see our test fail with the message expected “List Name” to be an array. Now we can see our false positive and fix our error.
We can still take it up a notch because right now we are only testing for the length and the data type. What if our Array was dynamic and changed lengths. That would make certain that our test works under random conditions.

var minNum = 1,
    maxNum = 15,
    i = 0,
    count = chance.integer({min: minNum, max: maxNum}),
    list = [];

for (i = 0; i < count; i++) {

expect(list).to.have.length.within(minNum, maxNum);

To do this let us refactor our test to create a dynamic array.


False positives can trip you up, and the only real way to prevent them is to think through your test. How should they work and how they should fail. In our example by adding a typeof check to ensure we’re testing the right data type, we can rest ensure our test are running effectively. The addition of the random array values is just icing on the cake, now we know without a doubt that if our code works with random numbers then it will work.