Unit tests – Testing Plone

Unit tests#

Unit tests are the first type that we are going to cover.

They are the quickest to run and the easiest to write because they don't need special environments or configuration to be executed.

We are going to use a Python unit testing library called unittest.

Note

More detailed documentation can be found in the unittest documentation.

Create our first unit test#

First, we create a new file in the tests folder.

Let's call it test_unit.py:

# -*- coding: utf-8 -*-

import unittest


class SomeTest(unittest.TestCase):
    def test_a_feature(self):
        self.assertTrue(1 == 1)
        self.assertEqual(1, 1)

Now we run it:

bin/test

If we are working on a single test and we don't want to run all our tests, we tell the testrunner which test to run:

bin/test -t test_a_feature

Exercise 1#

Create a new test and play with assertions (try also to make your assertions fail, to see what happens and to view the traceback information useful for debugging).

Exercise 2#

If in your package there are some helper methods that don't need CMS, Zope, or Plone features but they simply transform an input into an output, you could import them and test them in an unittest.

Create a new file in our package with a method that takes a number and returns this number multiplied by 2, and then write an unittest for this.

Solution

File helper_functions.py:

# -*- coding: utf-8 -*-

def double_number(x):
    return x * 2

In tests/test_unit.py:

# -*- coding: utf-8 -*-

from plonetraining.testing.helper_functions import double_number

import unittest


class SomeTest(unittest.TestCase):
    def test_a_feature(self):
        self.assertTrue(1 == 1)
        self.assertEqual(1, 1)

    def test_double_number(self):
        self.assertEqual(double_number(1), 2)
        self.assertEqual(double_number(2), 4)
        self.assertNotEqual(double_number(2), 3)