There are now at least 2 projects that integrate Django and behave. Both use a LiveServerTestCase to spin up a runserver for the tests automatically, and shut it down when done with the test run. The approach used for integrating Django, though, varies slightly.
python manage.py behave.
Allows running tests against an existing database as a special feature.
See setup behave-django
and usage instructions.python manage.py test <app_name> by default.
See setup django-behave
instructions.Alternatively, you can integrate Django using the following boilerplate code
in your environment.py file:
# -- FILE: my_django/behave_fixtures.py
from behave import fixture
import django
from django.test.runner import DiscoverRunner
from django.test.testcases import LiveServerTestCase
@fixture
def django_test_runner(context):
django.setup()
context.test_runner = DiscoverRunner()
context.test_runner.setup_test_environment()
context.old_db_config = context.test_runner.setup_databases()
yield
context.test_runner.teardown_databases(context.old_db_config)
context.test_runner.teardown_test_environment()
@fixture
def django_test_case(context):
context.test_case = LiveServerTestCase
context.test_case.setUpClass()
yield
context.test_case.tearDownClass()
del context.test_case
# -- FILE: features/environment.py
from behave import use_fixture
from my_django.behave_fixtures import django_test_runner, django_test_case
import os
os.environ["DJANGO_SETTINGS_MODULE"] = "test_project.settings"
def before_all(context):
use_fixture(django_test_runner, context)
def before_scenario(context, scenario):
use_fixture(django_test_case, context)
Taken from Andrey Zarubin’s blog post “BDD. PyCharm + Python & Django”.
See Practical Tips on Testing for automation libraries and implementation tips on your BDD tests.