Behave-Django

1.9.0 · active · verified Thu Apr 16

Behave-Django integrates the Behave BDD (Behavior-Driven Development) framework with Django projects, allowing developers to write human-readable feature files and step definitions to test web applications. As of its current version 1.9.0, it supports modern Python and Django versions, with a regular release cadence addressing compatibility and feature enhancements.

Common errors

Warnings

Install

Imports

Quickstart

To get started, define your `features/` directory with `.feature` files and corresponding step definitions in `features/steps/`. Behave-Django automatically detects these. An `environment.py` file is typically used for setup/teardown hooks (e.g., `before_all`, `after_scenario`). Ensure your Django settings are configured correctly for testing. You can run `behave` from your Django project root.

from django.test import TestCase

class MyFeatureTests(TestCase):
    def test_example_scenario(self):
        # Your Django-specific test logic here
        self.assertEqual(1 + 1, 2)

# features/example.feature
# Feature: Example Feature
#   Scenario: Basic addition
#     Given I have the number 1
#     And I add the number 1
#     Then the result should be 2

# features/steps/example_steps.py
# from behave import given, when, then
# from django.test import TestCase
# from behave_django.decorators import fixtures

# @fixtures(['my_fixture.json'])
# @given('I have the number {number:d}')
# def step_impl(context, number):
#     context.a = number

# @when('I add the number {number:d}')
# def step_impl(context, number):
#     context.result = context.a + number

# @then('the result should be {expected_result:d}')
# def step_impl(context, expected_result):
#     assert context.result == expected_result

view raw JSON →