2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-06-07 15:41:34 +02:00
The rule targets test methods that lack an assertion and consist solely of an action and, optionally, a setup.
2020-06-30 12:48:07 +02:00
2023-06-06 14:22:53 +02:00
[source,csharp]
----
[TestMethod]
public void Add_SingleNumber_ReturnsSameNumber()
{
var stringCalculator = new StringCalculator();
var actual = stringCalculator.Add("0");
}
----
2021-02-02 15:02:10 +01:00
2023-06-07 15:41:34 +02:00
Such tests only verify that the system under test does not throw any exceptions without providing any guarantees regarding the code's behavior under test.
Those tests increase the coverage without enforcing anything on the covered code, resulting in a false sense of security.
2023-06-06 14:22:53 +02:00
The rule identifies a potential issue when no assertions are present in tests utilizing the following frameworks:
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`
2024-07-11 16:30:49 +02:00
* `Moq`
2021-09-02 13:39:52 +02:00
* `Shoudly`
2020-06-30 12:48:07 +02:00
2023-06-06 14:22:53 +02:00
By enforcing the presence of assertions, this rule aims to enhance the reliability and comprehensiveness of tests by ensuring that they provide meaningful validation of the expected behavior.
=== Exceptions
Test methods that include a call to a custom assertion method will not raise any issues.
2024-10-14 14:00:54 +02:00
// This is on purpose not spelled "How to fix it" because:
// - we cannot mix the generic section with framework-specific sections
// - we cannot use a subsection with that name.
=== How can you fix it?
2023-06-06 14:22:53 +02:00
To address this issue, you should include assertions to validate the expected behavior.
2024-07-11 16:30:49 +02:00
Choose an appropriate assertion method provided by your testing framework (such as MSTest, NUnit, xUnit) or select a suitable assertion library like FluentAssertions, NFluent, NSubstitute, Moq, or Shouldly.
2023-06-06 14:22:53 +02:00
In addition to using built-in assertion methods, you also have the option to create custom assertion methods. To do this, declare an attribute named `[AssertionMethodAttribute]` and apply it to the respective method. This allows you to encapsulate specific validation logic within your custom assertion methods without raising the issue.
Here's an example:
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:48:07 +02:00
----
2023-06-06 14:22:53 +02:00
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class CustomTestExample
{
[TestMethod]
public void Add_SingleNumber_ReturnsSameNumber()
{
var stringCalculator = new StringCalculator();
var actual = stringCalculator.Add("0");
Validator.AssertCustomEquality(0, actual); // Compliant
}
}
public static class Validator
{
[AssertionMethod]
public static void AssertCustomEquality(int expected, int actual)
{
2024-07-11 16:30:49 +02:00
// ...
2023-06-06 14:22:53 +02:00
}
}
public class AssertionMethodAttribute : Attribute { }
----
2024-01-26 15:46:17 +01:00
== How to fix it in MSTest
2023-06-06 14:22:53 +02:00
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
----
2020-06-30 12:48:07 +02:00
[TestMethod]
2023-06-06 14:22:53 +02:00
public void Add_SingleNumber_ReturnsSameNumber()
2020-06-30 12:48:07 +02:00
{
2023-06-06 14:22:53 +02:00
var stringCalculator = new StringCalculator();
var actual = stringCalculator.Add("0");
2020-06-30 12:48:07 +02:00
}
----
2023-06-06 14:22:53 +02:00
==== Compliant solution
2020-06-30 12:48:07 +02:00
2023-06-06 14:22:53 +02:00
[source,csharp,diff-id=1,diff-type=compliant]
2020-06-30 12:48:07 +02:00
----
[TestMethod]
2023-06-06 14:22:53 +02:00
public void Add_SingleNumber_ReturnsSameNumber()
2020-06-30 12:48:07 +02:00
{
2023-06-06 14:22:53 +02:00
var stringCalculator = new StringCalculator();
var actual = stringCalculator.Add("0");
Assert.AreEqual(0, actual);
2020-06-30 12:48:07 +02:00
}
----
2020-12-21 15:38:52 +01:00
2024-01-26 15:46:17 +01:00
== How to fix it in NUnit
2020-12-21 15:38:52 +01:00
2023-06-06 14:22:53 +02:00
=== Code examples
2020-12-21 15:38:52 +01:00
2023-06-06 14:22:53 +02:00
==== Noncompliant code example
2021-02-02 15:02:10 +01:00
2023-06-06 14:22:53 +02:00
[source,csharp,diff-id=2,diff-type=noncompliant]
----
[Test]
public void Add_SingleNumber_ReturnsSameNumber()
{
var stringCalculator = new StringCalculator();
var actual = stringCalculator.Add("0");
}
2020-12-21 15:38:52 +01:00
----
2023-06-06 14:22:53 +02:00
==== Compliant solution
[source,csharp,diff-id=2,diff-type=compliant]
----
[Test]
public void Add_SingleNumber_ReturnsSameNumber()
2020-12-21 15:38:52 +01:00
{
2023-06-06 14:22:53 +02:00
var stringCalculator = new StringCalculator();
var actual = stringCalculator.Add("0");
Assert.That(0, Is.EqualTo(actual));
2020-12-21 15:38:52 +01:00
}
2023-06-06 14:22:53 +02:00
----
2020-12-21 15:38:52 +01:00
2024-01-26 15:46:17 +01:00
== How to fix it in xUnit
2023-06-06 14:22:53 +02:00
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=3,diff-type=noncompliant]
----
[Fact]
public void Add_SingleNumber_ReturnsSameNumber()
2020-12-21 15:38:52 +01:00
{
2023-06-06 14:22:53 +02:00
var stringCalculator = new StringCalculator();
var actual = stringCalculator.Add("0");
2020-12-21 15:38:52 +01:00
}
2023-06-06 14:22:53 +02:00
----
2020-12-21 15:38:52 +01:00
2023-06-06 14:22:53 +02:00
==== Compliant solution
[source,csharp,diff-id=3,diff-type=compliant]
2020-12-21 15:38:52 +01:00
----
2023-06-06 14:22:53 +02:00
[Fact]
public void Add_SingleNumber_ReturnsSameNumber()
{
var stringCalculator = new StringCalculator();
var actual = stringCalculator.Add("0");
Assert.Equal(0, actual);
}
----
== Resources
=== Documentation
* https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-with-mstest[Unit testing C# with MSTest]
* https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-with-nunit[Unit testing C# with NUnit]
* https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-with-dotnet-test[Unit testing C# with xUnit]
=== Articles & blog posts
* https://www.everydayunittesting.com/2017/03/unit-testing-anti-pattern-not-asserting.html[Unit Testing anti-pattern]
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)
2023-05-25 14:18:12 +02:00
=== on 2 May 2019, 14:44:07 Andrei Epure wrote:
This is implemented in https://github.com/SonarSource/sonar-dotnet/blob/7.13.0.8313/sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/TestMethodShouldContainAssertion.cs[TestMethodShouldContainAssertion ]
include::../comments-and-links.adoc[]
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]