rspec/rules/S2187/csharp/rule.adoc

71 lines
3.2 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2023-06-06 16:24:33 +02:00
To ensure proper testing, it is important to include test cases in a test class. If a test class does not have any test cases, it can give the wrong impression that the class being tested has been thoroughly tested, when in reality, it has not.
2020-06-30 12:48:07 +02:00
2023-06-06 16:24:33 +02:00
This rule will raise an issue when any of these conditions are met:
2021-02-02 15:02:10 +01:00
2023-06-06 16:24:33 +02:00
* For `NUnit`, a class is marked with `TestFixture` but does not contain any method marked with `Test`, `TestCase`, `TestCaseSource`, or `Theory`.
* For `MSTest`, a class is marked with `TestClass` but does not contain any method marked with `TestMethod` or `DataTestMethod`.
2021-02-02 15:02:10 +01:00
2023-06-06 16:24:33 +02:00
It does not apply to `xUnit` since `xUnit` does not require a https://xunit.net/docs/comparisons#attributes[test class attribute].
2023-06-06 16:24:33 +02:00
=== Exceptions
2020-06-30 12:48:07 +02:00
2023-06-06 16:24:33 +02:00
There are scenarios where not having any test cases within a test class is perfectly acceptable and not seen as a problem.
2020-06-30 12:48:07 +02:00
2023-06-06 16:24:33 +02:00
==== `abstract` classes
2020-06-30 12:48:07 +02:00
2023-06-06 16:24:33 +02:00
To facilitate the creation of common test cases, test logic, or test infrastructure, it is advisable to use a base class.
2020-06-30 12:48:07 +02:00
2023-06-06 16:24:33 +02:00
Additionally, in both `NUnit` and `MSTest`, abstract classes that are annotated with their respective attributes (`TestFixture` in NUnit and `TestClass` in MSTest) are automatically ignored.
2020-06-30 12:48:07 +02:00
2023-06-06 16:24:33 +02:00
Therefore, there is no need to raise an issue in this particular scenario.
2020-06-30 12:48:07 +02:00
2023-06-07 15:41:34 +02:00
More information here:
2020-06-30 12:48:07 +02:00
2023-06-06 16:24:33 +02:00
* https://docs.nunit.org/articles/nunit/writing-tests/attributes/testfixture.html[`TestFixture` documentation in `NUnit`]
* https://github.com/microsoft/testfx/blob/0f19160cc319338ef6e23acb320da1562b40decd/src/Adapter/MSTest.TestAdapter/Discovery/TypeValidator.cs#L86-L97[`TypeValidator` class in `MSTest` (GitHub)]
==== Derived classes that inherit test cases from a base class
2023-06-07 15:41:34 +02:00
A base class containing one or more test cases to provide generic test cases is also considered a compliant scenario.
2023-06-06 16:24:33 +02:00
==== Classes that contain `AssemblyInitialize` or `AssemblyCleanup` methods
2023-06-07 15:41:34 +02:00
*This particular exception scenario only applies to the MSTest test framework.*
2023-06-06 16:24:33 +02:00
The `AssemblyInitialize` and `AssemblyCleanup` attributes are used to annotate methods that are executed only once at the beginning and at the end of a test run. These attributes can only be applied once per assembly.
It is logical to have a dedicated class for these methods, and this scenario is also considered compliant.
Furthermore, it is important to note that the test engine will execute a method annotated with either the `AssemblyInitialize` or `AssemblyCleanup` attribute only if that method is part of a class annotated with the `TestClass` attribute.
2023-06-07 15:41:34 +02:00
More information here:
2023-06-06 16:24:33 +02:00
* https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2008/ms245278(v=vs.90)[`AssemblyInitialize` attribute]
* https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2008/ms245265(v=vs.90)[`AssemblyCleanup` attribute]
include::how-mstest.adoc[]
include::how-nunit.adoc[]
2020-06-30 12:48:07 +02:00
2023-06-06 16:24:33 +02:00
include::resources.adoc[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
=== on 9 Aug 2017, 12:11:08 Amaury Levé wrote:
Please review
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]