Asserts that `page` can be rendered without raising a fatal error.
For page types with multiple routes, you can use `route_path` to specify a partial path to be added to the page's regular `url`.
When `post_data` is provided, the test makes a `POST` request with `post_data` in the request body. Otherwise, a `GET` request is made.
When supplied, `query_data` is always converted to a querystring and added to the request URL.
When `user` is provided, the test is conducted with them as the active user.
By default, the assertion will fail if the request to the page URL results in a 301, 302 or 404 HTTP response. If you are testing a page/route where a 404 response is expected, you can use `accept_404=True` to indicate this, and the assertion will pass when encountering a 404 response. Likewise, if you are testing a page/route where a redirect response is expected, you can use `accept_redirect=True` to indicate this, and the assertion will pass when encountering 301 or 302 response.
This assertion is great for getting coverage on custom rendering logic for page types. Here is an example:
Asserts that the page edit view works for `page` without raising a fatal error.
When `user` is provided, the test is conducted with them as the active user. Otherwise, a superuser is created and used for the test.
After a successful `GET` request, a `POST` request is made with field data in the request body. If `post_data` is provided, that will be used for this purpose. If not, this data will be extracted from the `GET` response HTML.
This assertion is great for getting coverage on custom fields, panel configuration and custom validation logic. Here is an example:
Asserts that the page preview view can be loaded for `page` without raising a fatal error.
For page types that support different preview modes, you can use `mode` to specify the preview mode to be tested.
When `user` is provided, the test is conducted with them as the active user. Otherwise, a superuser is created and used for the test.
To load the preview, the test client needs to make a `POST` request including all required field data in the request body. If `post_data` is provided, that will be used for this purpose. If not, the method will attempt to extract this data from the page edit view.
This assertion is great for getting coverage on custom preview modes, or getting reassurance that custom rendering logic is compatible with Wagtail's preview mode. Here is an example:
Assert a particular child Page type can not be created under a parent Page type. `parent_model` and `child_model` should be the Page classes being tested.
```python
def test_cant_create_under_event_page(self):
# You can not create a ContentPage under an EventPage
Assert that a child of the given Page type can be created under the parent, using the supplied POST data.
`parent` should be a Page instance, and `child_model` should be a Page subclass. `data` should be a dict that will be POSTed at the Wagtail admin Page creation method.
`publish` specifies whether the page being created should be published or not, default is `False`. When `True`, it checks if the response url includes the url of the Wagtail Explorer Page, displaying an error if does not include that url. Otherwise it checks that the correct edit page loads.
Creating [fixtures](django:howto/initial-data) for tests is best done by creating content in a development
environment, and using Django's [dumpdata](https://docs.djangoproject.com/en/stable/ref/django-admin/#django-admin-dumpdata) command.
Note that by default `dumpdata` will represent `content_type` by the primary key; this may cause consistency issues when adding / removing models, as content types are populated separately from fixtures. To prevent this, use the `--natural-foreign` switch, which represents content types by `["app", "model"]` instead.
### Manual modification
You could modify the dumped fixtures manually, or even write them all by hand.
Here are a few things to be wary of.
#### Custom Page models
When creating customised Page models in fixtures, you will need to add both a
`wagtailcore.page` entry, and one for your custom Page model.
Let's say you have a `website` module which defines a `Homepage(Page)` class.
You could create such a homepage in a fixture with:
```json
[
{
"model": "wagtailcore.page",
"pk": 3,
"fields": {
"title": "My Customer's Homepage",
"content_type": ["website", "homepage"],
"depth": 2
}
},
{
"model": "website.homepage",
"pk": 3,
"fields": {}
}
]
```
#### Treebeard fields
Filling in the `path` / `numchild` / `depth` fields is necessary in order for tree operations like `get_parent()` to work correctly.
`url_path` is another field that can cause errors in some uncommon cases if it isn't filled in.
The [Treebeard docs](https://django-treebeard.readthedocs.io/en/latest/mp_tree.html) might help in understanding how this works.