Back to Nunit

NUnit1016

docs/articles/nunit-analyzers/NUnit1016.md

latest3.0 KB
Original Source

NUnit1016

The source type does not have a default constructor

TopicValue
IdNUnit1016
SeverityError
EnabledTrue
CategoryStructure
CodeTestCaseSourceUsesStringAnalyzer

Description

The source type must have a default constructor in order to provide test cases.

Motivation

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

How to fix violations

Example Violation

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

class DivideCases : IEnumerable
{
    public DivideCases(int i) { }

    public IEnumerator GetEnumerator()
    {
        yield return new object[] { 12, 3, 4 };
        yield return new object[] { 12, 2, 6 };
        yield return new object[] { 12, 4, 3 };
    }
}

Explanation

In the sample above, the class DivideCases does not have a default constructor - i.e. a constructor with no parameters.

However, source types specified by TestCaseSource must have a default constructor.

Fix

Add a default constructor to the source type (or remove the parameters from an existing constructor):

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

class DivideCases : IEnumerable
{
    public DivideCases() { }

    public DivideCases(int i) { }

    public IEnumerator GetEnumerator()
    {
        yield return new object[] { 12, 3, 4 };
        yield return new object[] { 12, 2, 6 };
        yield return 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
# NUnit1016: The source type does not have a default constructor
dotnet_diagnostic.NUnit1016.severity = chosenSeverity

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

Via #pragma directive

csharp
#pragma warning disable NUnit1016 // The source type does not have a default constructor
Code violating the rule here
#pragma warning restore NUnit1016 // The source type does not have a default constructor

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

csharp
#pragma warning disable NUnit1016 // The source type does not have a default constructor

Via attribute [SuppressMessage]

csharp
[System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
    "NUnit1016:The source type does not have a default constructor",
    Justification = "Reason...")]
<!-- end generated config severity -->