Class GherkinFormatter
- Namespace
- TinyBDD
- Assembly
- TinyBDD.dll
Formats a ScenarioContext into Gherkin-style text and writes it to an IBddReporter.
public static class GherkinFormatter
- Inheritance
-
GherkinFormatter
- Inherited Members
Examples
Example: writing a simple scenario report
// Execute a scenario to populate the context
var ctx = Bdd.CreateContext(this);
await Bdd.Given(ctx, "start", () => 2)
.When("double", x => x * 2)
.Then(">= 4", v => v >= 4);
// Write as Gherkin
var reporter = new StringBddReporter();
GherkinFormatter.Write(ctx, reporter);
Console.WriteLine(reporter.ToString());
// Possible output:
// Feature: <YourTestClassNameOrFeatureAttributeName>
// Scenario: <TestMethodNameOrScenarioAttributeName>
// Given start [OK] 1 ms
// When double [OK] 0 ms
// Then >= 4 [OK] 0 ms
Example: error reporting
var ctx = Bdd.CreateContext(this);
await Bdd.Given(ctx, "seed", () => 1)
.When("boom", _ => throw new InvalidOperationException("nope"))
.Then("unreached", () => Task.CompletedTask);
var reporter = new StringBddReporter();
GherkinFormatter.Write(ctx, reporter);
Console.WriteLine(reporter.ToString());
// Output will include a FAIL line and an Error: detail for the When step.
Remarks
The output format is:
Feature: <FeatureName>
<FeatureDescription (optional)>
Scenario: <ScenarioName>
<StepKind> <StepTitle> [OK|FAIL] <ElapsedMs> ms
Error: <ExceptionType>: <Message> (only when a step fails)
Behavior:
FeatureDescriptionis written only when non-empty.- Each step line includes status (
OKorFAIL) and elapsed time in milliseconds (rounded). - When a step has an error, an indented
Error:line follows with the exception type and message.
This type is stateless and thread-safe.
Methods
Write(ScenarioContext, IBddReporter)
Writes the provided ScenarioContext to the reporter
using a compact Gherkin-style format.
public static void Write(ScenarioContext ctx, IBddReporter reporter)
Parameters
ctxScenarioContextThe scenario context containing feature, scenario, and step data to render.
reporterIBddReporterThe reporter that receives the formatted lines.
Remarks
This method expects non-null arguments. If ctx or reporter is null,
a runtime exception (e.g., NullReferenceException) may occur.
Elapsed time is rounded to the nearest millisecond and shown as
<number> ms.