Allure Spock

Allure Spock latest version

Generate beautiful HTML reports using Allure Report and your Spock tests.

Allure Report Spock Example

Check out the example project at github.com/allure-examples/allure-spock2-gradle-kts to see Allure Spock in action.

How to start

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
  2. In the dependencies section of your build.gradle.kts file, add the Allure Spock dependency specification before the Spock framework dependencies.

    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.

    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") // ... }
  3. Update the project and fetch all dependencies:

    Bash
    ./gradlew build
    Bash
    gradlew build

2. Run tests

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

Bash
./gradlew test
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:

  • gradle allureReport processes the test results and saves an HTML report into build/reports/allure-report or another directory, according to the settings, see AllureExtension. To view the report, use the allure open command.

    Use this command if you need to save the report for future reference or for sharing it with colleagues.

  • gradle allureServe creates the same report as gradle allureReport but puts it into a temporary directory and starts a local web server configured to show this directory's contents. The command then automatically opens the main page of the report in a web browser.

    Use this command if you need to view the report for yourself and do not need to save it.

Writing tests

The Allure Spock adapter 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:

There is a lot of metadata you can add to each test so that it would appear in the report. See the reference 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.

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 } }
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, 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).

To specify a test's location in the behavior-based hierarchy:

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 } }
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:

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 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: “annotated steps” and “no-op steps”, see the reference.

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 } } }
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 pattern.

The easiest way to write a parametrized test is to define a data table, see Data Driven Testing 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() 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" | _ "[email protected]" | _ } def "Test Authentication With Empty Login"() { Allure.parameter("login", "") expect: true } }

Attach screenshots and other files

You can attach any sorts of files 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.

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:

Bash
export ALLURE_TESTPLAN_PATH=testplan.json ./gradlew test
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.

Allure Report Environments Widget

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.

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.

Powered by
logo

Join our newsletter

Join our community

We aim to make Allure Report as reliable and user-friendly as possible, and together with the community, we're here to help when problems arise.

© 2024 Qameta Software Inc. All rights reserved.