Skip to content

Enabling model transformations

Romfos edited this page Feb 26, 2026 · 2 revisions

Description

There some cases when could be useful to use C# models in gherkin steeps.

For you have model like this:

internal sealed class CustomModel
{
    public required int First { get;  init; }
    public required string Second { get; init; }
}

And gherkin step like this:

namespace BddDotNetAot.Steps;

internal sealed class Steps5
{
    [When("this is given step with model transformation:")]
    public void Step1(CustomModel model)
    {
        // use model
    }
}

And Feature file like this:

Scenario: step with model transformation scenario
    When this is given step with model transformation:
    | Name   | Value |
    | First  | 1     |
    | Second | abcd  |

How to enable it

  1. You need to install BddDotNet.Gherkin.Models nuget package
<PackageReference Include="BddDotNet.Gherkin.Models" Version="2.1.0" />
  1. Call ModelTransformation<CustomModel>() extension method on IServiceCollection type in Program.cs
using BddDotNet;
using BddDotNet.Gherkin.Models;
using BddDotNetAot.Models;
using BddDotNetAot.Transformations;
using Microsoft.Testing.Platform.Builder;

var builder = await TestApplication.CreateBuilderAsync(args);

var services = builder.AddBddDotNet();
services.SourceGeneratedGherkinScenarios();
services.SourceGeneratedGherkinSteps();

services.ModelTransformation<CustomModel>();

using var testApp = await builder.BuildAsync();
return await testApp.RunAsync();

Usage & Limitations

Key points:

  1. Model should have at least 1 constructor. First constructor will be used.
  2. Public properties with set are supported
  3. Public fields with are supported
  4. Records are supported
  5. Nested models are not supported for now
  6. Each model transformation should be registered in Program.cs

Clone this wiki locally