GwtUnit.XUnit
1.9.0
dotnet add package GwtUnit.XUnit --version 1.9.0
NuGet\Install-Package GwtUnit.XUnit -Version 1.9.0
<PackageReference Include="GwtUnit.XUnit" Version="1.9.0" />
paket add GwtUnit.XUnit --version 1.9.0
#r "nuget: GwtUnit.XUnit, 1.9.0"
// Install GwtUnit.XUnit as a Cake Addin #addin nuget:?package=GwtUnit.XUnit&version=1.9.0 // Install GwtUnit.XUnit as a Cake Tool #tool nuget:?package=GwtUnit.XUnit&version=1.9.0
GWT Unit - Given When Then unit testing.
This library provides a base classes for unit testing. Using a DSL called Given When Then to facilitate and structure tests for readability and TDD.
The difference of base classes resides with the Given property. One class uses a dynamic type and the other uses a class you specify. In both cases the Thens class is specified using generic syntax.
The dynamic typing allows properties to be created on the the fly without having previously defined it. The only downside is lack of intellisense in VisualStduio(c).
GWT derives from AAA (Arrange Act Assert) but strives to provide organization and readability in the tests.
Given (Arrange)
Setup the data and context for the test.
When (Act)
Make the When actions composable. The When method takes params array of Action methods.
Then (Assert)
A class where all result context can be stored during a test and can be asserted on. I prefer using FluentAssertions from NuGet. These extension methods provide cleaner test failure messages and make the code more readable.
Features
- Exception assertions closer to the origin of the thrown exception
- Given and Then automatically cleaned up before each test
- When assumes the Creating action will be done first and can be ommitted, however if Creating is provided then it will not be called automatically.
- XUnitTestBase is purpose built to accomodate the XUnit framework.
- Use Dependency Injection through IServiceCollection and IServiceProvider.
XUnit support
using FluentAssertions;
using Randal.Core.Testing.XUnit;
namespace Someplace
{
public sealed class TestObjectTests : XUnitTestBase<TestObjectTests.Thens>
{
[Fact, PositiveTest]
public void ShouldHaveValidInstanceWithValue_WhenCreatingObject_GivenValue123()
{
Given.NeededValue = 123; // Given is a dynamic object, create any number of property values on the fly
When(Creating); // 'When' consumes and executes a list of Action
Then.Target.Should().NotBeNull();
}
[Fact, PositiveTest]
public void ShouldHaveFormattedText_WhenFormatting_GivenInstanceWithValue123()
{
Given.NeededValue = 123;
When(Formatting); // Creating can be left out, as it is assumed as our first action
Then.Text.Should().Be("Object said, 123");
}
[Fact, NegativeTest]
public void ShouldThrowFormatExcpetion_WhenFormatting_GivenUnescapedOpeningBrace()
{
Given.Text = "Hey {name,";
When(Defer(Formatting));
DeferredAction.Should().Throw<FormatException>("Oops");
}
[Fact, PositiveTest]
public void ShouldRepeatAction_WhenRepeatIncrementing()
{
When(Repeat(Incrementing, 10));
Then.Repetitions.Should().Be(10);
}
[Fact, PositiveTest]
public void ShouldAwaitAsynchronousFunction_WhenTestingAsyncMethod()
{
When(Await(Processing));
Then.DelayedValue.Should().Be(4567);
}
protected override Creating()
{
// can check if a dynamic value is defined through GivensDefined("NeededValue",...)
Then.Target = new TestObject(Given.NeededValue);
}
private void Formatting()
{
Then.Text = Then.Target.Format();
}
private void Incrementing()
{
Then.Repetitions++;
}
private async Task Processing()
{
await Task.Delay(1000);
Then.DelayedValue = 4567;
}
public sealed class Thens : IDisposable // optionally define as IDisposable to have automatic disposal after each test
{
public TestObject Target;
public string Text;
public int Repetitions;
public int DelayedValue;
public void Dispose()
{
// optionally define as IDisposable to have automatic disposal after each test
}
}
}
}
Make use of dependency injection and helper methods
public class MyTest : XUnitTestBase<MyTest.Thens>
{
[Fact]
public void ShouldHaveMockEngaged_WhenTakingAction()
{
When(TakingAction);
RequireMock<IDidSomething>().Verify(x => x.CallMe());
Require<IDidSomething>().Should().NotBeNull();
}
protected override void Creating()
{
Services.AddScoped<A>();
CreateMock<IDidSomething>(mock =>
{
if (TryGiven("ThrowException", out bool throwEx))
mock.Setup(x => x.CallMe()).Throws<InvalidOperationException>();
});
Then.Target = BuildTarget<B>();
}
}
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 is compatible. 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | 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.1
- Microsoft.CSharp (>= 4.7.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- Moq (>= 4.20.70)
- Newtonsoft.Json (>= 13.0.3)
- xunit (>= 2.7.0)
-
net6.0
- Microsoft.CSharp (>= 4.7.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- Moq (>= 4.20.70)
- Newtonsoft.Json (>= 13.0.3)
- xunit (>= 2.7.0)
-
net7.0
- Microsoft.CSharp (>= 4.7.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- Moq (>= 4.20.70)
- Newtonsoft.Json (>= 13.0.3)
- xunit (>= 2.7.0)
-
net8.0
- Microsoft.CSharp (>= 4.7.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- Moq (>= 4.20.70)
- Newtonsoft.Json (>= 13.0.3)
- xunit (>= 2.7.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.