Skip to content
Django and Mocha unit tests (with a CLI test runner)
JavaScript Python
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
example initial commit Jan 9, 2012


Integrating Django and Mocha for great good.

But how?

Djamocha, lovingly named after a sort-of-gross-but-sometimes-appealing beverage from Arby's, provides a new management command: djamochatest and a metaclass DSL for defining tests.

How does it work?

  • Djamocha is designed to run a single browser against the entire test suite at a time, automatically.

Djamocha looks for subclasses of djamocha.Test using the same process that test looks for unit tests. Running djamochatest will run testserver in the background, and tell you to hit http://localhost:8000/idle/. When you do so, it will automatically open up a popup that cycles through all of your Djamocha test cases. When it finishes, your command line program will exit with the number of failures as its exit code. If you wish to run again, assuming your browser is still on the idle page, you can simply rerun the management command.

While a test suite is running, it assumes full control over the URL structure of your Django app -- you may provide a link to a, a patterns object, or nothing at all in your test -- and stub XHR-based views this way.

Djamocha automatically runs a static server that provides your Media, excluding your tests.

Your tests are pulled from a js directory alongside the test file containing the unit test, which allows you to keep your JavaScript tests separate from your static media.


The python side of things:

# myapp/tests/

import djamocha

class JSTest(djamocha.Test):
    # the media to be tested
    class Media:
        css = {}
        js = []

    # the routes that will be available while this 
    # test case is running
    routes = '' 

    # the tests to run against that media
    tests = ['some.test.js']

You may define as many of these as you like in a single file.

routes accepts a string (to import) or a patterns object. You may use this to stub out XHR requests (or perform full stack testing -- up to you!)

tests is a list of JavaScript files containing Mocha test suites to pull from the current test's adjacent js directory. You have access to jQuery, suite, test, and assert (patterned off of Node's assert module).

A look at our JavaScript:

// myapp/tests/js/some.test.js

suite("test", function() {
  test("hello world", function() {
    assert.equal(2, 1 + 1)


new BSD

Something went wrong with that request. Please try again.