Back to Nunit

NUnit1017

docs/articles/nunit-analyzers/NUnit1017.md

latest2.5 KB
Original Source

NUnit1017

The specified source is not static

TopicValue
IdNUnit1017
SeverityError
EnabledTrue
CategoryStructure
CodeTestCaseSourceUsesStringAnalyzer

Description

The specified source must be static.

Motivation

To prevent tests that will fail at runtime due to improper construction.

How to fix violations

Example Violation

csharp
public class MyTestClass
{
    [TestCaseSource(nameof(DivideCases))]
    public void DivideTest(int n, int d, int q)
    {
        ClassicAssert.AreEqual(q, n / d);
    }

    object[] DivideCases =
    {
        new object[] { 12, 3, 4 },
        new object[] { 12, 2, 6 },
        new object[] { 12, 4, 3 }
    };
}

Explanation

In the sample above, DivideCases is not a static field.

However, sources specified by TestCaseSource must be static.

Fix

Make the source static:

csharp
public class MyTestClass
{
    [TestCaseSource(nameof(DivideCases))]
    public void DivideTest(int n, int d, int q)
    {
        ClassicAssert.AreEqual(q, n / d);
    }

    static object[] DivideCases =
    {
        new object[] { 12, 3, 4 },
        new object[] { 12, 2, 6 },
        new object[] { 12, 4, 3 }
    };
}
<!-- start generated config severity -->

Configure severity

Via ruleset file

Configure the severity per project, for more info see MSDN.

Via .editorconfig file

ini
# NUnit1017: The specified source is not static
dotnet_diagnostic.NUnit1017.severity = chosenSeverity

where chosenSeverity can be one of none, silent, suggestion, warning, or error.

Via #pragma directive

csharp
#pragma warning disable NUnit1017 // The specified source is not static
Code violating the rule here
#pragma warning restore NUnit1017 // The specified source is not static

Or put this at the top of the file to disable all instances.

csharp
#pragma warning disable NUnit1017 // The specified source is not static

Via attribute [SuppressMessage]

csharp
[System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
    "NUnit1017:The specified source is not static",
    Justification = "Reason...")]
<!-- end generated config severity -->