| 1 |
======================= |
| 2 |
Django Test Integration |
| 3 |
======================= |
| 4 |
|
| 5 |
There are now at least 2 projects that integrate `Django`_ and :pypi:`behave`. |
| 6 |
Both use a `LiveServerTestCase`_ to spin up a runserver for the tests automatically, |
| 7 |
and shut it down when done with the test run. The approach used for integrating |
| 8 |
Django, though, varies slightly. |
| 9 |
|
| 10 |
:pypi:`behave-django` |
| 11 |
Provides a dedicated management command. Easy, automatic integration (thanks |
| 12 |
to monkey patching). Behave tests are run with ``python manage.py behave``. |
| 13 |
Allows running tests against an existing database as a special feature. |
| 14 |
See `setup behave-django <https://pythonhosted.org/behave-django/installation.html>`_ |
| 15 |
and `usage <https://pythonhosted.org/behave-django/usage.html>`_ instructions. |
| 16 |
|
| 17 |
:pypi:`django-behave` |
| 18 |
Provides a Django-specific TestRunner for Behave, which is set with the |
| 19 |
`TEST_RUNNER`_ property in your settings. Behave tests are run |
| 20 |
with the usual ``python manage.py test <app_name>`` by default. |
| 21 |
See `setup django-behave <https://github.com/django-behave/django-behave/blob/master/README.md#how-to-use>`_ |
| 22 |
instructions. |
| 23 |
|
| 24 |
.. _Django: https://www.djangoproject.com |
| 25 |
.. _LiveServerTestCase: https://docs.djangoproject.com/en/1.8/topics/testing/tools/#liveservertestcase |
| 26 |
.. _TEST_RUNNER: https://docs.djangoproject.com/en/1.8/topics/testing/advanced/#using-different-testing-frameworks |
| 27 |
|
| 28 |
|
| 29 |
Manual Integration |
| 30 |
================== |
| 31 |
|
| 32 |
Alternatively, you can integrate Django using the following boilerplate code |
| 33 |
in your ``environment.py`` file: |
| 34 |
|
| 35 |
.. code-block:: python |
| 36 |
|
| 37 |
# -- FILE: my_django/behave_fixtures.py |
| 38 |
from behave import fixture |
| 39 |
import django |
| 40 |
from django.test.runner import DiscoverRunner |
| 41 |
from django.test.testcases import LiveServerTestCase |
| 42 |
|
| 43 |
@fixture |
| 44 |
def django_test_runner(context): |
| 45 |
django.setup() |
| 46 |
context.test_runner = DiscoverRunner() |
| 47 |
context.test_runner.setup_test_environment() |
| 48 |
context.old_db_config = context.test_runner.setup_databases() |
| 49 |
yield |
| 50 |
context.test_runner.teardown_databases(context.old_db_config) |
| 51 |
context.test_runner.teardown_test_environment() |
| 52 |
|
| 53 |
@fixture |
| 54 |
def django_test_case(context): |
| 55 |
context.test_case = LiveServerTestCase |
| 56 |
context.test_case.setUpClass() |
| 57 |
yield |
| 58 |
context.test_case.tearDownClass() |
| 59 |
del context.test_case |
| 60 |
|
| 61 |
|
| 62 |
.. code-block:: python |
| 63 |
|
| 64 |
# -- FILE: features/environment.py |
| 65 |
from behave import use_fixture |
| 66 |
from my_django.behave_fixtures import django_test_runner, django_test_case |
| 67 |
import os |
| 68 |
|
| 69 |
os.environ["DJANGO_SETTINGS_MODULE"] = "test_project.settings" |
| 70 |
|
| 71 |
def before_all(context): |
| 72 |
use_fixture(django_test_runner, context) |
| 73 |
|
| 74 |
def before_scenario(context, scenario): |
| 75 |
use_fixture(django_test_case, context) |
| 76 |
|
| 77 |
|
| 78 |
Taken from Andrey Zarubin's blog post "`BDD. PyCharm + Python & Django`_". |
| 79 |
|
| 80 |
.. _`BDD. PyCharm + Python & Django`: |
| 81 |
https://anvileight.com/blog/2016/04/12/behavior-driven-development-pycharm-python-django/ |
| 82 |
|
| 83 |
|
| 84 |
Strategies and Tooling |
| 85 |
====================== |
| 86 |
|
| 87 |
See :ref:`id.practicaltips` for automation libraries and implementation tips |
| 88 |
on your BDD tests. |