Other BDD Frameworks: Behave and Reqnroll
On this page
Gherkin is a shared format, so the same feature file can be executed in many languages. This chapter runs the "wrong credentials" outline from chapter 6 with two popular alternatives to Cucumber itself: Behave for Python and Reqnroll for .NET. Both are separate open-source projects that read Gherkin; neither is maintained by the Cucumber team. The point is to show what stays the same (almost everything) and what changes (how steps get their arguments and their state).
The feature file is identical for both:
Feature: Signing in to the secure area
Scenario Outline: Wrong credentials are rejected
When someone signs in as "<username>" with password "<password>"
Then the sign-in is rejected because the <field> is invalid
Examples:
| username | password | field |
| tomsmith | not-his-password | password |
| tomjones | SuperSecretPassword! | username |
Behave (Python)#
Behave expects features/ with the .feature files, features/steps/ with step modules, and an optional features/environment.py for hooks.
python3 -m venv .venv
.venv/bin/pip install behave==1.3.3 selenium==4.52.0
Hooks and state: environment.py
#
Behave's hooks are plain functions with fixed names (before_all, before_feature, before_scenario, after_step and so on). Shared state goes on the context object, which Behave passes to every hook and step. Attributes you set during a scenario are removed when the scenario ends, which gives you the same per-scenario isolation as the World in Cucumber-JS.
import os
from selenium import webdriver
def before_scenario(context, scenario):
if os.environ.get("GRID") == "testingbot":
options = webdriver.ChromeOptions()
options.browser_version = "latest"
options.platform_name = os.environ.get("TB_PLATFORM", "WIN11")
options.set_capability("tb:options", {
"name": scenario.name,
"build": os.environ.get("TB_BUILD", "local"),
})
hub = f"https://{os.environ['TB_KEY']}:{os.environ['TB_SECRET']}@hub.testingbot.com/wd/hub"
context.driver = webdriver.Remote(command_executor=hub, options=options)
else:
options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
context.driver = webdriver.Chrome(options=options)
def after_scenario(context, scenario):
try:
if os.environ.get("GRID") == "testingbot":
status = "passed" if scenario.status.name == "passed" else "failed"
context.driver.execute_script(f"tb:test-result={status}")
finally:
context.driver.quit()
Only an explicit passed is reported as passed: a scenario that ended undefined, skipped or untested checked nothing, so it must not show up green on the dashboard.
Steps: features/steps/sign_in_steps.py
#
Behave's default step matcher uses parse-style patterns: {username} captures text, and quotes in the pattern are matched literally. The decorators are @given, @when and @then.
from behave import then, when
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
MESSAGES = {
"username": "Your username is invalid!",
"password": "Your password is invalid!",
}
@when('someone signs in as "{username}" with password "{password}"')
def step_sign_in(context, username, password):
driver = context.driver
driver.get("https://the-internet.herokuapp.com/login")
driver.find_element(By.ID, "username").send_keys(username)
driver.find_element(By.ID, "password").send_keys(password)
driver.find_element(By.CSS_SELECTOR, "button[type=submit]").click()
@then("the sign-in is rejected because the {field} is invalid")
def step_rejected(context, field):
flash = WebDriverWait(context.driver, 10).until(
EC.visibility_of_element_located((By.ID, "flash"))
)
assert MESSAGES[field] in flash.text, flash.text
Run it locally with .venv/bin/behave, and on the grid with:
GRID=testingbot TB_BUILD=behave-course .venv/bin/behave
1 feature passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 0 skipped
4 steps passed, 0 failed, 0 skipped
On TestingBot the two rows ran as fbaf9ee65502-b237cd7c19eb-a1cc8477e669-179455818250-38612372 and 56716aa15b1f-4035d7368858-325a50656a00-179455819132-72286370, both marked as passed. Behave names outline rows after the outline plus the row, for example "Wrong credentials are rejected -- @1.1", so the dashboard shows which row a session ran. The Behave docs page has more capabilities and a parallel example repository. If your team writes Python but does not need Gherkin, the Selenium Python course with pytest is the simpler route; pytest-bdd is a third option that runs Gherkin scenarios as pytest tests.
In Behave, where does the WebDriver created in before_scenario live so that steps can use it?
The context is Behave's per-run state object; attributes set during a scenario are cleaned up when it ends. A module global would leak between scenarios.
SpecFlow and Reqnroll (.NET)#
For years the .NET BDD tool was SpecFlow. Its owner, Tricentis, announced in December 2024 that SpecFlow reached end of life on 31 December 2024; specflow.org stated that "SpecFlow will no longer be available after December 31, 2024", and the SpecFlow GitHub projects were removed in January 2025 (Reqnroll's announcement, 9 January 2025). Existing NuGet packages still install, but receive no fixes.
Reqnroll is the community fork of the SpecFlow codebase, started in early 2024 by people from the original SpecFlow team and renamed because Tricentis holds the SpecFlow trademark. It is very close to SpecFlow, so migration is mostly a package and namespace change; the Reqnroll docs page has a migration section, and the SpecFlow page remains for existing projects. Start new projects on Reqnroll.
Project#
A test project with Reqnroll on NUnit (xUnit and MSTest packages exist too):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
<PackageReference Include="NUnit" Version="4.6.1" />
<PackageReference Include="NUnit3TestAdapter" Version="6.3.1" />
<PackageReference Include="Reqnroll.NUnit" Version="3.3.4" />
<PackageReference Include="Selenium.WebDriver" Version="4.52.0" />
<PackageReference Include="Selenium.Support" Version="4.52.0" />
</ItemGroup>
</Project>
Reqnroll generates an NUnit test for every scenario at build time, so dotnet test runs the feature files like any other tests.
Hooks and state#
Reqnroll has a built-in dependency injection container, so it works like PicoContainer in chapter 5: classes marked [Binding] receive what they need in their constructor, one instance per scenario. A hook can also register an object it created, so that step classes can ask for it:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using Reqnroll;
using Reqnroll.BoDi;
namespace SignIn.Specs.Support;
[Binding]
public class BrowserHooks(IObjectContainer container, ScenarioContext scenario)
{
private static bool OnTestingBot => Environment.GetEnvironmentVariable("GRID") == "testingbot";
[BeforeScenario]
public void StartBrowser()
{
var options = new ChromeOptions();
IWebDriver driver;
if (OnTestingBot)
{
options.BrowserVersion = "latest";
options.PlatformName = Environment.GetEnvironmentVariable("TB_PLATFORM") ?? "WIN11";
options.AddAdditionalOption("tb:options", new Dictionary<string, object>
{
["key"] = Environment.GetEnvironmentVariable("TB_KEY")!,
["secret"] = Environment.GetEnvironmentVariable("TB_SECRET")!,
["name"] = scenario.ScenarioInfo.Title,
["build"] = Environment.GetEnvironmentVariable("TB_BUILD") ?? "local",
});
driver = new RemoteWebDriver(new Uri("https://hub.testingbot.com/wd/hub"), options);
}
else
{
options.AddArgument("--headless=new");
driver = new ChromeDriver(options);
}
container.RegisterInstanceAs(driver);
}
[AfterScenario]
public void StopBrowser(IWebDriver driver)
{
try
{
if (OnTestingBot)
{
var status = scenario.TestError == null ? "passed" : "failed";
((IJavaScriptExecutor)driver).ExecuteScript($"tb:test-result={status}");
}
}
finally
{
driver.Quit();
}
}
}
Here the key and secret travel in tb:options instead of the hub URL; TestingBot accepts both. ScenarioContext.TestError is set when any step threw, which is how the hook knows the result.
Steps#
Reqnroll supports Cucumber expressions, so the patterns look exactly like the Java ones. {word} captures a single word:
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using Reqnroll;
namespace SignIn.Specs.StepDefinitions;
[Binding]
public class SignInSteps(IWebDriver driver)
{
[When("someone signs in as {string} with password {string}")]
public void WhenSomeoneSignsIn(string username, string password)
{
driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/login");
driver.FindElement(By.Id("username")).SendKeys(username);
driver.FindElement(By.Id("password")).SendKeys(password);
driver.FindElement(By.CssSelector("button[type=submit]")).Click();
}
[Then("the sign-in is rejected because the {word} is invalid")]
public void ThenRejected(string field)
{
var flash = new WebDriverWait(driver, TimeSpan.FromSeconds(10))
.Until(d => d.FindElement(By.Id("flash")));
Assert.That(flash.Text, Does.Contain($"Your {field} is invalid!"));
}
}
Run it with dotnet test locally, and on the grid with:
GRID=testingbot TB_BUILD=reqnroll-course dotnet test
Passed! - Failed: 0, Passed: 2, Skipped: 0, Total: 2
On TestingBot: 5c7e26c142c2-72fb58488648-e43eff9ea3d4-179455822460-97054822 and abbcac9c0329-b8af0a4a4ea7-c6f3ca48770d-179455823205-47512111, both passed. Reqnroll gives both rows the outline title, which is one more reason to put a placeholder in it (chapter 6).
A team is starting a new .NET project and wants Gherkin scenarios. What should they use?
SpecFlow packages still install but get no fixes. Reqnroll continues the same codebase under a new name and accepts SpecFlow-style projects with small changes.
What carried over#
| Concept | Cucumber-JVM | Cucumber-JS | Behave | Reqnroll |
|---|---|---|---|---|
| Per-scenario state | PicoContainer (DI) | World (this) |
context |
Built-in DI |
| Step patterns | Cucumber expressions | Cucumber expressions |
parse patterns |
Cucumber expressions |
| Before/after scenario |
@Before / @After
|
Before / After
|
before_scenario / after_scenario
|
[BeforeScenario] / [AfterScenario]
|
| Result for the hook | Scenario.isFailed() |
result.status |
scenario.status |
ScenarioContext.TestError |
The skills that matter most, discovery and writing good Gherkin, do not depend on the tool at all.