docs/articles/nunit-analyzers/NUnit1015.md
| Topic | Value |
|---|---|
| Id | NUnit1015 |
| Severity | Error |
| Enabled | True |
| Category | Structure |
| Code | TestCaseSourceUsesStringAnalyzer |
The source type must implement I(Async)Enumerable in order to provide test cases.
To prevent tests that will fail at runtime due to improper construction.
public class MyTestClass
{
[TestCaseSource(typeof(DivideCases))]
public void DivideTest(int n, int d, int q)
{
ClassicAssert.AreEqual(q, n / d);
}
}
class DivideCases
{
public IEnumerator GetData()
{
yield return new object[] { 12, 3, 4 };
yield return new object[] { 12, 2, 6 };
yield return new object[] { 12, 4, 3 };
}
}
In the sample above, the class DivideCases does not implement IEnumerable nor IAsyncEnumerable
However, source types specified by TestCaseSource
must implement IEnumerable or IAsyncEnumerable.
Make the source type implement IEnumerable or IAsyncEnumerable
public class MyTestClass
{
[TestCaseSource(typeof(DivideCases))]
public void DivideTest(int n, int d, int q)
{
ClassicAssert.AreEqual(q, n / d);
}
}
class DivideCases : IEnumerable
{
public IEnumerator GetEnumerator()
{
yield return new object[] { 12, 3, 4 };
yield return new object[] { 12, 2, 6 };
yield return new object[] { 12, 4, 3 };
}
}
Configure the severity per project, for more info see MSDN.
# NUnit1015: The source type does not implement I(Async)Enumerable
dotnet_diagnostic.NUnit1015.severity = chosenSeverity
where chosenSeverity can be one of none, silent, suggestion, warning, or error.
#pragma warning disable NUnit1015 // The source type does not implement I(Async)Enumerable
Code violating the rule here
#pragma warning restore NUnit1015 // The source type does not implement I(Async)Enumerable
Or put this at the top of the file to disable all instances.
#pragma warning disable NUnit1015 // The source type does not implement I(Async)Enumerable
[SuppressMessage][System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
"NUnit1015:The source type does not implement I(Async)Enumerable",
Justification = "Reason...")]