Repository navigation
BDD+TUnit... #7103
|
Is there a good/ great/ wonderful BDD-style experience with BDD-style acceptance testing? Ranging from light-weight to full-on Gherkin generation. I've been using LightBDD but it currently doesn't support the cancellation token being past through as a parameter, which is almost muscle memory for me these days. SpecFlow/ Reqnroll are all brilliant... but they are large. I liked the idea of LightBDD because it was a good starting point for readable-style acceptance testing. |
Replies: 2 comments 5 replies
|
Both tools work with TUnit today. Neither accepts a Full Gherkin: Reqnroll.TUnit A using TUnit.Core;
[Binding]
public class OllamaSteps(TestContext testContext)
{
private CancellationToken Ct => testContext.Execution.CancellationToken;
[Given("the Ollama service is reachable")]
public async Task GivenOllamaIsReachable()
{
using OllamaClient client = new(config.OllamaEndpoint, TimeSpan.FromMinutes(config.OllamaTimeoutMinutes));
var ok = await client.IsReachableAsync(Ct);
await Assert.That(ok).IsTrue();
}
}Lighter, code-first: LightBDD.TUnit A public abstract class FeatureBase : FeatureFixture
{
protected static CancellationToken ScenarioToken =>
TestContext.Current?.Execution.CancellationToken ?? CancellationToken.None;
}Pass In both cases it's the same token TUnit would otherwise inject as a parameter: I haven't run the Reqnroll snippet myself; it follows the documented |
|
Another lightweight, code-first option is BDTest (also mine). It has no Gherkin and no generated code. You write It has a dedicated TUnit adapter ( [Story(AsA = "user", IWant = "to talk to Ollama", SoThat = "I get answers")]
public class OllamaTests : TUnitBDTestBase<OllamaContext>
{
[Test, Timeout(60_000)]
[ScenarioText("Ollama is reachable")]
public async Task OllamaIsReachable()
{
await Given(ct => Context.Client.ConnectAsync(ct))
.When(ct => Context.Client.PingAsync(ct))
.Then(() => Context.ThePingSucceeded());
}
}You can also set per-step and per-scenario limits with It also generates a single, self-contained HTML report from the run that works offline. It shows a summary of results, failures grouped by exception, and scenarios grouped by story. You can search and filter by status or tag, and each scenario expands to show its steps with their status and timing, exceptions, retries and captured output. That makes it useful for sharing acceptance test results with non-developers. Add public class BDTestReports
{
[After(HookType.Assembly)]
public static async Task AfterAllTests()
{
var assembly = typeof(BDTestReports).Assembly;
try
{
var run = TUnitBDTestAssembly.GetRun(assembly);
await File.WriteAllTextAsync("BDTest-Report.html", BDTestHtmlHelper.GetTestHtmlReport(null, run));
}
finally { TUnitBDTestAssembly.Dispose(assembly); }
}
}There's also an optional report server if you want to publish and browse reports from CI. Docs: |
Another lightweight, code-first option is BDTest (also mine). It has no Gherkin and no generated code. You write
Given/When/Thenchains in C#.It has a dedicated TUnit adapter (
BDTest.TUnit), and cancellation tokens work the way you'd expect. Any async step can take aCancellationToken, and the adapter automatically links it toTestContext.Current.Execution.CancellationToken. That means TUnit's[Timeout],Execution.Cancel()and runner cancellation all flow into your steps: