A 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));
}
}
- The method must be defined inside a
class
orstruct
.- These must be
public
orinternal
- For classes without a parameterless constructor the method must be static
- These must be
- The method must follow the rules of a test method (MSTest).
- must be
public
- cannot be
async void
- cannot use generics
- must be
- The method must not have more than 15 parameters.
- The same Attribute rules apply here. Your parameters have to be either
- a constant value
- a
System.Type
(defined at compile-time) - a single-dimensional array of the above