---
title: Behave
description: Learn how to integrate Allure with Behave to generate rich, interactive test reports. Follow step-by-step setup, test execution, and report generation guidance.
---

# Getting started with Allure Behave

[![Allure Behave pypi latest version](https://img.shields.io/pypi/v/allure-behave?style=flat "Allure Behave pypi latest version")](https://pypi.org/project/allure-behave/)

Generate beautiful HTML reports using [Allure Report](https://allurereport.org/docs/) and your [behave](https://behave.readthedocs.io/en/stable/) tests.

Info:
Check out the example project at [github.com/allure-examples/behave-pip](https://github.com/allure-examples/behave-pip) to see Allure Behave in action.

## Setting up

### 1. Prepare your project

1. Install the Allure Report command-line tool, if it is not yet installed in your operating system. Note that Allure Report requires Java, see the [installation instructions](/docs/v2/install/).

1. Open a terminal and go to the project directory. For example:

   ```bash
   cd /home/user/myproject
   ```

1. If necessary for your system configuration, activate the virtual Python environment for your project.

   For example, if the project uses a `venv` environment, the command to activate it may look like this:

   ```bash
   source .venv/bin/activate
   ```

   This step is not necessary if you are using the system Python environment.

1. Install the Allure Behave integration.

   ```bash
   pip install allure-behave
   ```

1. In the [Behave configuration file](https://behave.readthedocs.io/en/stable/behave/#configuration-files) (such as `.behaverc` or `behave.ini`), specify the Allure Behave as the output formatter and specify the path to the test results directory.

   For example:

   ```ini
   [behave]
   format=allure_behave.formatter:AllureFormatter
   outfiles=allure-results
   ```

### 2. Run tests

Run your behave tests the same way as you would run them usually. For example:

```bash
python -m behave
```

This will save necessary data into `allure-results` or other directory, according to the `outfile` option. If the directory already exists, the new files will be added to the existing ones, so that a future report will be based on them all.

### 3. Generate a report

Finally, convert the test results into an HTML report. This can be done by one of two commands:

- `allure generate` processes the test results and saves an HTML report into the `allure-report` directory. To view the report, use the `allure open` command.

- `allure serve` creates the same report as `allure generate`, then automatically opens the main page of the report in a web browser.

## Writing tests

The Allure Behave integration extends the standard reporting features of behave by providing additional capabilities for crafting more informative and structured tests. This section highlights key enhancements that can be utilized:

- **Metadata Annotation**: Enhance test reports with [descriptions, links, and other metadata](#specify-description-links-and-other-metadata).
- **Test Organization**: Structure your tests into [clear hierarchies](#organize-tests) for better readability and organization.
- **Step Division**: Break down tests into smaller [test steps](#divide-a-test-into-steps) for easier understanding and maintenance.
- **Parametrized Tests**: Clearly describe the parameters for [parametrized tests](#describe-parametrized-tests) to specify different scenarios.
- **Attachments**: Automatically capture [screenshots and other files](#attach-screenshots-and-other-files) during test execution.
- **Test Selection**: Use a test plan file to [select which tests to run](#select-tests-via-a-test-plan-file), allowing for flexible test execution.
- **Environment Details**: Include comprehensive [environment information](#environment-information) to accompany the test report.

In most cases, you need to indicate to Allure Behave that a certain property needs to be assigned to the test result. Most properties can be assigned via Gherkin tags or via the Runtime API.

- **Gherkin tags**: use [Gherkin tags](https://behave.readthedocs.io/en/latest/gherkin/#tags) to assign various data to a particular `Scenario` or a whole `Feature`. Note that due to a limitation in the Gherkin syntax, a tag cannot contain spaces.

  When using this approach, the data is guaranteed to be added to the test result regardless of how the test itself runs.

- **Runtime API**: use Allure's functions to add data to the test result during the execution of its steps. This approach allows for constructing the data dynamically.

  Note that it is recommended to call the Allure's functions as close to the beginning of the test as possible. This way, the data will be added even if the test fails early.

### Specify description, links and other metadata

There is a lot of [metadata](/docs/v2/readability/#description-links-and-other-metadata) you can add to each test so that it would appear in the report. See the [reference](/docs/behave-reference/#metadata) for more details.

**Gherkin tags:**
```gherkin
Feature: Labels

  @critical
  @allure.label.owner:JohnDoe
  @allure.link:https://dev.example.com/
  @allure.issue:UI-123
  @allure.tms:TMS-456
  Scenario: Create new label for authorized user
    When I open labels page
    And I create label with title "hello"
    Then I should see label with title "hello"
```

**Runtime API:**
```python
import allure
from behave import then

@then('I open labels page')
def step_impl(context):
    allure.dynamic.link('https://dev.example.com/', name='Website')
    allure.dynamic.issue('UI-123')
    allure.dynamic.testcase('TMS-456')
    ...
```

### Organize tests

As described in [Improving navigation in your test report](/docs/v2/navigation/), Allure supports multiple ways to organize tests into hierarchical structures. Allure Behave provides functions to assign the relevant fields to tests either by adding decorators or “dynamically” (same as for the [metadata fields](#specify-description-links-and-other-metadata)).

To specify a test's location in the [behavior-based hierarchy](/docs/v2/navigation/#behavior-based-hierarchy):

**Gherkin tags:**
```gherkin
@allure.label.epic:WebInterface
Feature: Labels

  @allure.label.story:Labels
  Scenario: Create new label for authorized user
    When I open labels page
    And I create label with title "hello"
    Then I should see label with title "hello"
```

To specify a test's location in the [suite-based hierarchy](/docs/v2/navigation/#suite-based-hierarchy):

**Gherkin tags:**
```gherkin
Feature: Labels

  @allure.label.parentSuite:WebInterface
  @allure.label.suite:EssentialFeatures
  @allure.label.subSuite:Labels
  Scenario: Create new label for authorized user
    When I open labels page
    And I create label with title "hello"
    Then I should see label with title "hello"
```

To specify a test's location in the [package-based hierarchy](/docs/v2/navigation/#package-based-hierarchy):

**Gherkin tags:**
```gherkin
Feature: Labels

  @allure.label.package:org.example
  @allure.label.testClass:TestMyWebsite
  @allure.label.testMethod:TestLabels()
  Scenario: Create new label for authorized user
    When I open labels page
    And I create label with title "hello"
    Then I should see label with title "hello"
```

### Divide a test into steps

Allure Behave provides two ways of [creating steps and sub-steps](/docs/steps/): “decorated steps” and “context steps”, see the [reference](/docs/behave-reference/#test-steps).

**Decorated steps:**
```python
import allure
from behave import then

@then("I open labels page")
def step_impl(context):
    step1()
    for val in ["val1", "val2", "val3"]:
        step2(val)

@allure.step("Step 1")
def step1():
    ...

@allure.step("Step 2 (with value {val})")
def step2(val):
    ...
```

**Context steps:**
```python
import allure
from behave import then

@then("I open labels page")
def step_impl(context):
    with allure.step("Step 1"):
        ...

    for val in ["val1", "val2", "val3"]:
        with allure.step(f"Step 2 (with value {val})"):
            ...
```

### Describe parametrized tests

Allure Behave can display parameters passed to the tests via `Examples` tables, see the [reference](/docs/behave-reference/#parametrized-tests).

```gherkin
Feature: User management

  Scenario Outline: Registration
    When I go to the registration form
    And I enter my details: <login>, <password>, <name>, <birthday>
    Then the profile should be created

    Examples:
      | login   | password | name     | birthday   |
      | johndoe | qwerty   | John Doe | 1970-01-01 |
      | janedoe | 123456   | Jane Doe | 1111-11-11 |
```

### Attach screenshots and other files

You can [attach any sorts of files](/docs/attachments/) to your Allure report. For example, a popular way to make a report easier to understand is to attach a screenshot of the user interface at a certain point.

Allure Behave provides various ways to create an attachment, both from existing files or generated dynamically, see the [reference](/docs/behave-reference/#attachments).

```python
import allure
from behave import then

@then("I open labels page")
def step_impl(context):
    ...
    with open('/path/to/image.png', 'rb') as png_file:
        png_bytes = png_file.read()
    allure.attach(png_bytes, name="img", attachment_type=allure.attachment_type.PNG)
```

### Select tests via a test plan file

If the `ALLURE_TESTPLAN_PATH` environment variable is defined and points to an existing file, behave will only run tests listed in this file.

Here's an example of running tests according to a file named `testplan.json`:

**MacOS/Linux:**
```bash
export ALLURE_TESTPLAN_PATH=testplan.json
python -m behave
```

**Windows:**
```powershell
$Env:ALLURE_TESTPLAN_PATH = "testplan.json"
python -m behave
```

### Environment information

For the main page of the report, you can collect various information about the environment in which the tests were executed.

For example, it is a good idea to use this to remember the OS version and Python version. This may help the future reader investigate bugs that are reproducible only in some environments.

Images: /images/python/environment-allure3.png, /images/python/environment-allure2.png

To provide environment information, put a file named `environment.properties` into the `allure-results` directory after running the tests. See the example in [Environment file](/docs/how-it-works-environment-file/).

Note that this feature should be used for properties that do not change for all tests in the report. If you have properties that can be different for different tests, consider using [Parametrized tests](#describe-parametrized-tests).
