Automated testing: Difference between revisions

From OpenHatch wiki
Content added Content deleted
imported>Blues chick
(Replaced content with "{{Hacking OpenHatch}} This page is now included in our project package, and is automatically generated by sphinx at openhatch.readthedocs.org: [http://openhatch.readthedo...")
 
(25 intermediate revisions by 17 users not shown)
Line 1: Line 1:
{{Hacking OpenHatch}}
{{Hacking OpenHatch}}


This page is now included in our project package, and is automatically generated by sphinx at openhatch.readthedocs.org: [http://openhatch.readthedocs.org/en/latest/advanced/advanced_testing.html Advanced Testing]
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.

Latest revision as of 15:30, 22 August 2014

This is a page about improving or modifying OpenHatch.

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


This page is now included in our project package, and is automatically generated by sphinx at openhatch.readthedocs.org: Advanced Testing