GwtUnit.XUnit 1.3.0

.NET 6.0 .NET Standard 2.1
There is a newer version of this package available.
See the version list below for details.
NuGet\Install-Package GwtUnit.XUnit -Version 1.3.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
dotnet add package GwtUnit.XUnit --version 1.3.0
<PackageReference Include="GwtUnit.XUnit" Version="1.3.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add GwtUnit.XUnit --version 1.3.0
#r "nuget: GwtUnit.XUnit, 1.3.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install GwtUnit.XUnit as a Cake Addin
#addin nuget:?package=GwtUnit.XUnit&version=1.3.0

// Install GwtUnit.XUnit as a Cake Tool
#tool nuget:?package=GwtUnit.XUnit&version=1.3.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 suppport
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
			}
		}
	}
}
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.1
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

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.7.0 104 6/13/2022
1.6.0 74 6/4/2022
1.5.3 187 3/15/2022
1.5.2 76 3/15/2022
1.5.1 95 3/1/2022
1.5.0 79 3/1/2022
1.4.0 91 2/18/2022
1.3.0 83 2/18/2022
1.2.0 80 2/10/2022
1.1.0 73 2/9/2022
1.0.0 86 2/9/2022