Back to Nunit

NUnit1034

docs/articles/nunit-analyzers/NUnit1034.md

latest2.7 KB
Original Source

NUnit1034

Base TestFixtures should be abstract

TopicValue
IdNUnit1034
SeverityWarning
EnabledTrue
CategoryStructure
CodeTestFixtureShouldBeAbstractAnalyzer

Description

Base TestFixtures should be abstract to prevent base class tests executing separately.

Motivation

When a base class is not abstract it will also be run as a standalone test which is most times not the intention.

csharp
namespace Tests
{
    internal class ParentFixture
    {
        [Test]
        public void ParentTest()
        {
            Assert.Pass($"Run {nameof(ParentTest)} from class {GetType().Name}");
        }
    }

    internal class ChildFixture : ParentFixture
    {
        [Test]
        public void ChildTest()
        {
            Assert.Pass($"Run {nameof(ChildTest)} from class {GetType().Name}");
        }
    }
}

As the Parent class is valid as a standalone TestFixture it will be instantiated and run separately.

text
ChildTest: Run ChildTest from class ChildFixture
ParentTest: Run ParentTest from class ChildFixture
ParentTest: Run ParentTest from class ParentFixture

This rule only fires when a class is found to be used as a base class in the current compilation.

How to fix violations

Mark any base class test fixture as abstract:

csharp
    internal abstract class Parent
    {
        [Test]
        public void ParentRun()
        {
            Assert.Pass($"Run {nameof(ParentRun)} from {GetType().Name}");
        }
    }
<!-- start generated config severity -->

Configure severity

Via ruleset file

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

Via .editorconfig file

ini
# NUnit1034: Base TestFixtures should be abstract
dotnet_diagnostic.NUnit1034.severity = chosenSeverity

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

Via #pragma directive

csharp
#pragma warning disable NUnit1034 // Base TestFixtures should be abstract
Code violating the rule here
#pragma warning restore NUnit1034 // Base TestFixtures should be abstract

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

csharp
#pragma warning disable NUnit1034 // Base TestFixtures should be abstract

Via attribute [SuppressMessage]

csharp
[System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
    "NUnit1034:Base TestFixtures should be abstract",
    Justification = "Reason...")]
<!-- end generated config severity -->