Why is this an issue?
A test case without assertions ensures only that no exceptions are thrown. Beyond basic runnability, it ensures nothing about the behavior of the
code under test.
This rule raises an exception when no assertions from any of the following frameworks are found in a test:
-
MSTest
-
NUnit
-
xUnit
-
FluentAssertions
(4.x and 5.x)
-
NFluent
-
NSubstitute
-
Shoudly
Noncompliant code example
[TestMethod]
public void MyMethod_WhenSomething_ExpectsSomething()
{
var myClass = new Class();
var result = myClass.GetFoo();
}
Compliant solution
[TestMethod]
public void MyMethod_WhenSomething_ExpectsSomething()
{
var myClass = new Class();
var result = myClass.GetFoo();
Assert.IsTrue(result);
}
Exceptions
To create a custom assertion method declare an attribute with name AssertionMethodAttribute
and mark the method with it:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class CustomTest
{
[TestMethod]
public void TestMethod1() => Validator.CustomMethod(42); // Compliant
}
public static class Validator
{
[AssertionMethod]
public static void CustomMethod(int value) { }
}
public class AssertionMethodAttribute : Attribute { }