Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Jasmine Unit Testing With 100+ Tests Of The Same Type

I recently found Jasmine for unit testing, and it seems like a good solution for what I'm doing. However, I'm testing somewhere around 100 different possibilities, and I don't want to write the same line of code over and over.

So I made an object full of tests, and I'm looping around the unit test over and over with these tests. It prints out the correct number of tests when I run it. They all pass as shown below.

But then I changed "cero" to "cerFOOBARBAZ" and it still passes, which is wrong. Then I change 0 to an arbitrary number (993 for example) and it doesn't pass (and it shouldn't, but ALL tests fail.

What's up with that?

   var tests = {
        0 : "cero",
        1 : "uno",
        2 : "dos", 
        3 : "tres", 
        4 : "cuatro",
        5 : "cinco",
        6 : "seis", 
        7 : "siete",
        8 : "ocho",
        9 : "nueve",
        10 : "diez",
        11 : "once",
        12 : "doce",
        13 : "trece"

        };

describe("Numbers Return Correctly", function() {

    for(var test in tests) {
        it("Returns Correct String Based On Integer Input", function() {
            var number = parseInt(test);
            expect(number.convertNumToWord("es")).toEqual(tests[test]);
        });
    }
});

EDIT: I found out what was the problem. I was running the entire describe multiple times, not single specs.

However, when I do this:

    var tests = {
        0 : "cero",
        1 : "uno",
        2 : "dos", 
        3 : "tres", 
        4 : "cuatro",
        5 : "cinco",
        6 : "seis", 
        7 : "siete",
        8 : "ocho",
        9 : "nueve",
        10 : "diez",
        11 : "once",
        12 : "doce",
        13 : "trece"

        };

describe("Numbers Return Correctly", function() {


        //console.log(test);
        //console.log(tests[test]);
        it("Returns Correct String Based On Integer Input", function() {
            for(var test in tests) {
                var number = parseInt(test);
                expect(number.convertNumToWord("es")).toEqual(tests[test]);
            }
        });

});

I get the expected output, except there are no fine-grained details on which spec didn't pass. Any help there?

like image 659
freedomflyer Avatar asked Nov 18 '12 07:11

freedomflyer


People also ask

Is it possible to achieve 100 test coverage?

Yes, 100% Test coverage is possible. It always varies from application to application, the major factors that ensures test coverage are size of the application, complexity of the code and project bandwidth. Small the size of the application and more the coverage is achievable.

Do you need a unit test for every method?

Every behavior should be covered by a unit test, but every method doesn't need its own unit test. Many developers don't test get and set methods, because a method that does nothing but get or set an attribute value is so simple that it is considered immune to failure.

How much unit test coverage is enough?

While there is no standard for unit testing, one number often cited in the testing world is 80%. "Eighty percent is what I usually see as the gating standard for code coverage in corporate shops," said Tim Ottinger, a senior consultant at Industrial Logic. "Any higher or lower than that is unusual."


1 Answers

You need to create a closure for each of your test, otherwise in your first example you are testing only the last value a lot of time. A clean version of it would look like this :

describe("Numbers Return Correctly", function() {
    var tests = {  }; // ...

    function addTest(test) {
        it("Returns Correct String Based On Integer Input " + test, function() {
            var number = parseInt(test, 10);
            expect(number.convertNumToWord("es")).toEqual(tests[test]);
        });
    }

    for(var test in tests) {
        addTest(test);
    }
});
like image 181
HoLyVieR Avatar answered Nov 09 '22 03:11

HoLyVieR