PyCharm 2023.3 Help

Enable support for behave-django

With PyCharm you can benefit from the behavior-driven development (BDD) with Django by enabling behave-django integration in your Django applications.

To enable behave-django integration:

  1. In your Django project, install the following Python packages:

    • behave

    • behave-django

  2. Modify the structure of your project to meet the behave structural requirements. In your Django application directory, create the features directory for the test scenario files and the steps subdirectory for the Python implementations of the scenarios. Create a new Gherkin feature file, features/auth.feature, and a new Python step file, features/steps/auth.py.

    a Django project structure required for behave-django integration
  3. Now, add your test scenarios to the auth.feature by using Gherkin feature testing language:

    Feature: auth Scenario: Unauthenticated user can't access the page Given I am not authenticated When I access the page Then Status code is 302 Scenario: Authenticated user can access the page Given I am authenticated When I access the page Then Status code is 200
  4. Implement the steps used in the auth.feature scenarios in the features/steps/auth.py Python file:

    from behave import * from django.contrib.auth.models import User use_step_matcher("re") @given("I am not authenticated") def step_impl(context): pass @when("I access the page") def step_impl(context): context.response = context.test.client.get("/") @then("Status code is (?P<status>\d+)") def step_impl(context, status): code = context.response.status_code assert code == int(status), "{0} != {1}".format(code, status) @given("I am authenticated") def step_impl(context): user = User.objects.create_superuser("jane", "jane@example.org", "123") context.test.client.force_login(user)
  5. Modify the views.py file and the urls.py files to add code required for our testing sample:

    views.py

    from django.contrib.auth.decorators import login_required from django.http import HttpResponse # Create your views here. @login_required def show_user(request): return HttpResponse("OK")

    urls.py

    from django.urls import path from Django_Behave import views urlpatterns = [ path('', views.show_user), ]
  6. Now, implement the key tweak: add 'behave_django' to the INSTALLED_APPS section of the settings.py.

    enabling behave-django integration in the settings.py file

    With this done, your application is ready for a test session. All you need is to run test configurations for your scenarios.

  7. Open the auth.feature file in the Editor window. Click Gutter icon to run a behave scenario in the gutter next to any of the Scenario statements and choose Run <scenario name>. The Run tool window opens with test run results.

    Test running for a behave configuration

    From the test report, you can learn that the test was run by manage.py. You can also see how many steps of the scenario have been passed, failed, or skipped.

    By default, successful steps are not shown. Click Show passed tests on the toolbar to display them.

Last modified: 11 December 2023