Appearance
Allure SpecFlow reference
These are the attributes and methods that you can use to integrate your SpecFlow tests with Allure Report.
In most cases, you need to indicate to Allure SpecFlow 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 to assign various data to a particular
Scenario
or a wholeFeature
. By default, all Gherkin tags will be converted to test tags, but you can configure conversion to various other metadata via regular expressions in the configuration file.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.
Metadata
Assign a test's description, links and other metadata.
Title
AllureApi.SetTestName(string name)
Set the test's title.
csharp
using Allure.Net.Commons;
using TechTalk.SpecFlow;
[Binding]
public class Steps
{
[When("I open labels page")]
public void TestLabels()
{
AllureApi.SetTestName("Open labels page");
// ...
}
}
Description
AllureApi.SetDescription(string description)
Set the test's description.
Allure SpecFlow uses a scenario's description from the Gherkin file, if present. Alternatively, use Runtime API to set the description dynamically.
gherkin
Feature: Labels
Scenario: Create new label for authorized user
This test attempts to create a label with specified title
When I open labels page
And I create label with title "hello"
Then I should see label with title "hello"
csharp
using Allure.Net.Commons;
using TechTalk.SpecFlow;
[Binding]
public class Steps
{
[When("I open labels page")]
public void TestLabels()
{
AllureApi.SetDescription("This test attempts to create a label with specified title");
// ...
}
}
Owner
AllureApi.SetOwner(string owner)
Set the test's owner.
The format for the Gherkin tags is configured via the specflow.labels.owner
option.
gherkin
@allure.label.owner:JohnDoe
Feature: 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"
csharp
using Allure.Net.Commons;
using TechTalk.SpecFlow;
[Binding]
public class Steps
{
[When("I open labels page")]
public void ThenLabels()
{
AllureApi.SetOwner("John Doe");
// ...
}
}
Tag
AllureApi.AddTags(params string[] tags)
Set the test's tags.
Any Gherkin tag is automatically added to the list of the test's tags, unless it matches one of the regular expressions in the configuration.
gherkin
@UI @Labels
Feature: 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"
csharp
using Allure.Net.Commons;
using TechTalk.SpecFlow;
[Binding]
public class Steps
{
[When("I open labels page")]
public void TestLabels()
{
AllureApi.AddTags("UI", "Labels");
// ...
}
}
Severity
AllureApi.SetSeverity(SeverityLevel severity)
Set the test's severity.
The format for the Gherkin tags is configured via the specflow.labels.severity
option.
gherkin
Feature: Labels
@critical
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"
csharp
using Allure.Net.Commons;
using TechTalk.SpecFlow;
[Binding]
public class Steps
{
[When("I open labels page")]
public void TestLabels()
{
AllureApi.SetSeverity(SeverityLevel.critical);
// ...
}
}
Label
AllureApi.AddLabel(string name, string value)
AllureApi.AddLabel(Label label)
AllureApi.AddLabels(params Label[] labels)
Set an arbitrary label for the test. This is the underlying implementation for a lot of Allure's other APIs.
The format for the Gherkin tags is configured via the specflow.labels.label
option.
gherkin
@allure.label.layer:web
@allure.label.owner:eroshenkoam
@allure.label.page:/{org}/{repo}/labels
@allure.label.jira:AE-2
Feature: 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"
csharp
using Allure.Net.Commons;
using TechTalk.SpecFlow;
[Binding]
public class Steps
{
[When("I open labels page")]
public void TestLabels()
{
AllureApi.AddLabel("layer", "web");
AllureApi.AddLabel("owner", "eroshenkoam");
AllureApi.AddLabel("page", "/{org}/{repo}/labels");
AllureApi.AddLabel("jira", "AE-2");
// ...
}
}
Allure ID (Allure TestOps)
Set the test's ID.
The format for the Gherkin tags is configured via the specflow.labels.label
option. In the tag, use allure_id
as the label name.
gherkin
Feature: Labels
@allure.label.allure_id:123
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"
Link
AllureApi.AddLink(string url)
AllureApi.AddLink(string name, string url)
AllureApi.AddLink(string name, string type, string url)
AllureApi.AddLinks(params Link[] links)
AllureApi.AddIssue(string url)
AllureApi.AddIssue(string name, string url)
AllureApi.AddTmsItem(string url)
AllureApi.AddTmsItem(string name, string url)
Add a link related to the test.
Based on the type
(which can be any string; defaults to “link”), Allure will try to load a corresponding link pattern to process the URL, as defined by the allure.links
section of the configuration file. If no pattern found for the given type, the URL is left unmodified.
The name
will be used as the link's text. If it is omitted, the unprocessed URL will be used instead.
Additionally, you can use the specflow.links
section of the configuration file to specify formats for adding “link”, “issue” and “tms” links via Gherkin tags.
gherkin
@link:https://dev.example.com/
@issue:UI-123
@tms:TMS-456
Feature: 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"
csharp
using Allure.Net.Commons;
using TechTalk.SpecFlow;
[Binding]
public class Steps
{
[When("I open labels page")]
public void TestLabels()
{
AllureApi.AddLink("Website", "https://dev.example.com/");
AllureApi.AddIssue("UI-123");
AllureApi.AddTmsItem("TMS-456");
// ...
}
}
Behavior-based hierarchy
AllureApi.AddEpic(string epic)
AllureApi.AddFeature(string feature)
AllureApi.AddStory(string story)
Assign names of epics, features or user stories for a test, as part of Allure's behavior-based hierarchy.
The format for the Gherkin tags is configured via the specflow.grouping.behaviors
options.
gherkin
@allure.label.epic:WebInterface
Feature: Labels
@allure.label.story:CreateLabels
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"
csharp
using Allure.Net.Commons;
using TechTalk.SpecFlow;
[Binding]
public class Steps
{
[When("I open labels page")]
public void TestLabels()
{
AllureApi.AddEpic("Web interface");
AllureApi.AddFeature("Essential features");
AllureApi.AddStory("Labels");
// ...
}
}
Suite-based hierarchy
AllureApi.AddParentSuite(string parentSuite)
AllureApi.AddSuite(string suite)
AllureApi.AddSubSuite(string subSuite)
Assign the names of parent suite, suite or sub-suite for a test, as part of Allure's suite-based hierarchy.
The format for the Gherkin tags is configured via the specflow.grouping.suites
options.
gherkin
@allure.label.parentSuite:WebInterface
@allure.label.suite:EssentialFeatures
@allure.label.subSuite:Labels
Feature: 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"
csharp
using Allure.Net.Commons;
using TechTalk.SpecFlow;
[Binding]
public class Steps
{
[When("I open labels page")]
public void TestLabels()
{
AllureApi.AddParentSuite("Web interface");
AllureApi.AddSuite("Essential features");
AllureApi.AddSubSuite("Labels");
// ...
}
}
Package-based hierarchy
Assign the names of package, test class and test method, as part of Allure's package-based hierarchy.
The format for the Gherkin tags is configured via the specflow.grouping.packages
options.
gherkin
@allure.label.package:org.example
@allure.label.testClass:TestMyWebsite
@allure.label.testMethod:TestLabels()
Feature: 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"
csharp
using Allure.Net.Commons;
using TechTalk.SpecFlow;
[Binding]
public class Steps
{
[When("I open labels page")]
public void TestLabels()
{
AllureApi.AddLabel(Label.Package("org.example"));
AllureApi.AddLabel(Label.TestClass("TestMyWebsite"));
AllureApi.AddLabel(Label.TestMethod("TestLabels()"));
// ...
}
}
Test steps
AllureApi.Step(string name)
AllureApi.Step(string name, Action acti
T AllureApi.Step<T>(string name, Func<T> function)
async Task AllureApi.Step(string name, Func<Task> action)
async Task<T> AllureApi.Step<T>(string name, Func<Task<T>> function)
Define a test sub-steps with the given name
.
There are two ways of defining a sub-step.
Lambda sub-steps
Write a test sub-step in a lambda function and pass it to
Step()
. If the lambda function returns a value,Step()
will return it without modification, and it will not affect the report.Use
AllureLifecycle.Instance.UpdateStep()
to add custom parameters to the current sub-step, as shown in the example below.No-op sub-steps
If you call
Step()
with a name but without a lambda function, Allure will add to the report a no-op sub-step. This allows for a log-style reporting within a larger step. A no-op sub-step finishes immediately after it started and cannot have any parameters, attachments or sub-steps of its own.
In the example below, a new lambda sub-step is created at runtime for each URL from a list. The URL is added to the list of the sub-step's parameters. Within each of the sub-steps, a sequence of three no-op sub-steps occurs.
csharp
using Allure.Net.Commons;
using TechTalk.SpecFlow;
[Binding]
public class Steps
{
[Then("visit pages")]
public void ThenVisitPages()
{
string[] urls =
{
"https://domain1.example.com/",
"https://domain2.example.com/"
};
foreach (string url in urls)
{
AllureApi.Step($"Test {url}", () =>
{
AllureLifecycle.Instance.UpdateStep(stepResult =>
{
stepResult.parameters.Add(
new Parameter { name = "Webpage URL", value = url }
);
});
AllureApi.Step("Opening web browser...");
// ...
AllureApi.Step($"Visiting {url}...");
// ...
AllureApi.Step("Closing web browser...");
// ...
});
}
}
}
Parametrized tests
An Allure test report can reflect multiple ways in which you can pass data from your Gherkin file to your C# implementation code, namely:
Scenario outlines
In Gherkin, a Scenario Outline
(or a Scenario Template
) implements the parametrized tests pattern. A scenario outline must contain an Examples
table, from which SpecFlow loads sets of parameters, one row after another. Each set of parameters is being placed into the step declarations according to the placeholders, thus generating a new scenario based on the row. SpecFlow then runs each of them independently, as if it was a separate Scenario
. The data can then be captured by a regular expression and passed as separate arguments to the C# code.
Allure SpecFlow automatically recognizes this pattern. No additional configuration is required.
The example below shows a Gherkin file and a C# implementation file of a test. In this example, the four parameters for the “I enter my details...” step will be displayed in both instances of the scenario in the test report.
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 |
csharp
using System;
using TechTalk.SpecFlow;
[Binding]
public class Steps
{
[When("I go to the registration form")]
public void WhenIGoToTheRegistrationForm()
{
// ...
}
[When(@"I enter my details: (.*), (.*), (.*), (.*)")]
public void WhenIEnterMyDetails(
string login, string password, string name, DateTime birthday)
{
// ...
}
[Then("the profile should be created")]
public void ThenTheProfileShouldBeCreated()
{
// ...
}
}
Vertical data tables
If your Gherkin step declaration contains a data table with two columns, Allure SpecFlow can display its content as the step-level parameters. Depending on the configuration, this behavior can be applied either to all tables with two columns or only to those whose headers match a specific regular expression.
The functionality is optional. To enable or disable it or modify the regular expressions, edit the specflow.stepArguments
section of the configuration file.
The table data itself is passed to the C# code without modifications.
The example below shows a configuration file, a Gherkin file and a C# implementation file of a test. In this example, the four parameters for the “I enter my details” step will be displayed in the test report.
json
{
"specflow": {
"stepArguments": {
"convertToParameters": true,
"paramNameRegex": "^Parameter$",
"paramValueRegex": "^Value$"
}
}
}
gherkin
Feature: User management
Scenario: Registration
When I go to the registration form
And I enter my details
| Parameter | Value |
| login | johndoe |
| password | qwerty |
| name | John Doe |
| birthday | 1970-01-01 |
Then the profile should be created
csharp
using TechTalk.SpecFlow;
[Binding]
public class Steps
{
[When("I go to the registration form")]
public void WhenIGoToTheRegistrationForm()
{
// ...
}
[When("I enter my details")]
public void WhenIEnterMyDetails(Table table)
{
// ...
}
[Then("the profile should be created")]
public void ThenTheProfileShouldBeCreated()
{
// ...
}
}
Horizontal data tables
If your Gherkin step declaration contains a data table with one row, Allure SpecFlow can display its content as the step-level parameters.
The functionality is optional. To enable or disable it, edit the specflow.stepArguments
section of the configuration file.
The table data itself is passed to the C# code without modifications.
WARNING
Due to an implementation detail, the Allure SpecFlow's horizontal data table processor only works correctly if you set all the configuration options for the vertical data table processor. Make sure to provide some regular expressions for both paramNameRegex
and paramValueRegex
, even if you do not plan to use vertical tables in your project.
Also note that currently, Allure SpecFlow does not support horizontal tables with two columns only. Instead, such a table will be interpreted as a vertical data table.
The example below shows a configuration file, a Gherkin file and a C# implementation file of a test. In this example, the four parameters for the “I enter my details” step will be displayed in the test report.
json
{
"specflow": {
"stepArguments": {
"convertToParameters": true,
"paramNameRegex": "^Parameter$",
"paramValueRegex": "^Value$"
}
}
}
gherkin
Feature: User management
Scenario: Registration
When I go to the registration form
And I enter my details
| login | password | name | birthday |
| johndoe | qwerty | John Doe | 1970-01-01 |
Then the profile should be created
csharp
using TechTalk.SpecFlow;
[Binding]
public class Steps
{
[When("I go to the registration form")]
public void WhenIGoToTheRegistrationForm()
{
// ...
}
[When("I enter my details")]
public void WhenIEnterMyDetails(Table table)
{
// ...
}
[Then("the profile should be created")]
public void ThenTheProfileShouldBeCreated()
{
// ...
}
}
Runtime parameters
AllureApi.AddTestParameter(string name, object? value)
AllureApi.AddTestParameter(string name, object? value, ParameterMode mode)
AllureApi.AddTestParameter(string name, object? value, bool excluded)
AllureApi.AddTestParameter(string name, object? value, ParameterMode mode, bool excluded)
AllureApi.AddTestParameter(Parameter parameter)
The AddTestParameter()
function can be used at any point, even if the Gherkin file does not specify actual parameters or data tables to any step.
If the excluded
argument is set to true, Allure will not use the parameter when comparing the current test result with previous one in the history. This argument is only used by Allure TestOps.
The mode
argument affects how the parameter will be displayed in the report. Available options are defined in the ParameterMode
enumeration:
ParameterMode.Default
(same as not specifying any mode) — the parameter and its value will be shown in a table along with other parameters.ParameterMode.Masked
— the parameter will be shown in the table, but its value will be hidden. Use this mode for passwords, tokens and other sensitive parameters.ParameterMode.Hidden
— the parameter and its value will not be shown in the test report. Note, however, that it is still possible to extract the value from theallure_results
directory if you publish it.
The example below shows a Gherkin file and a C# implementation file of a test. As far as SpecFlow is concerned, the “I enter my details” in the example does not have any data passed to it, however, Allure will have the four parameters displayed on the test level.
gherkin
Feature: User management
Scenario: Registration
When I go to the registration form
And I enter my details
Then the profile should be created
csharp
using Allure.Net.Commons;
using TechTalk.SpecFlow;
[Binding]
public class Steps
{
[When("I go to the registration form")]
public void WhenIGoToTheRegistrationForm()
{
// ...
}
[When("I enter my details")]
public void WhenIEnterMyDetails()
{
AllureApi.AddTestParameter("login", "johndoe");
AllureApi.AddTestParameter("password", "qwerty");
AllureApi.AddTestParameter("name", "John Doe");
AllureApi.AddTestParameter("birthday", "1970-01-01");
// ...
}
[Then("the profile should be created")]
public void ThenTheProfileShouldBeCreated()
{
// ...
}
}
Attachments
AllureApi.AddAttachment(string name, string type, string path)
AllureApi.AddAttachment(string name, string type, byte[] content, string fileExtension = "")
AllureApi.AddAttachment(string path, string? name = null)
AllureApi.AddScreenDiff(string expectedPng, string actualPng, string diffPng)
Add an attachment to the test result under the given name
.
TIP
You can use data produced by any function, not necessarily read from an actual file.
To create an attachment using the Runtime API, call AddAttachment()
at any point during your test. Pass either the content
or the path
from which the data will be read.
To ensure that the reader's web browser will display attachments correctly, it is recommended to specify each attachment's type. To do so, pass the media type of the content as type
and, optionally, a filename extension as fileExtension
. The media type affects how the data will be displayed in the test report, while the filename extension is appended to the filename when user wants to save the file.
csharp
using System.IO;
using System.Text;
using Allure.Net.Commons;
using TechTalk.SpecFlow;
[Binding]
public class Steps
{
[When("I open labels page")]
public void TestLabels()
{
AllureApi.AddAttachment(
"data.txt",
"text/plain",
Encoding.UTF8.GetBytes("This is the file content.")
);
AllureApi.AddAttachment(
"image1.png",
"image/png",
File.ReadAllBytes("/path/to/image1.png")
);
AllureApi.AddAttachment(
"image2.png",
"image/png",
"/path/to/image2.png"
);
}
}
Allure SpecFlow also provides a dedicated AddScreenDiff()
function for attaching visual comparisons.