1

I often test the output of JavaScript functions manually (by simply looking at the output of each function in the console), and this can often be quite tedious. In JavaScript, is there any way to test the output of a series of function calls automatically, and return all tests that did not produce the expected results?

checkOutput([["add(1, 2)", 3], ["add(2, 2)", 4]]); //if the input does not match the output in one of these arrays, then return the function call(s) that didn't produce the correct output

function checkOutput(functionArray){
    //this function is not yet implemented, and should return a list of function calls that did not produce correct output (if there are any).
}

function add(num1, num2){
    return num1 + num2;
}
0

5 Answers 5

2

Seems as simple as looping, eval the first element of each array and compare it to the second.

function checkOutput(functionArray) {
    var check = function(e) {
        return eval(e[0]) !== e[1];
    };
    if( Array.prototype.filter)
        return functionArray.filter(check);
    // shim in older browsers
    var l = functionArray.length, i, out = [];
    for( i=0; i<l; i++)
        if( check(functionArray[i]))
            out.push(functionArray[i]);
    return out;
}
Sign up to request clarification or add additional context in comments.

3 Comments

There is a missing parentheses at this line: return eval(e[0]) !== e[1]);
Thanks for pointing that out - I had originally written it as an if, then realised I was writing if (something) return true else return false
Also, here is another automated testing method that I found: jsfiddle.net/jarble/S2wTJ
2

Absolutely. Use a Unit Testing suite, such as QUnit.

I like that one in particular, and it boasts that it's used by the various jQuery projects. That's a pretty solid endorsement.

A typical test would look something like this...

test( "add test", function(num1, num2) {
  ok( num1 + num2 == 42, "Passed!" );
});

And, if you don't like that suggestion, check out other Unit Testing frameworks at good ol' Wikipedia: JavaScript Unit Testing Frameworks.

2 Comments

Does QUnit do anything like this, by any chance? :)
Yes. As with any Unit Testing framework, you create assertions (expected == 1, etc.) and get results all at once.
1

Maybe you want switch to jasmine for testing javascript.

Comments

1

My testing library, suite.js, does something like this. Basically the syntax looks like this:

suite(add, [
    [[1, 2]], 3
]);

Usually I bind a parameter using partial application so my tests look like this:

suite(partial(add, 1), [
    -5, 4,
    2, 3
]);

And taken to the ultimate level I skip these tests altogether and define a contract based on which I generate tests. For this I use annotate.js. In this case I would end up with something like the following:

// implementation
function adder(a, b) {
    return a + b;
}

var add = annotate('add', 'adds things').on(is.number, is.number, adder);

// test some invariants now
suite(add, function(op, a, b) {
    return op(a, b) == op(b, a);
});

I know it's not a trivial amount of code anymore. This allows you to define invariants for function parameters, though, and based on that information we can generate tests, documentation etc.

suite.js works only in Node environment for now but if there is enough interest, I don't mind porting it to browser so you can run your tests there.

Comments

0

Look into QUnit. You can basically run tests on your functions all at once.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.