Niche.GherkinSyntax
0.1.53
dotnet add package Niche.GherkinSyntax --version 0.1.53
NuGet\Install-Package Niche.GherkinSyntax -Version 0.1.53
<PackageReference Include="Niche.GherkinSyntax" Version="0.1.53" />
paket add Niche.GherkinSyntax --version 0.1.53
#r "nuget: Niche.GherkinSyntax, 0.1.53"
// Install Niche.GherkinSyntax as a Cake Addin
#addin nuget:?package=Niche.GherkinSyntax&version=0.1.53
// Install Niche.GherkinSyntax as a Cake Tool
#tool nuget:?package=Niche.GherkinSyntax&version=0.1.53
Scenario Testing
This is an experiment in creating a Gherkin style syntax for scenario testing within the confines of the C# language.
I tried to create a similar library some years ago, but the boilerplate overhead required by C# made the library unusable. Recent improvements to the C# language (mostly changes in the C# 7 series), have inspired me to try again and I think the result is at least somewhat usable.
Motivation
I'm a fan of the Gherkin syntax for behaviour driven development, though I've not managed to successfully evangelise its adoption on any serious projects. (Btw, this is not a reflection on the quality of tools like Cucumber or SpecFlow, more that the teams on those projects weren't ready to adopt this level of test automation.)
This project is an experiment to see how close we can get to Gherkin syntax within the confines of legal C# code. Recent extensions to the C# language (in the 7.x releases) have made this much more achievable than it was when I tried a similar experiment several years ago.
Example
Let's take a pair of very simple tests - the kind you might write if you're creating a basic four-function calculator.
Scenario: Can do basic addition
Given: a new calculator
When: the user enters 42.0
And: the user adds 15.0
Then: the answer is 57.0
Scenario: Can do basic subtraction
Given: a new calculator
When: the user enters 42.0
And: the user subtracts 15.0
Then: the answer is 57.0
With this library, you can fairly directly express these tests in pure C#:
[Fact]
public void CanDoBasicAddition()
=> Given(ANewCalculator)
.When(UserEnters, 42.0)
.And(UserAdds, 15.0)
.Then(AnswerIs, 57.0);
[Fact]
public void CanDoBasicSubtraction()
=> Given(ANewCalculator)
.When(UserEnters, 42.0)
.And(UserSubtracts, 15.0)
.Then(AnswerIs, 27.0);
How it works
The Given()
method is declared on the static class GherkinFactory
. Methods on this class are brought into scope at the top of the file:
using static Niche.GherkinSyntax.GherkinFactory;
When called, Given()
returns an IGivenSyntax
implementation, defining the available syntax; each call returns another instance, allowing method chaining through to the end.
The precondition method ANewCalculator()
is converted into a lambda expression by the C# compiler. The return value from this method is used as the context of the test - it carries the state information of the test.
private Calculator ANewCalculator() ...
The methods (UserEnters()
, UserAdds()
and UserSubtracts()
) are action methods that modify the context of the test.
private Calculator UserEnters(Calculator calculator, double value) {...}
private Calculator UserAdds(Calculator calculator, double value) { ... }
private Calculator UserSubtracts(Calculator calculator, double value) { ... }
Each action method returns the new context for the next step.
Finally, the expectation method AnswerIs()
is used to check that the answer is the one we want.
private static void AnswerIs(Calculator calculator, double value) ...
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.1.53 | 569 | 5/9/2019 |