It is good to have automated unit tests for the codes while developing. In unit test we test smallest unit of the code for a particular behaviour. The Unit test helps us to find bugs in the code early in the development cycle. Essentially unit test is the piece of code which verifies behaviour of a particular unit of the code in the development phase. Unit tests can be run by the test runner multiple times to verify behaviour of a particular unit of code for different set of input. Now a day’s most of the application development are adhering to Agile and TDD approach. In the test driven development approach, first you write a unit test, it will fail, then write application code to pass the test. Unit testing is not different in JavaScript than other programming languages. To do unit testing or TDD in JavaScript, you need a testing framework. There are many popular JavaScript testing framework available. Some of them are as follows:
- · Mocha
- · Jasmine
- · QUnit
- · JSUnit
In this article we will focus on QUnit. QUnit is a unit testing framework provided by the jQuery team. It provides a rich set of test assertions, highly informative test suite UI, support of synchronous and asynchronous call back, support of test module etc. In this post we will cover the following topics,
- Write first unit test
- Understating the test suite UI
- A look into the assertions
- Grouping the tests
Writing the first test
Let us start with setting up the QUnit for the JavaScript unit testing. You need to add reference of the two QUnit files on the HTML page. Either you can have files locally in the project or you can use the reference of the jQuery CDN. I am going to use the CDN option as shows below:
Leave a Reply