Cornichon 3.0.0
See the version list below for details.
dotnet add package Cornichon --version 3.0.0
NuGet\Install-Package Cornichon -Version 3.0.0
<PackageReference Include="Cornichon" Version="3.0.0" />
paket add Cornichon --version 3.0.0
#r "nuget: Cornichon, 3.0.0"
// Install Cornichon as a Cake Addin #addin nuget:?package=Cornichon&version=3.0.0 // Install Cornichon as a Cake Tool #tool nuget:?package=Cornichon&version=3.0.0
Cornichon
A super-simple library to write tests in C#, using a syntax that looks as much like Gherkin as possible, while remaining C#, and without having to deal with nasty regular expressions. That's less pretty than the Gherkin natural language, but it also reduces the need for special tooling, in particular for debugging.
The second goal of Cornichon is to be Core-compatible. It also has no dependencies other than .NET itself.
Implementation
The Cornichon.Scenario
class provides a fluent API that exposes the classic Gherkin
steps: Given
, When
, Then
, and And
(no but).
All of these methods are actually the same implementation, which is to take the Action
passed as the argument, invoke that, and return the scenario object for chaining.
Testing with Cornichon
Cornichon makes no assumption about the testing framework, libraries, or runner. Bring your own. In this document, I'll use xUnit, but you don't have to, and Cornichon certainly takes no dependency on it.
It's typical to define the steps of your tests as closures, so state is managed automatically within the context of your test method:
/// <summary>
/// Refund item.
/// </summary>
public class RefundItem
{
[Fact]
public void CustomerReturningProductsCausesRefund()
{
var jeff = new Customer
{
Balance = 0
};
var order = new Order(new Product("microwave", 100));
Scenario
.Given(() => { jeff.Orders.Add(order); })
.And(() => { jeff.Receipts.Add(order.Receipt); })
.When(() => { jeff.Return(order.Products[0]); })
.Then(() => { Assert.Equal(100, jeff.Balance); });
}
}
But wait, except for the "scenario", "given", and stuff, this looks nothing like Gherkin! You are right, it doesn't, yet. That's because when writing tests with any Gherkin-based test framework, you have to start by writing preferably reusable pieces of code that implement the sentences that you'll use after the Gherkin keywords. That code typically implements user actions that can then be used to construct scenarios. This is no different, so let's refactor the code above.
First, we'll implement a test customer helper that implements possible customer actions:
public class CustomerTestHelper
{
public CustomerTestHelper()
{
Customer = new Customer
{
Balance = 0
};
}
public Customer Customer { get; }
public Order Order { get; set; }
public Purchase bought_a(string product)
{
return new Purchase(product, this);
}
public void has_a_receipt() => Customer.Receipts.Add(Order.Receipt);
public void returns_the(string productName)
{
if (Order != null)
{
var product = Order.Products.FirstOrDefault(product => product.Name == productName);
if (product != null)
{
Customer.Return(product);
}
}
}
public void should_have(decimal amount) => Assert.Equal(amount, Customer.Balance);
public class Purchase
{
private string _product;
private CustomerTestHelper _customerTestHelper;
public Purchase(string product, CustomerTestHelper customerTestHelper)
{
_product = product;
_customerTestHelper = customerTestHelper;
}
public void at(decimal price)
=> _customerTestHelper.Order = new Order(new Product(_product, price));
}
}
Then we can use that class to rewrite our test in a much more readable way:
/// <summary>
/// Refund item.
/// </summary>
public class RefundItem
{
private readonly TestCustomer jeff = new TestCustomer();
[Fact]
public void CustomerReturningProductsCausesRefund()
=> Scenario
.Given(() => { jeff.bought_a("microwave").at(100); })
.And(() => { jeff.has_a_receipt(); })
.When(() => { jeff.returns_the("microwave"); })
.Then(() => { jeff.should_have(100); });
}
Asynchronous tests
Since version 3.0.0, Cornichon allows for asynchronous actions. Tests can still be run synchronously as before, using the same syntax, but it is also possible to use asynchronous Lambdas to run tests asynchronously. The scenario steps are run sequentially, and the results of the previous steps are awaited before the next one is run. It is possible to use a mix of synchronous and asynchronous steps.
If the CustomerTestHelper
class is modified to have an asynchronous method:
public async Task asynchronously_returns_the(string productName)
{
if (Order != null)
{
Product product = await Order.FetchProductAsync(productName);
if (product != null)
{
await Customer.ReturnAsync(product);
}
}
}
And CustomerTestHelper.Purchase
also gets one:
public async Task asynchronously_for(decimal price)
=> _customerTestHelper.Order = await Order.CreateAsync(new Product(_product, price));
Then the scenario can be run asynchronously as well:
[Fact]
public async Task AsynchronousCustomerReturningProductsCausesRefund()
=> await Scenario
.Given(async () => { await jeff.bought_a("microwave").asynchronously_for(100); })
.And( () => { jeff.has_a_receipt(); })
.When(async () => { await jeff.asynchronously_returns_the("microwave"); })
.Then( () => { jeff.should_have(100); });
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
- System.Threading.Tasks.Extensions (>= 4.5.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.