2020-06-30 12:48:07 +02:00
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.
2021-02-02 15:02:10 +01:00
2020-06-30 12:48:07 +02:00
This rule raises an exception when no assertions from any of the following frameworks are found in a test:
2020-06-30 14:49:38 +02:00
2021-09-02 13:39:52 +02:00
* `MSTest`
* `NUnit`
* `xUnit`
* `FluentAssertions` (4.x and 5.x)
* `NFluent`
* `NSubstitute`
* `Shoudly`
2020-06-30 12:48:07 +02:00
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:48:07 +02:00
----
[TestMethod]
public void MyMethod_WhenSomething_ExpectsSomething()
{
var myClass = new Class();
var result = myClass.GetFoo();
}
----
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:48:07 +02:00
----
[TestMethod]
public void MyMethod_WhenSomething_ExpectsSomething()
{
var myClass = new Class();
var result = myClass.GetFoo();
Assert.IsTrue(result);
}
----
2020-12-21 15:38:52 +01:00
== Exceptions
2021-01-27 13:42:22 +01:00
To create a custom assertion method declare an attribute with name ``++AssertionMethodAttribute++`` and mark the method with it:
2020-12-21 15:38:52 +01:00
2021-02-02 15:02:10 +01:00
2020-12-21 15:38:52 +01:00
----
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 { }
----
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]