Automated testing

From OpenHatch wiki

This is a page about improving or modifying OpenHatch.

We call that "Hacking OpenHatch," and there is a whole category of pages about that.


The purpose of this page is to show you how to write automated tests within the OpenHatch codebase.

If you already know how software testing works, skip to Details specific to OpenHatch.

Tests: An overview

When you run:

./bin/mysite test

and you'll see a bunch of dots. Dots mean success.

This runs the many tests that are part of the OpenHatch code.

In general, you really should write a test if you add new functionality. This page explains how and when to write new tests and how to run the tests we have.

What a basic test looks like

Imagine this is in mysite/base/views.py:

def multiply(x, y):
    return x * y

Then this would be in mysite/base/tests.py:

import mysite.base.views

class TestMultiplication(django.test.TestCase):
    def test_return_one(self):
        # Make a dictionary that should return 1
        self.assertEqual(35,
                         mysite.base.views.multiply(7, 5))

Getting your local dev OpenHatch set up to run tests

To run tests correctly you'll need to have subversion installed -

$ apt-get install subversion

Then run the full suite of tests --

$ ./bin/mysite test

That's 2 celevr by half and 2x2 clever 4 me. Thanks!

General testing tips

How to write code that is easy to test

If you are writing a function, make it accept arguments for its data, rather having it calculate the input itself. For example:

Good

def multiply(x, y):
    return x * y

Less good

def multiply(x):
    y = settings.MULTIPLICATION_FACTOR
    return x * y

It's okay to rely on things like system settings and database content, but in general if your functions are simpler, they are easier to test.

For the love of God, keep writing these atcirles.