Skip to content

Instantly share code, notes, and snippets.

@jonesandy
Last active April 24, 2024 07:35
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jonesandy/f622874e78d9d9f356896c4ac63c0ac3 to your computer and use it in GitHub Desktop.
Save jonesandy/f622874e78d9d9f356896c4ac63c0ac3 to your computer and use it in GitHub Desktop.
A cheat sheet of Asserts for xUnit.net in C#
/*
STRINGS
*/
Assert.Equal(expectedString, actualString);
Assert.StartsWith(expectedString, stringToCheck);
Assert.EndsWith(expectedString, stringToCheck);
// Some can also take optional params
Assert.Equal(expectedString, actualString, ignoreCase: true);
Assert.StartsWith(expectedString, stringToCheck, StringComparison.OrdinalIgnoreCase);
/*
COLLECTIONS
*/
Assert.Contains(expectedThing, collection);
// Overload method for contains
Assert.Contains(collection, item => item.Contains(thingToCheck));
Assert.DoesNotContain(expectedThing, collection);
Assert.Empty(collection);
Assert.All(collection, item => Assert.False(string.IsNullOrWhiteSpace(item)));
/*
NUMBERS
*/
Assert.InRange(thingToCheck, lowRange, highRange);
/*
EXCEPTIONS
*/
Assert.Throws<T>(() => sut.Method());
/*
TYPES
*/
Assert.IsType<T>(thing);
Assert.IsAssignableFrom<T>(thing);
Assert.Same(obj1, obj2);
Assert.NotSame(obj1, obj2);
/*
COOL XUNIT STUFF
*/
// Inherit from the DataAttribute from xunit.sdk
public class CustomData : DataAttribute
{
// Needs a method that returns an IEnumerable<object[]>
public override IEnumerable<object[]> GetData(MethodInfo testMethod)
{
yield return new object[] { data1, data2, data3 };
yield return new object[] { data4, data5, data6 };
}
}
public class TestClass
{
[Theory]
[CustomData]
public void TestMethod(param1, param2, param3)
{
// Test using the 3 params
}
}
// Create the fixture to share
public class TestSetup : IDisposable
{
// Some stuff here
}
public class TestClass : IClassFixture<TestSetup>
{
public TestClass(TestSetup setup)
{
// Initialise setup to share across test methods
}
}
// Create collection to share across test classes
[CollectionDefinition("Name of Collection")]
public class TestCollection : ICollectionFixture<TestSetup>
{
}
[Collection("Name of Collection")]
public class TestClass2
{
public TestClass2(TestSetup setup)
{
// Initialise setup
}
}
@Dennis-N8
Copy link

Thank you,

can you please add strongly typed theory test data?

https://andrewlock.net/creating-strongly-typed-xunit-theory-test-data-with-theorydata/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment