Tutorial 12: Use another Spoken Language

Goal

Use another spoken language for testing (other than English)

Most BDD frameworks provide internationalisation support. The key part (but only the first step) is that the Given ... When ... Then keywords are provided in the native language, for example French or German. If this is the case, a developer can provide step definitions in another spoken language.

Hint

The list of supported languages can be displayed via:

behave --lang-list

Feature files can be tagged for a specific language, like:

# language: de
# -- file:*.feature
...

When this happens, the BDD framework selects the keywords for this language. The default language can be defined in the configuration file. behave uses either behave.ini or .behaverc as configuration file:

# -- file:behave.ini
[behave]
lang = de

Write the Feature Test

# file:features/tutorial12_spoken_language.feature
# language: de
Funktionalität: Using Languages other than English (tutorial12)

  Als Deutscher
  will ich BDD-Tests auch in Deutsch schreiben und ausführen.

    Szenario: Run a simple test with German Keywords
        Angenommen we have behave installed
        Wenn we implement a test
        Dann behave will test it for us!

    Szenario: Run a simple test completely in German
        Angenommen wir haben "behave" installiert
        Wenn wir einen Test implementieren
        Dann wird "behave" ihn für uns testen!

Provide the Test Automation

# file:features/steps/step_tutorial01.py
# ----------------------------------------------------------------------------
# STEPS:
# ----------------------------------------------------------------------------
from behave import given, when, then

@given('we have behave installed')
def step_impl(context):
    pass

@when('we implement a test')
def step_impl(context):
    assert True is not False

@then('behave will test it for us!')
def step_impl(context):
    assert context.failed is False
# file:features/steps/step_tutorial12.py
# -*- coding: UTF-8 -*-
# language: de
# ----------------------------------------------------------------------------
# STEPS:
# ----------------------------------------------------------------------------
from behave import given, when, then

@given('wir haben "behave" installiert')
def step_impl(context):
    context.execute_steps(u"Angenommen we have behave installed")

@when('wir einen Test implementieren')
def step_impl(context):
    context.execute_steps(u"Wenn we implement a test")

@then(u'wird "behave" ihn für uns testen!')
def step_impl(context):
    context.execute_steps(u'Dann behave will test it for us!')

Run the Feature Test

Automatic language selection (via feature-file language marker):

$ behave ../features/tutorial12_spoken_language.feature
Funktionalität: Using Languages other than English (tutorial12)   # ../features/tutorial12_spoken_language.feature:2
  Als Deutscher
  will ich BDD-Tests auch in Deutsch schreiben und ausführen.

  Szenario: Run a simple test with German Keywords  # ../features/tutorial12_spoken_language.feature:7
    Angenommen we have behave installed             # ../features/steps/step_tutorial01.py:12
    Wenn we implement a test                        # ../features/steps/step_tutorial01.py:16
    Dann behave will test it for us!                # ../features/steps/step_tutorial01.py:20

  Szenario: Run a simple test completely in German  # ../features/tutorial12_spoken_language.feature:12
    Angenommen wir haben "behave" installiert       # ../features/steps/step_tutorial12.py:25
    Wenn wir einen Test implementieren              # ../features/steps/step_tutorial12.py:29
    Dann wird "behave" ihn für uns testen!          # ../features/steps/step_tutorial12.py:33

1 feature passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 0 skipped
6 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.001s

Explicit language selection via command-line usage of --lang=${lang}:

$ behave --lang=de ../features/tutorial12_spoken_language.feature
Funktionalität: Using Languages other than English (tutorial12)   # ../features/tutorial12_spoken_language.feature:2
  Als Deutscher
  will ich BDD-Tests auch in Deutsch schreiben und ausführen.

  Szenario: Run a simple test with German Keywords  # ../features/tutorial12_spoken_language.feature:7
    Angenommen we have behave installed             # ../features/steps/step_tutorial01.py:12
    Wenn we implement a test                        # ../features/steps/step_tutorial01.py:16
    Dann behave will test it for us!                # ../features/steps/step_tutorial01.py:20

  Szenario: Run a simple test completely in German  # ../features/tutorial12_spoken_language.feature:12
    Angenommen wir haben "behave" installiert       # ../features/steps/step_tutorial12.py:25
    Wenn wir einen Test implementieren              # ../features/steps/step_tutorial12.py:29
    Dann wird "behave" ihn für uns testen!          # ../features/steps/step_tutorial12.py:33

1 feature passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 0 skipped
6 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.001s