Search Results for

    Show / Hide Table of Contents

    NUnit1015

    The source type does not implement IEnumerable

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

    Description

    The source type must implement IEnumerable 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

    public class MyTestClass
    {
        [TestCaseSource(typeof(DivideCases))]
        public void DivideTest(int n, int d, int q)
        {
            Assert.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 };
        }
    }
    

    Explanation

    In the sample above, the class DivideCases does not implement IEnumerable.

    However, source types specified by TestCaseSource must implement IEnumerable.

    Fix

    Make the source type implement IEnumerable:

    public class MyTestClass
    {
        [TestCaseSource(typeof(DivideCases))]
        public void DivideTest(int n, int d, int q)
        {
            Assert.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 severity

    Via ruleset file

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

    Via .editorconfig file

    # NUnit1015: The source type does not implement IEnumerable
    dotnet_diagnostic.NUnit1015.severity = chosenSeverity
    

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

    Via #pragma directive

    #pragma warning disable NUnit1015 // The source type does not implement IEnumerable
    Code violating the rule here
    #pragma warning restore NUnit1015 // The source type does not implement IEnumerable
    

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

    #pragma warning disable NUnit1015 // The source type does not implement IEnumerable
    

    Via attribute [SuppressMessage]

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
        "NUnit1015:The source type does not implement IEnumerable",
        Justification = "Reason...")]
    
    • Improve this Doc
    In This Article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0