Getting started
Installation
-
Install the
aweXpect
nuget packagedotnet add package aweXpect
-
Add the following
using
statement:using aweXpect;
This brings the static
Expect
class and lots of extension methods into scope. -
Simplify expectations (optional)
If you want to simplify the assertions, you can add aglobal using static aweXpect.Expect;
statement anywhere in your test project. This allows writing a more concise syntax:// ↓ Default behaviour
await Expect.That(subject).IsTrue();
await That(subject).IsTrue();
// ↑ With global static
Write your first expectation
Write your first expectation:
[Fact]
public async Task SomeMethod_WhenInputIsInvalid_ShouldReturnFalse()
{
bool result = SomeMethod("invalid input");
await Expect.That(result).IsFalse();
}
If it fails, it will throw a framework-specific exception with the following message:
Expected result to
be False,
but it was True
Add a reason
You can add a reason for all expectations, that will be included in the exception message:
[Fact]
public async Task SomeMethod_WhenInputIsInvalid_ShouldReturnFalse()
{
bool result = SomeMethod("invalid input");
await Expect.That(result).IsFalse().Because("the input was invalid");
}
This will result in
Expected result to
be False, because the input was invalid,
but it was True
Migration
We added support to migrate from other testing frameworks.
-
Temporarily install the aweXpect.Migration package
in the test project and add the following global using statements in the test project:
global using System.Threading.Tasks;
global using aweXpect; -
Depending on the framework, the assertions will be marked with a warning:
- For FluentAssertions:
All usages of.Should()
will be marked withaweXpectM002: fluentassertions should be migrated to aweXpect
- For Xunit:
All usages ofAssert
will be marked withaweXpectM003: Xunit assertions should be migrated to aweXpect
- For FluentAssertions:
-
Most warnings can be automatically fixed with a code fix provider. Make sure to await all migrated expectations (fix
aweXpect0001: Expectations must be awaited or verified
). -
Fix the remaining warnings manually.
-
Remove the
aweXpect.Migration
package again.
Detecting test frameworks
We support a lot of different unit testing frameworks:
- Microsoft Test Framework
- xUnit (v2 & v3)
- NUnit (v3 & v4)
- TUnit
When you have a reference to the corresponding test framework assembly, we will automatically throw the corresponding exceptions.