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
More detailed documentation can be found in the unittest documentation.
Create our first unit test¶
First, we create a new file in the
Let's call it
# -*- 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:
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
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).
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.
# -*- coding: utf-8 -*- def double_number(x): return x * 2
# -*- 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)