BDD with Reqnroll¶
This page sets the rules for acceptance and end-to-end tests written as Gherkin features with Reqnroll and Reqnroll.MsTest. It is written for engineers who write feature files, step bindings and hooks, and for those who run them in pipelines. Most rules here stop a scenario from being skipped or bound wrongly while the run still reports success.
How the acceptance host, TestStartup and the hooks are wired is explained in BDD testing in ConnectSoft Microservice Template. The framework choice is CS-TST-001.
Silent skips¶
A Reqnroll run can report Passed! and exit with code 0 while scenarios did not run:
- An unbound step. By default Reqnroll reports a scenario with an undefined or pending step as inconclusive, which MSTest shows as skipped. Every other assertion in that scenario stops running too.
- A filter that matches nothing.
dotnet test --filteragainst an assembly with no matching test printsNo test matches the given testcase filterand, withTreatNoTestsAsErrorset tofalse, exits 0. When another assembly in the same run matches, the summary still saysPassed!.
Read the Skipped count and the Total line of every assembly, not only the final verdict. The rules below turn both cases into failures.
Rules¶
CS-TST-009: Fail the run on undefined or pending steps¶
Severity: blocker · Applies to: service, library, blazor, template · Enforced by: manual (target: ENF-059) · Status: active
Why¶
A step that no binding matches makes Reqnroll skip the whole scenario. With the default setting the test run still succeeds, so a scenario can stay skipped for months, and the defect it was written to catch goes unnoticed.
Do¶
- Add a
reqnroll.jsonfile to every Reqnroll test project and setmissingOrPendingStepsOutcometoError. - Mark a scenario that must not run with a tag that the pipeline filter excludes, and give the reason in the feature file.
Don't¶
- Leave
missingOrPendingStepsOutcomeunset or set toInconclusiveorIgnore. - Use a pending step as a placeholder for work in progress.
Example¶
{
"$schema": "https://schemas.reqnroll.net/reqnroll-config-latest.json",
"runtime": {
"missingOrPendingStepsOutcome": "Error"
}
}
How to check¶
- Every project that references
Reqnroll.MsTestcontainsreqnroll.jsonwith"missingOrPendingStepsOutcome": "Error". - A test run with skipped tests and no exclusion tags is a finding.
CS-TST-010: Treat a test run that runs no tests as an error¶
Severity: major · Applies to: service, library, api-client, blazor, template, pipelines · Enforced by: manual (target: ENF-060) · Status: active
Why¶
A test filter that matches nothing, for example a category spelled differently from the one on the tests, runs nothing and reports success. The pipeline is then green without having tested the code.
Do¶
- Set
<TreatNoTestsAsError>true</TreatNoTestsAsError>in every new.runsettingsfile. - In an existing repository, set it when you change its
.runsettingsfile or its test filters, after you have checked that every filtered assembly runs tests. This is a ratchet: once set, it is not turned off.
Don't¶
- Add a new
.runsettingsfile withTreatNoTestsAsErrorset tofalse. - Turn the setting back to
falseto make a pipeline pass.
Example¶
<RunSettings>
<RunConfiguration>
<TreatNoTestsAsError>true</TreatNoTestsAsError>
</RunConfiguration>
</RunSettings>
How to check¶
- New
.runsettingsfiles setTreatNoTestsAsErrortotrue. - A changed
.runsettingsfile does not change the value fromtruetofalse.
CS-TST-011: Write each step as a Cucumber expression or as an anchored regular expression¶
Severity: major · Applies to: service, library, blazor, template · Enforced by: manual · Status: active
Why¶
Reqnroll reads a step pattern as a Cucumber expression unless it looks like a regular expression. In a Cucumber expression, parentheses mean optional text, so a pattern such as the order is (open|closed) matches nothing. It compiles, and the step is reported as undefined.
Do¶
- Write a Cucumber expression with parameter types, such as
{string},{int}or a custom{OrderStatus}parameter, and/for alternative words. - When you need a regular expression, anchor it with
^at the start and$at the end.
Don't¶
- Mix regular expression syntax, such as
(a|b),(.*)or(\d+), into an unanchored pattern.
Example¶
// Do: Cucumber expression
[Given("the order {string} has {int} lines")]
// Do: anchored regular expression
[Given(@"^the order is (open|closed)$")]
// Don't: read as a Cucumber expression, matches nothing
[Given(@"the order is (open|closed)")]
How to check¶
Search new step attributes for a pattern that contains ( and | or \d but does not start with ^.
CS-TST-012: Write one binding per sentence form¶
Severity: major · Applies to: service, library, blazor, template · Enforced by: manual · Status: active
Why¶
Reqnroll passes no argument for an optional capture group that did not match. A binding with an optional group, such as (?: by (.*))?, then fails at run time with a parameter count mismatch, after the earlier steps have passed. The scenario that uses every optional part passes, which hides the problem.
Do¶
- Write a separate binding for each form of the sentence, and let them call one private method.
- Keep optional parts only when they contain no capture group.
Don't¶
- Put a capture group inside an optional group.
How to check¶
Search new step patterns for )? that encloses a capture group.
CS-TST-013: Use the keyword the step is bound with¶
Severity: major · Applies to: service, library, blazor, template · Enforced by: manual · Status: active
Why¶
And and But take the keyword of the step above them. An And after a Given is a Given step, so a binding registered only as [Then] is not considered, and the step is reported as undefined even though its text matches exactly.
Do¶
- Before you use an existing step, check which attribute it is bound with, and write that keyword, or an
Andunder a step with that keyword. - When a step is genuinely both a precondition and an outcome, add the second attribute to the binding.
Don't¶
- Rephrase a
[Then]step as aGivenprecondition without adding a[Given]attribute.
How to check¶
Run the changed feature. With CS-TST-009 in place, a keyword mismatch fails the run.
CS-TST-014: Restore every environment variable or static that a hook changes¶
Severity: major · Applies to: service, library, blazor, template · Enforced by: manual · Status: active
Why¶
Environment variables and static fields belong to the process, not to the feature. A hook that sets ASPNETCORE_ENVIRONMENT or a static switch for one tagged feature changes every feature that runs after it. The new scenarios pass alone and fail in the full run, or, worse, later features pass against the wrong configuration.
Do¶
- In
[BeforeFeature]or[BeforeScenario], record the previous value before you change it. - Restore it in the matching
[AfterFeature]or[AfterScenario]hook.
Don't¶
- Set an environment variable or a static in a hook without restoring it.
Example¶
[BeforeFeature("OrderLimitsDisabled")]
public static void DisableOrderLimits(FeatureContext featureContext)
{
featureContext["PreviousEnvironment"] = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "OrderLimitsDisabled");
}
[AfterFeature("OrderLimitsDisabled")]
public static void RestoreEnvironment(FeatureContext featureContext)
{
Environment.SetEnvironmentVariable(
"ASPNETCORE_ENVIRONMENT",
(string?)featureContext["PreviousEnvironment"]);
}
How to check¶
Every Environment.SetEnvironmentVariable or static assignment in a hook has a matching restore in the paired hook.
CS-TST-015: Filter by tag with one TestCategory clause per tag¶
Severity: major · Applies to: pipelines · Enforced by: manual · Status: active
Why¶
Reqnroll turns each scenario tag into an MSTest TestCategory. In a filter such as TestCategory=Orders|@Invoices, the second clause is a name match, not a category match, and selects nothing. The run is quietly narrower than asked.
Do¶
- Write
TestCategory=Orders|TestCategory=Invoicesto select several tags. - Write
FullyQualifiedName~OrderSubmissionto select one feature. - After a filtered run, read the Total count of every assembly.
Don't¶
- Mix a tag clause with a bare value in one filter.
- Report a filtered run as passing without checking that each assembly ran tests.
How to check¶
Filters in pipelines and scripts use a full TestCategory= clause for every tag.