Skip to content

IBeforeStep

Romfos edited this page Feb 26, 2026 · 2 revisions

Description

You can use IBeforeStep for create custom logic that will be executed before each step:

namespace BddDotNet.Steps;

public interface IBeforeStep
{
    Task BeforeStepAsync(StepContext context);
}

Example of usage

  1. Create class with custom logic
internal sealed class MyBeforeStep : IBeforeStep
{
    public async Task BeforeStepAsync(StepContext context)
    {
        Console.WriteLine($"MyBeforeStep {context.Type}, {context.Text}");
    }
}
  1. Register it in Program.cs pipeline via BeforeStep extension method on IServiceCollection
using BddDotNet;
using BddDotNet.Steps;
using Microsoft.Testing.Platform.Builder;

var builder = await TestApplication.CreateBuilderAsync(args);

var services = builder.AddBddDotNet();

services.BeforeStep<MyBeforeStep>();

services.SourceGeneratedGherkinScenarios();
services.SourceGeneratedGherkinSteps();

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

Result:

image

Clone this wiki locally