Skip to content

Enabling Gherkin support

Romfos edited this page Feb 26, 2026 · 10 revisions

How to enable gherkin support

  1. Prerequisites. Setup BddDotNet project
  2. Add BddDotNet.Gherkin.SourceGenerator nuget package
<PackageReference Include="BddDotNet.Gherkin.SourceGenerator" Version="2.1.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
  1. Add using directive with you assembly name to Program.cs file
  2. Use SourceGeneratedGherkinScenarios and SourceGeneratedGherkinSteps extensions for configuration

Example:

image

How it works

Every time when you try to build your current project source generator will produce these method with content like this:

// <auto-generated/>
#nullable disable warnings
using BddDotNet;
using BddDotNet.Scenarios;
using Microsoft.Extensions.DependencyInjection;

namespace BddDotNet.Gherkin;
internal static partial class GherkinSourceGeneratorExtensions
{
    public static partial void SourceGeneratedGherkinScenarios(this IServiceCollection services)
    {
        services.Scenario("DemoApp", "DemoApp", "Feature1", "demo scenario", """E:\dev\BDDNugetTest\BDDNugetTest\Features\Feature1.feature""", 6, async scenario =>
        {
#line (7, 5) - (8, 1) 12 "E:\dev\BDDNugetTest\BDDNugetTest\Features\Feature1.feature"
            await scenario.Given("""this is simple given step""");
#line (8, 5) - (9, 1) 12 "E:\dev\BDDNugetTest\BDDNugetTest\Features\Feature1.feature"
            await scenario.When("""this is simple when step""");
#line (9, 5) - (10, 1) 12 "E:\dev\BDDNugetTest\BDDNugetTest\Features\Feature1.feature"
            await scenario.Then("""this is simple then step""");
#line default
        });
    }
}
// <auto-generated/>
#nullable disable warnings
using BddDotNet;
using BddDotNet.Steps;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace BddDotNet.Gherkin;
internal static partial class GherkinSourceGeneratorExtensions
{
    public static partial void SourceGeneratedGherkinSteps(this IServiceCollection services)
    {
        services.TryAddScoped<global::DemoApp.Steps.Steps>();
        services.Given(new("this is simple given step"), services => services.GetRequiredService<global::DemoApp.Steps.Steps>().Step1);
        services.When(new("this is simple when step"), services => services.GetRequiredService<global::DemoApp.Steps.Steps>().Step2);
        services.Then(new("this is simple then step"), services => services.GetRequiredService<global::DemoApp.Steps.Steps>().Step3);
    }
}

Clone this wiki locally