Search Results for

    Show / Hide Table of Contents

    NUnit1020

    The TestCaseSource provides parameters to a source - field or property - that expects no parameters

    Topic Value
    Id NUnit1020
    Severity Error
    Enabled True
    Category Structure
    Code TestCaseSourceUsesStringAnalyzer

    Description

    The TestCaseSource must not provide any parameters when the source is a field or a property.

    Motivation

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

    How to fix violations

    Example Violation

    public class MyTestClass
    {
        [TestCaseSource(nameof(DivideCases), new object[] { "Testing" })]
        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 }
        };
    }
    

    Explanation

    In the sample above, DivideCases is a field, and as such does not accept any arguments, so the TestCaseSource should not supply any parameters.

    Fix

    Either remove the parameter from TestCaseSource or change the field into a method.:

    public class MyTestClass
    {
        [TestCaseSource(nameof(DivideCases), new object[] { "Testing" })]
        public void DivideTest(int n, int d, int q)
        {
            ClassicAssert.AreEqual(q, n / d);
        }
    
        static object[] DivideCases(string input)
        {
            return new object[]
            {
                new object[] { 12, 3, 4 },
                new object[] { 12, 2, 6 },
                new object[] { 12, 4, 3 }
            };
        }
    }
    

    Configure severity

    Via ruleset file

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

    Via .editorconfig file

    # NUnit1020: The TestCaseSource provides parameters to a source - field or property - that expects no parameters
    dotnet_diagnostic.NUnit1020.severity = chosenSeverity
    

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

    Via #pragma directive

    #pragma warning disable NUnit1020 // The TestCaseSource provides parameters to a source - field or property - that expects no parameters
    Code violating the rule here
    #pragma warning restore NUnit1020 // The TestCaseSource provides parameters to a source - field or property - that expects no parameters
    

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

    #pragma warning disable NUnit1020 // The TestCaseSource provides parameters to a source - field or property - that expects no parameters
    

    Via attribute [SuppressMessage]

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
        "NUnit1020:The TestCaseSource provides parameters to a source - field or property - that expects no parameters",
        Justification = "Reason...")]
    
    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0