All test modules reside in the postorius/src/postorius/tests directory and this is where you should put your own tests, too. To make the django test runner find your tests, make sure to add them to the folder’s __init__.py:
from postorius.tests import test_utils
from postorius.tests.test_list_members import ListMembersViewTest
from postorius.tests.test_list_settings import ListSettingsViewTest
from postorius.tests.my_own_tests import MyOwnUnitTest
__test__ = {
"Test Utils": test_utils,
"List Members": ListMembersViewTest,
"List Settings": ListSettingsViewTest,
"My Own Test": MyOwnUnitTest,
}
When writing unittests make sure that any postorius.* imports are made at the test method level and not at the module level. Here’s why:
The Postorius documentation (the one you are reading right now) imports some doctest modules from the test package using Sphinx’s autodoc extension. This is a very nice feature, but in this scenario it has the nasty side effect of breaking the build process if application code is imported as well (it will fail to find an environment variable that Django needs to run). This can be easily prevented by avoiding module level imports of postorius code in the test modules.
Good:
from django.utils import unittest
from mock import patch
class SomeTest(unittest.TestCase):
def test_some_method(self):
from postorius.views import SomeViewClass
foo = 'bar'
Bad:
from django.utils import unittest
from mock import patch
from postorius.views import SomeViewClass
class SomeTest(unittest.TestCase):
def test_some_method(self):
foo = 'bar'
Postorius uses Michael Foord’s mock library for mocking. There are some shortcuts you can use to quickly create mock objects that behave a little bit like objects retreived from mailman.client, like:
postorius.tests.utils.create_mock_list creates a mock domain object:
>>> properties = dict(contact_address='postmaster@example.org',
... description='Example dot Org',
... mail_host='example.org',
... url_host='www.example.org')
>>> mock_domain = create_mock_domain(properties)
>>> print mock_domain
<MagicMock name='Domain' id='...'>
>>> print mock_domain.contact_address
postmaster@example.org
>>> print mock_domain.description
Example dot Org
>>> print mock_domain.mail_host
example.org
>>> print mock_domain.url_host
www.example.org
postorius.tests.utils.create_mock_list creates a mock list object:
>>> properties = dict(fqdn_listname='testlist@example.org',
... mail_host='example.org',
... list_name='testlist',
... display_name='Test List')
>>> mock_list = create_mock_list(properties)
>>> print mock_list
<MagicMock name='List' id='...'>
>>> print mock_list.fqdn_listname
testlist@example.org
>>> print mock_list.mail_host
example.org
>>> print mock_list.list_name
testlist
>>> print mock_list.display_name
Test List
postorius.tests.utils.create_mock_list creates a mock list object:
>>> properties = dict(fqdn_listname='testlist@example.org',
... address='les@example.org',)
>>> mock_member = create_mock_member(properties)
>>> print mock_member
<MagicMock name='Member' id='...'>
>>> print mock_member.fqdn_listname
testlist@example.org
>>> print mock_member.address
les@example.org