A C# source generator for quick creation of simple unit tests.
$ dotnet add package Sungaila.InlineTestA C# source generator for quick creation of simple unit tests. Just add these attributes to your method:
[AreEqual][AreNotEqual][IsTrue][IsFalse][IsNull][IsNotNull][IsInstanceOfType<T>][IsNotInstanceOfType<T>][ThrowsException<T>][AreEqual(6, 3, Expected = 2)]
[AreEqual(1, 1, Expected = 1)]
[AreNotEqual(10, 1, NotExpected = 42)]
[ThrowsException<ArgumentOutOfRangeException>(1, 0)]
public static int Divide(int dividend, int divisor)
{
if (divisor == 0)
throw new ArgumentOutOfRangeException(nameof(divisor));
return dividend / divisor;
}
The source generator will produce classes containing the matching unit tests.
// shortened code for readability
[GeneratedCode("Sungaila.InlineTest", "1.0.0+17ac90a4b0b471c88edc5fcedee4124a7cbbac28")]
[TestClass]
public partial class ReadmeExampleTests
{
[TestMethod]
[DataRow(6, 3, 2)]
[DataRow(1, 1, 1)]
public void DivideAreEqual(int dividend, int divisor, int expected)
{
var result = ReadmeExample.Divide(dividend, divisor);
Assert.AreEqual(expected, result);
}
[TestMethod]
[DataRow(10, 1, 42)]
public void DivideAreNotEqual(int dividend, int divisor, int notExpected)
{
var result = ReadmeExample.Divide(dividend, divisor);
Assert.AreNotEqual(notExpected, result);
}
[TestMethod]
[DataRow(1, 0)]
public void DivideThrowsException_ArgumentOutOfRangeException(int dividend, int divisor)
{
Assert.ThrowsException<ArgumentOutOfRangeException>(
() => ReadmeExample.Divide(dividend, divisor));
}
}
System.Type (defined at compile-time)class or struct.static.