Unit testing

During the codeX bootcamp you wrote various functions that range from checking registration numbers, checking dates, greeting your neighbour, checking airtime and helping your friend with his WordCrush fixation. You wrote the functions the bootcamp web application checked that your functions returned the expected results.

You are now going to take over and test the functions you wrote during the bootcamp with unit tests.

What is Unit testing?

Unit testing is a testing technique that test small sections of your code - each section being called a ‘unit’. Unit tests should be fast and easy to run.

In the bootcamp each function was a testable unit with an expected outcome for a given input. Each function had to pass unit tests for the function to be marked as completed - then it went green.

What does a Unit Test look like?

Unit Tests look different depending on the framework that you are using. You will be using Mocha

A Mocha unit test for the Bootcamp’s greet function looks like this:


describe('The greet function', function(){

    it('should greet Andrew correctly', function(){
        assert.equal('Hello, Andre', greet('Andrew'));
    });
    it('should greet Karen correctly', function(){
        // this test will fail - can you fix it?
        assert.equal('Hello, Karen', greet('André'));
    });
});

The unit test is doing a few things:

Over to you

Next you will learn how to configure Mocha and then you will write some unit tests for your Bootcamp functions.