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

# Getting started with Allure Spock

[![Allure Spock latest version](https://img.shields.io/maven-central/v/io.qameta.allure/allure-spock?style=flat "allure-spock")](https://mvnrepository.com/artifact/io.qameta.allure/allure-spock)

Generate beautiful HTML reports using [Allure Report](https://allurereport.org/) and your [Spock](https://spockframework.org/) tests.

Info:
Check out the example projects at [github.com/allure-examples](https://github.com/orgs/allure-examples/repositories?q=visibility%3Apublic+archived%3Afalse+topic%3Aexample+topic%3Aspock) to see Allure Spock in action.

## Setting up

### 1. Prepare your project

1. Open a terminal and make sure that Java version 8 or higher is available in the environment.

   ```bash
   java --version
   ```

1. In the `dependencies` section of your `build.gradle.kts` file, add the Allure Spock dependency specification **before** the Spock framework dependencies.

   Warning:
   Due to an implementation detail, some of the Allure Spock functionality **will not work** unless the dependencies are listed in the correct order. For technical details, see the [discussion on GitHub](https://github.com/allure-framework/allure-java/issues/928#issuecomment-1594596412).

   ```kts
   dependencies {
       // Allure Spock adapter
       testImplementation(platform("io.qameta.allure:allure-bom:2.24.0"))
       testImplementation("io.qameta.allure:allure-spock2")
       testImplementation("io.qameta.allure:allure-junit-platform")

       // Spock framework
       testImplementation(platform("org.spockframework:spock-bom:2.3-groovy-4.0"))
       testImplementation("org.spockframework:spock-core")

       // ...
   }
   ```

1. Update the project and fetch all dependencies:

   **MacOS/Linux:**
   ```bash
   ./gradlew build
   ```

   **Windows:**
   ```bash
   gradlew build
   ```

### 2. Run tests

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

**MacOS/Linux:**
```bash
./gradlew test
```

**Windows:**
```bash
gradlew test
```

This will save necessary data into `build/allure-results` or other directory, according to the settings, see `AllureExtension`. 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 Spock integration not only collects the data provided by Spock's standard features, but also provides additional features for writing even better tests. This section lists the most notable ways to improve your tests, using both Spock's and Allure Spock's features.

With Allure Spock, you can:

- provide [description, links and other metadata](#specify-description-links-and-other-metadata),
- [organize tests](#organize-tests) into hierarchies,
- divide the test into smaller, easier-to-read [test steps](#divide-a-test-into-steps),
- describe parameters used when running [parametrized tests](#describe-parametrized-tests),
- make the test save [screenshots and other files](#attach-screenshots-and-other-files) during execution,
- [select which tests to run](#select-tests-via-a-test-plan-file) via a test plan file,
- provide arbitrary [environment information](#environment-information) for the whole test report.

### 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/spock-reference/#metadata) for more details.

For each of the metadata fields, there are two ways to assign it: via an annotation before a test method or via a method call inside a test method's body. The second way is called “dynamic”, because it allows you to construct strings and other values at runtime before passing to the methods. Note, however, that it is highly recommended to assign all metadata as early as possible. Otherwise, there is a risk of the test failing before having all metadata set, which is bad for the test report's readability.

**Annotations API:**
```groovy
import io.qameta.allure.Description
import io.qameta.allure.Issue
import io.qameta.allure.Link
import io.qameta.allure.Owner
import io.qameta.allure.Severity
import io.qameta.allure.TmsLink
import spock.lang.Specification

import static io.qameta.allure.SeverityLevel.*

class TestMyWebsite extends Specification {

    @Description("This test attempts to log into the website using a login and a password. Fails if any error happens.\n\nNote that this test does not test 2-Factor Authentication.")
    @Severity(CRITICAL)
    @Owner("John Doe")
    @Link(name = "Website", url = "https://dev.example.com/")
    @Issue("AUTH-123")
    @TmsLink("TMS-456")
    def "Test Authentication"() {
        expect: true
    }
}
```

**Runtime API:**
```groovy
import io.qameta.allure.Allure
import spock.lang.Specification

class TestMyWebsite extends Specification {

    def "Test Authentication"() {
        Allure.description("This test attempts to log into the website using a login and a password. Fails if any error happens.\n\nNote that this test does not test 2-Factor Authentication.")
        Allure.label("severity", "critical")
        Allure.label("owner", "John Doe")
        Allure.link("Website", "https://dev.example.com/")
        Allure.issue("AUTH-123", "https://example.com/issues/AUTH-123")
        Allure.tms("TMS-456", "https://example.com/tms/TMS-456")
        expect: true
    }
}
```

### 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 Spock provides functions to assign the relevant fields to tests either by adding annotations 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):

**Annotations API:**
```groovy
import io.qameta.allure.Epic
import io.qameta.allure.Feature
import io.qameta.allure.Story
import spock.lang.Specification

class TestMyWebsite extends Specification {

    @Epic("Web interface")
    @Feature("Essential features")
    @Story("Authentication")
    def "Test Authentication"() {
        expect: true
    }
}
```

**Runtime API:**
```groovy
import io.qameta.allure.Allure
import spock.lang.Specification

class TestMyWebsite extends Specification {

    def "Test Authentication"() {
        Allure.epic("Web interface")
        Allure.feature("Essential features")
        Allure.story("Authentication")
        expect: true
    }
}
```

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

```groovy
import io.qameta.allure.Allure
import spock.lang.Specification

class TestMyWebsite extends Specification {

    def "Test Authentication"() {
        Allure.label("parentSuite" "Tests for web interface")
        Allure.suite("Tests for essential features")
        Allure.label("subSuite", "Tests for authentication")
        expect: true
    }
}
```

A test's location in the [package-based hierarchy](/docs/v2/navigation/#package-based-hierarchy) is defined by the fully qualified names of the classes they are declared in, with common prefixes shown as parent packages.

### Divide a test into steps

Allure Spock provides two ways of [creating steps and sub-steps](/docs/steps/): “annotated steps” and “no-op steps”, see the [reference](/docs/spock-reference/#test-steps).

**Annotated steps:**
```groovy
import io.qameta.allure.Step
import spock.lang.Specification

class TestMyWebsite extends Specification {

    def "Test Authentication"() {
        expect: true
        steps.step1()
        steps.step2()
    }

    private class steps {

        @Step("Step 1")
        static def step1() {
            expect: true
        }

        @Step("Step 2")
        static def step2() {
            expect: true
        }
    }
}
```

**No-op steps:**
```groovy
import io.qameta.allure.Allure
import spock.lang.Specification

class TestMyWebsite extends Specification {

    def "Test Authentication"() {
        Allure.step("Step 1")
        expect: true
        Allure.step("Step 2")
        expect: true
    }
}
```

### Describe parametrized tests

Allure Spock supports the [parametrized tests](/docs/v2/readability/#parametrized-tests) pattern.

The easiest way to write a parametrized test is to define a data table, see [Data Driven Testing](https://spockframework.org/spock/docs/2.3/data_driven_testing.html) in the Spock documentation. This will cause Spock to run the same test multiple times, and Allure will automatically detect each set of parameters and display them in the generated report.

Additionally, you can call the Allure Spock's [`parameter()`](/docs/spock/#parametrized-tests) method manually to define pseudo-parameters in any function.

```groovy
import io.qameta.allure.Allure
import spock.lang.Specification

class TestMyWebsite extends Specification {

    def "Test Authentication"() {
        expect: true
        where:
        login                 | _
        "johndoe"             | _
        "johndoe@example.com" | _
    }

    def "Test Authentication With Empty Login"() {
        Allure.parameter("login", "")
        expect: true
    }
}
```

### 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 Spock provides various ways to create an attachment, both from existing files or generated dynamically, see the [reference](/docs/spock-reference/#attachments).

```groovy
import io.qameta.allure.Allure
import spock.lang.Specification

import java.nio.file.Files
import java.nio.file.Paths

class TestMyWebsite extends Specification {

    def "Test Authentication"() {
        Allure.attachment("data.txt", "This is the file content.")
        Allure.attachment(
            "img.png",
            Files.newInputStream(Paths.get("/path/img.png"))
        )
        expect: true
    }
}
```

### Select tests via a test plan file

If the `ALLURE_TESTPLAN_PATH` environment variable is defined and points to an existing file, Spock 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
./gradlew test
```

**Windows:**
```powershell
$Env:ALLURE_TESTPLAN_PATH = "testplan.json"
gradlew test
```

### 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 Java version. This may help the future reader investigate bugs that are reproducible only in some environments.

Images: /images/java/environment-allure3.png, /images/java/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).
