Skip to content

Commit

Permalink
Merge pull request #3106 from pickfire/django-test
Browse files Browse the repository at this point in the history
Add Static/LiveServerTestCase for django test
  • Loading branch information
Zac-HD authored Sep 24, 2021
2 parents b3f4ecd + 0ecda1e commit 6d7446a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ their individual contributions.
* `Humberto Rocha <https://github.com/humrochagf>`_
* `Ilya Lebedev <https://github.com/melevir>`_ (melevir@gmail.com)
* `Israel Fruchter <https://github.com/fruch>`_
* `Ivan Tham <https://github.com/pickfire>`_
* `Jack Massey <https://github.com/massey101>`_
* `Jakub Nabaglo <https://github.com/nbgl>`_ (j@nab.gl)
* `Jenny Rouleau <https://github.com/jennyrou>`_
Expand Down
5 changes: 5 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RELEASE_TYPE: minor

This release adds :class:`~hypothesis.extra.django.LiveServerTestCase` and
:class:`~hypothesis.extra.django.StaticLiveServerTestCase` for django test.
Thanks to Ivan Tham for this feature!
6 changes: 5 additions & 1 deletion hypothesis-python/docs/django.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ if you're still getting security patches, you can test with Hypothesis.

Using it is quite straightforward: All you need to do is subclass
:class:`hypothesis.extra.django.TestCase` or
:class:`hypothesis.extra.django.TransactionTestCase`
:class:`hypothesis.extra.django.TransactionTestCase` or
:class:`~hypothesis.extra.django.LiveServerTestCase` or
:class:`~hypothesis.extra.django.StaticLiveServerTestCase`
and you can use :func:`@given <hypothesis.given>` as normal,
and the transactions will be per example
rather than per test function as they would be if you used :func:`@given <hypothesis.given>` with a normal
Expand All @@ -23,6 +25,8 @@ on these classes that do not use
:func:`@given <hypothesis.given>` will be run as normal.

.. class:: hypothesis.extra.django.TransactionTestCase
.. class:: hypothesis.extra.django.LiveServerTestCase
.. class:: hypothesis.extra.django.StaticLiveServerTestCase

We recommend avoiding :class:`~hypothesis.extra.django.TransactionTestCase`
unless you really have to run each test case in a database transaction.
Expand Down
4 changes: 4 additions & 0 deletions hypothesis-python/src/hypothesis/extra/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@

from hypothesis.extra.django._fields import from_field, register_field_strategy
from hypothesis.extra.django._impl import (
LiveServerTestCase,
StaticLiveServerTestCase,
TestCase,
TransactionTestCase,
from_form,
from_model,
)

__all__ = [
"LiveServerTestCase",
"StaticLiveServerTestCase",
"TestCase",
"TransactionTestCase",
"from_field",
Expand Down
9 changes: 9 additions & 0 deletions hypothesis-python/src/hypothesis/extra/django/_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from typing import Optional, Type, Union

from django import forms as df, test as dt
from django.contrib.staticfiles import testing as dst
from django.core.exceptions import ValidationError
from django.db import IntegrityError, models as dm

Expand Down Expand Up @@ -53,6 +54,14 @@ class TransactionTestCase(HypothesisTestCase, dt.TransactionTestCase):
pass


class LiveServerTestCase(HypothesisTestCase, dt.LiveServerTestCase):
pass


class StaticLiveServerTestCase(HypothesisTestCase, dst.StaticLiveServerTestCase):
pass


@defines_strategy()
def from_model(
*model: Type[dm.Model], **field_strategies: Union[st.SearchStrategy, InferType]
Expand Down

0 comments on commit 6d7446a

Please sign in to comment.