2
0

testing.rst 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. .. _reference:
  2. =========================
  3. Testing your Wagtail site
  4. =========================
  5. Wagtail comes with some utilities that simplify writing tests for your site.
  6. .. automodule:: wagtail.tests.utils
  7. WagtailPageTests
  8. ================
  9. .. class:: WagtailPageTests
  10. ``WagtailPageTests`` extends ``django.test.TestCase``, adding a few new ``assert`` methods. You should extend this class to make use of its methods:
  11. .. code-block:: python
  12. from wagtail.tests.utils import WagtailPageTests
  13. from myapp.models import MyPage
  14. class MyPageTests(WagtailPageTests):
  15. def test_can_create_a_page(self):
  16. ...
  17. .. automethod:: assertCanCreateAt
  18. .. code-block:: python
  19. def test_can_create_under_home_page(self):
  20. # You can create a ContentPage under a HomePage
  21. self.assertCanCreateAt(HomePage, ContentPage)
  22. .. automethod:: assertCanNotCreateAt
  23. .. code-block:: python
  24. def test_cant_create_under_event_page(self):
  25. # You can not create a ContentPage under an EventPage
  26. self.assertCanNotCreateAt(EventPage, ContentPage)
  27. .. automethod:: assertCanCreate
  28. .. code-block:: python
  29. def test_can_create_content_page(self):
  30. # Get the HomePage
  31. root_page = HomePage.objects.get(pk=2)
  32. # Assert that a ContentPage can be made here, with this POST data
  33. self.assertCanCreate(root_page, ContentPage, {
  34. 'title': 'About us',
  35. 'body': 'Lorem ipsum dolor sit amet')
  36. .. automethod:: assertAllowedParentPageTypes
  37. .. code-block:: python
  38. def test_content_page_parent_pages(self):
  39. # A ContentPage can only be created under a HomePage
  40. # or another ContentPage
  41. self.assertAllowedParentPageTypes(
  42. ContentPage, {HomePage, ContentPage})
  43. # An EventPage can only be created under an EventIndex
  44. self.assertAllowedParentPageTypes(
  45. EventPage, {EventIndex})
  46. .. automethod:: assertAllowedSubpageTypes
  47. .. code-block:: python
  48. def test_content_page_subpages(self):
  49. # A ContentPage can only have other ContentPage children
  50. self.assertAllowedSubpageTypes(
  51. ContentPage, {ContentPage})
  52. # A HomePage can have ContentPage and EventIndex children
  53. self.assertAllowedParentPageTypes(
  54. HomePage, {ContentPage, EventIndex})