Expect 1.0.0
dotnet add package Expect --version 1.0.0
NuGet\Install-Package Expect -Version 1.0.0
<PackageReference Include="Expect" Version="1.0.0" />
paket add Expect --version 1.0.0
#r "nuget: Expect, 1.0.0"
// Install Expect as a Cake Addin #addin nuget:?package=Expect&version=1.0.0 // Install Expect as a Cake Tool #tool nuget:?package=Expect&version=1.0.0
Expect
Expression-based Assertion Library for .NET
English | 日本語
Expect is an assertion library for .NET.
The concept of Expect is inspired by power-assert and swift-testing, offering better assertions using Expression Trees.
Why Expect?
In test frameworks like NUnit, assertions are written using Assert.That
. Below is an example of a unit test using NUnit:
// Define a record for testing
record Person(string Name, int Age);
[Test]
public void Example()
{
var person = new Person("Alice", 16);
Assert.That(person.Age, Is.GreaterThanOrEqualTo(18));
}
This test will fail, displaying the following message:
Expected: greater than or equal to 18
But was: 16
The problem with assertions using Assert.That
is that users are required to distinguish between a large number of Is.**
usages. Many test frameworks provide a vast number of APIs to cover all use cases, forcing users to memorize them.
Some assertion libraries adopt a Fluent Interface, such as foo.Should().Not.Be.Null()
. However, I believe such APIs should not be used, as they reduce readability. Overly verbose Fluent Interfaces, modeled after natural language, are not necessarily easy to read. Tests should not aim to mimic natural language but instead be programmer-friendly and written in readable code.
With Expect, there is only one API provided:
using static Expect.Expectation;
[Test]
public void Example()
{
var person = new Person("Alice", 16);
// Pass a condition
Expect(() => person.Age >= 18);
}
This displays the following message:
(person.Age → 16) >= 18
Expect analyzes the provided expression to display an appropriate error message.
Below is a comparison of collection assertions:
int[] array = [1, 2, 3, 4, 5];
// NUnit
Assert.That(array, Has.Member(-1));
// Expect
Expect(() => array.Contains(-1));
The error messages will be as follows:
Expected: some item equal to -1 and some item equal to 1
But was: < 1, 2, 3, 4, 5 >
(array → [1, 2, 3, 4, 5]).Contains(-1)
Installation
NuGet packages
Expect requires .NET Standard 2.0 or higher. The package is available via NuGet.
.NET CLI
dotnet add package Expect
Package Manager
Install-Package Expect
License
This library is released under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. 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 is compatible. 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. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.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 is compatible. |
.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.
-
.NETStandard 2.1
- No dependencies.
-
net6.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.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 |
---|---|---|
1.0.0 | 28 | 1/24/2025 |