Xunit.SerializedTheoryData 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Xunit.SerializedTheoryData --version 1.0.1                
NuGet\Install-Package Xunit.SerializedTheoryData -Version 1.0.1                
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.
<PackageReference Include="Xunit.SerializedTheoryData" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Xunit.SerializedTheoryData --version 1.0.1                
#r "nuget: Xunit.SerializedTheoryData, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Xunit.SerializedTheoryData as a Cake Addin
#addin nuget:?package=Xunit.SerializedTheoryData&version=1.0.1

// Install Xunit.SerializedTheoryData as a Cake Tool
#tool nuget:?package=Xunit.SerializedTheoryData&version=1.0.1                

Xunit.SerializedTheoryData

This project provides a simple replacement for TheoryData that automatically serializes and deserializes in order to show more fidelity in test cases. Normally when using custom classes (POCOs) as parameters with Xunit theories Visual Studio will show multiple test cases in one item in the Test Explorer, unless those classes implement IXunitSeralizable. This simple wrapper automatically does that for you, and as long as the classes are serializable and deserializable with Json.NET, Visual Studio Test Explorer will break out individual test cases.

Currently only classes, and lists are supported.

Build status NuGet package

Installation

This project is available as a NuGet package

Example

Suppose we have this simple class in a project somewhere:

public class Digit
{
	public int Value { get; set; }
}

This class could be in a class library where taking a dependency on Xunit and implementing IXunitSerializable is inconvenient, or we might just not want to bother for such a simple class.

Right now if we write the following simple Theory, Visual Studio displays the tests as shown:

[Theory]
[MemberData("GetTestCases")]
public void CheckPositive(Digit d)
{
	Assert.True(d.Value > 0);
}

public static TheoryData<Digit> GetTestCases()
{
	return new TheoryData<Digit>
	{
		{ new Digit() { Value = 1 } },
		{ new Digit() { Value = 2 } },
		{ new Digit() { Value = 3 } },
		{ new Digit() { Value = 4 } },
		{ new Digit() { Value = 5 } }
	};
}

Before

All of the test cases ran, but only one is shown in the Test Explorer. By simply changing TheoryData to SerializedTheoryData, and changing our test to take a SerializedWrapper<Digit> our code and Visual Studio now looks like this:

[Theory]
[MemberData("GetTestCases")]
public void CheckPositive(SerializedWrapper<Digit> d)
{
	Assert.True(d.Object.Value > 0);
}

public static SerializedTheoryData<Digit> GetTestCases()
{
	return new SerializedTheoryData<Digit>
	{
		{ new Digit() { Value = 1 } },
		{ new Digit() { Value = 2 } },
		{ new Digit() { Value = 3 } },
		{ new Digit() { Value = 4 } },
		{ new Digit() { Value = 5 } }
	};
}

After

Changing the parameter is not ideal but unfortunately things like implicit casts don't work when Xunit invokes the test method. The advantage is still there, in that the Digit class remains unchanged and is not muddied with test-supporting code. We also didn't have to change our test case generation code as the wrapping is automatic.

Lists

As an extra helper if our test parameter is a List<T> then there is no need to change the parameter specification at all, so for the following example, multiple test cases are displayed as is, simply by using SerializedTheoryData instead of TheoryData.

[Theory]
[MemberData("GetTestCases")]
public void CheckAllPositive(List<Digit> digits)
{
	Assert.True(digits.TrueForAll(d => d.Value > 0));
}

public static SerializedTheoryData<List<Digit>> GetTestCases()
{
	return new SerializedTheoryData<List<Digit>>
	{
		new List<Digit>
		{
			{ new Digit() { Value = 1 } },
			{ new Digit() { Value = 2 } }
		},
		new List<Digit>
		{
			{ new Digit() { Value = 3 } },
			{ new Digit() { Value = 4 } }
		},
		new List<Digit>
		{
			{ new Digit() { Value = 5 } },
			{ new Digit() { Value = 6 } }
		}
	};
}

Lists and normal class parameters can be intermingled as required.

Product Compatible and additional computed target framework versions.
.NET Framework net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
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.0.4 4,471 9/1/2021
1.0.3 508 9/1/2021
1.0.2 6,992 7/3/2018
1.0.1 1,120 8/10/2017
1.0.0 940 8/9/2017