Background Image
Table of Contents Table of Contents
Previous Page  255 / 478 Next Page
Information
Show Menu
Previous Page 255 / 478 Next Page
Page Background

2. Purely because it features the

NyanCat

test runner.

Not so in the JavaScript world! We use YUI at work, but I thought I’d go out and see

whether there were any new tools out there. I was overwhelmed with options—jsUnit,

Qunit, Mocha, Chutzpah, Karma, Testacular, Jasmine, and many more. And it doesn’t

end there either: as I had almost settled on one of them, Mocha,

2

I find out that I now

need to choose an

assertion framework

and a

reporter

, and maybe a

mocking library

, and

it never ends!

In the end I decided we should use

QUnit

because it’s simple, and it works well with

jQuery.

Make a directory called

tests

inside

lists/static

, and download the Qunit JavaScript and

CSS files into it, stripping out version numbers if necessary (I got version 1.12). We’ll

also put a file called

tests.html

in there:

$

tree lists/static/tests/

lists/static/tests/

├── qunit.css

├── qunit.js

└── tests.html

The boilerplate for a QUnit HTML file looks like this, including a smoke test:

lists/static/tests/tests.html.

<!DOCTYPE html>

<html>

<head>

<meta

charset=

"utf-8"

>

<title>

Javascript tests

</title>

<link

rel=

"stylesheet"

href=

"qunit.css"

>

</head>

<body>

<div

id=

"qunit"

></div>

<div

id=

"qunit-fixture"

></div>

<script

src=

"qunit.js"

></script>

<script>

/*global $, test, equal */

test

(

"smoke test"

,

function

() {

equal

(

1

,

1

,

"Maths works!"

);

});

</script>

</body>

</html>

Dissecting that, the important things to pick up are the fact that we pull in

qunit.js

using

the first

<script>

tag, and then use the second one to write the main body of tests.

Setting Up a Basic JavaScript Test Runner

|

227