|
| 1 | +# Configuration |
| 2 | + |
| 3 | +BDDfy's behavior is controlled through the static `Configurator` class. Everything from step scanning to reporting is pluggable. |
| 4 | + |
| 5 | +## The Configurator Class |
| 6 | + |
| 7 | +```csharp |
| 8 | +using TestStack.BDDfy.Configuration; |
| 9 | + |
| 10 | +// All configuration is done via static properties: |
| 11 | +Configurator.Processors // Per-scenario pipeline |
| 12 | +Configurator.BatchProcessors // End-of-run reporters |
| 13 | +Configurator.Scanners // Step discovery |
| 14 | +Configurator.IdGenerator // Unique key generation |
| 15 | +Configurator.StepExecutor // Step execution strategy |
| 16 | +Configurator.Humanizer // Method name → readable text |
| 17 | +Configurator.FluentScannerFactory // Fluent scanner creation |
| 18 | +Configurator.StepTitleFactory // Step title generation |
| 19 | +Configurator.CultureInfo // Culture for formatting |
| 20 | +Configurator.AsyncVoidSupportEnabled // async void step support (default: true) |
| 21 | +``` |
| 22 | + |
| 23 | +## Per-Scenario Processors |
| 24 | + |
| 25 | +Processors run for each scenario in order of their `ProcessType`: |
| 26 | + |
| 27 | +```csharp |
| 28 | +// Disable console output |
| 29 | +Configurator.Processors.ConsoleReport.Disable(); |
| 30 | + |
| 31 | +// Add a custom processor |
| 32 | +Configurator.Processors.Add(() => new MyCustomProcessor()); |
| 33 | +``` |
| 34 | + |
| 35 | +Built-in processors (in execution order): |
| 36 | +1. **TestRunner** — executes the scenario steps |
| 37 | +2. **ConsoleReporter** — prints to console |
| 38 | +3. **ExceptionProcessor** — handles/rethrows exceptions |
| 39 | +4. **StoryCache** — stores results for batch reporters |
| 40 | +5. **Disposer** — disposes the test object if `IDisposable` |
| 41 | + |
| 42 | +### Enabling/Disabling |
| 43 | + |
| 44 | +```csharp |
| 45 | +Configurator.Processors.TestRunner.Disable(); |
| 46 | +Configurator.Processors.ConsoleReport.Disable(); |
| 47 | +Configurator.Processors.StoryCache.Disable(); |
| 48 | +``` |
| 49 | + |
| 50 | +## Batch Processors |
| 51 | + |
| 52 | +Run once after all scenarios complete (typically reporters): |
| 53 | + |
| 54 | +```csharp |
| 55 | +Configurator.BatchProcessors.HtmlReport.Enable(); |
| 56 | +Configurator.BatchProcessors.HtmlMetroReport.Disable(); |
| 57 | +Configurator.BatchProcessors.MarkDownReport.Enable(); |
| 58 | +Configurator.BatchProcessors.DiagnosticsReport.Enable(); |
| 59 | + |
| 60 | +// Add custom batch processor |
| 61 | +Configurator.BatchProcessors.Add(new MyBatchProcessor()); |
| 62 | +``` |
| 63 | + |
| 64 | +## Step Scanners |
| 65 | + |
| 66 | +Control how BDDfy discovers steps in your test classes: |
| 67 | + |
| 68 | +```csharp |
| 69 | +// Disable attribute-based scanning |
| 70 | +Configurator.Scanners.ExecutableAttributeScanner.Disable(); |
| 71 | + |
| 72 | +// Disable method-name scanning |
| 73 | +Configurator.Scanners.DefaultMethodNameStepScanner.Disable(); |
| 74 | + |
| 75 | +// Add a custom step scanner |
| 76 | +Configurator.Scanners.Add(() => new MyCustomStepScanner()); |
| 77 | +``` |
| 78 | + |
| 79 | +## Story Metadata Scanner |
| 80 | + |
| 81 | +Replace how story metadata is discovered: |
| 82 | + |
| 83 | +```csharp |
| 84 | +Configurator.Scanners.StoryMetadataScanner = () => new MyCustomMetadataScanner(); |
| 85 | +``` |
| 86 | + |
| 87 | +## Humanizer |
| 88 | + |
| 89 | +The humanizer converts method names into readable titles: |
| 90 | + |
| 91 | +```csharp |
| 92 | +// Replace with a custom humanizer |
| 93 | +Configurator.Humanizer = new MyHumanizer(); |
| 94 | + |
| 95 | +public class MyHumanizer : IHumanizer |
| 96 | +{ |
| 97 | + public string Humanize(string name) |
| 98 | + { |
| 99 | + // Custom logic to convert PascalCase/underscored names to text |
| 100 | + return name.Replace("_", " "); |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +## Step Executor |
| 106 | + |
| 107 | +Override how individual steps are executed: |
| 108 | + |
| 109 | +```csharp |
| 110 | +Configurator.StepExecutor = new MyStepExecutor(); |
| 111 | + |
| 112 | +public class MyStepExecutor : IStepExecutor |
| 113 | +{ |
| 114 | + public void Execute(Step step, object testObject) |
| 115 | + { |
| 116 | + // Add logging, timing, retry logic, etc. |
| 117 | + Console.WriteLine($"Executing: {step.Title}"); |
| 118 | + step.Execute(testObject); |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +## Step Title Factory |
| 124 | + |
| 125 | +Customize how step titles are generated from expressions: |
| 126 | + |
| 127 | +```csharp |
| 128 | +Configurator.StepTitleFactory = new MyStepTitleFactory(); |
| 129 | +``` |
| 130 | + |
| 131 | +### IncludeInputsInStepTitle |
| 132 | + |
| 133 | +Controls whether step arguments are appended to step titles (default: `true`): |
| 134 | + |
| 135 | +```csharp |
| 136 | +Configurator.StepTitleFactory.IncludeInputsInStepTitle = false; |
| 137 | +``` |
| 138 | + |
| 139 | +### AddGherkinPrefixToSecondarySteps |
| 140 | + |
| 141 | +When a step has an explicit title (via `[Given("...")]`, `[When("...")]`, `[Then("...")]`, or `[StepTitle("...")]`), BDDfy prepends the Gherkin keyword to the title by default. For consecutive steps in the same group, this means they get an "And" prefix. Set this to `false` to disable the "And" prefix on consecutive custom-titled steps: |
| 142 | + |
| 143 | +```csharp |
| 144 | +Configurator.StepTitleFactory.AddGherkinPrefixToSecondarySteps = true; // default |
| 145 | +``` |
| 146 | + |
| 147 | +**With prefix enabled (default):** |
| 148 | + |
| 149 | +``` |
| 150 | +Scenario: With prefix enabled |
| 151 | + Given the user is logged in |
| 152 | + And the cart has items |
| 153 | + And the payment gateway is available |
| 154 | + When the user checks out |
| 155 | + Then the order is confirmed |
| 156 | + And a confirmation email is sent |
| 157 | +``` |
| 158 | + |
| 159 | +```csharp |
| 160 | +Configurator.StepTitleFactory.AddGherkinPrefixToSecondarySteps = false; |
| 161 | +``` |
| 162 | + |
| 163 | +**With prefix disabled:** |
| 164 | + |
| 165 | +``` |
| 166 | +Scenario: With prefix disabled |
| 167 | + Given the user is logged in |
| 168 | + the cart has items |
| 169 | + the payment gateway is available |
| 170 | + When the user checks out |
| 171 | + Then the order is confirmed |
| 172 | + a confirmation email is sent |
| 173 | +``` |
| 174 | + |
| 175 | +> **Note:** Primary step keywords (Given/When/Then) are always applied regardless of this setting. Only the consecutive "And" prefix on custom-titled steps is affected. |
| 176 | +
|
| 177 | +## Culture |
| 178 | + |
| 179 | +Set the culture used for formatting values in reports: |
| 180 | + |
| 181 | +```csharp |
| 182 | +Configurator.CultureInfo = new CultureInfo("en-US"); |
| 183 | +``` |
| 184 | + |
| 185 | +## Async Void Support |
| 186 | + |
| 187 | +BDDfy can detect and await `async void` step methods. Disable if it causes issues: |
| 188 | + |
| 189 | +```csharp |
| 190 | +Configurator.AsyncVoidSupportEnabled = false; |
| 191 | +``` |
| 192 | + |
| 193 | +## Configuration Timing |
| 194 | + |
| 195 | +Configure BDDfy before any tests run. With xUnit, use a module initializer or assembly fixture: |
| 196 | + |
| 197 | +```csharp |
| 198 | +using System.Runtime.CompilerServices; |
| 199 | +using TestStack.BDDfy.Configuration; |
| 200 | + |
| 201 | +public static class BDDfySetup |
| 202 | +{ |
| 203 | + [ModuleInitializer] |
| 204 | + public static void Initialize() |
| 205 | + { |
| 206 | + Configurator.BatchProcessors.HtmlReport.Disable(); |
| 207 | + Configurator.BatchProcessors.MarkDownReport.Enable(); |
| 208 | + } |
| 209 | +} |
| 210 | +``` |
| 211 | + |
| 212 | +With NUnit, use `[SetUpFixture]`: |
| 213 | + |
| 214 | +```csharp |
| 215 | +[SetUpFixture] |
| 216 | +public class BDDfySetup |
| 217 | +{ |
| 218 | + [OneTimeSetUp] |
| 219 | + public void Setup() |
| 220 | + { |
| 221 | + Configurator.BatchProcessors.HtmlMetroReport.Enable(); |
| 222 | + } |
| 223 | +} |
| 224 | +``` |
0 commit comments