Getting started with FuncUnit

Why FuncUnit

Writing tests is a chore and won't be done unless it's stupidly easy. Plus, tests won't get run unless they can be run automatically.

The problem with other automated testing solutions is that everyone has to install and configure cumbersome external software to even begin writing tests. After setting up the software, you write the tests with some obtuse API in a language other than JavaScript. But the worst part is debugging these tests across the software-browser bridge. The tests are almost impossible to debug!

Installation & First test

You can download standalone FuncUnit here or you can download complete JavaScriptMVC framework which includes FuncUnit by default. In this tutorial we'll use the standalone FuncUnit.

To run a FuncUnit test all you need to do is:

  1. Create HTML page (mypage_test.html) that loads qunit.css, funcunit.js and a test script for your page (mypage_test.js).
    <html>
      <head>
        <link href='../funcunit/qunit.css' type='text/css' rel='stylesheet' />
        <script src='../funcunit/funcunit.js type='text/javascript' >
        <script src='mypage_test.js' type='text/javascript'>
        <title>MyPage Test Suite</title>
      </head>
      <body>
        <h1 id="qunit-header">MyPage Test Suite</h1>
        <h2 id="qunit-banner"></h2>
        <div id="qunit-testrunner-toolbar"></div>
        <h2 id="qunit-userAgent"></h2>
        <ol id="qunit-tests"></ol>
      </body>
    </html>
  2. Create a JS file (pages/mypage_test.js) for your tests. The skeleton should like:
    module("APPNAME", {
      setup: function() {
        // opens the page you want to test
        S.open("myPage.html");
      }
    })
    
    test("page has content", function(){
      ok( S("body *").size(), "There be elements in that there body")
    })
  3. Open your html page (mypage_test.html) in a browser. Did it pass? If not check the paths.
    P.S. Your page and test files don't have to be in the same folder; however, on the filesystem, Firefox and Chrome don't let you access parent folders. We wanted the demo to work without having to host these files.
  4. Now run your test in Selenium. In Windows:
    > envjs ../../pages/mypage_test.html
    In Linux / Mac
    > ./envjs ../../pages/mypage_test.html
    This will run mypage_test.html on the filesystem. To run it served, jjust pass in the url of your test page:
    envjs http://localhost/pages/mypage_test.html

Congratulations! You've successfully installed FuncUnit and created first test.

Next steps

You can find out more about FuncUnit in these blog posts:

Make sure you also read the documentation, join the forum and follow @funcunit.

Javascriptmvc
© Jupiter IT - JavaScriptMVC Training and Support