Back to Nunit

Overview of implemented NUnit diagnostics and code fixes

docs/articles/nunit-analyzers/NUnit-Analyzers.md

latest17.4 KB
Original Source

Overview of implemented NUnit diagnostics and code fixes

In the tables below we use the following symbols:

  • :mag: - whether the diagnostic is enabled by default or not
  • :memo: - the severity of the diagnostic
  • :bulb: - whether the diagnostic has an associated code fix or not

The severity of a diagnostic can the one of the following (in increasing severity):

  • :thought_balloon: - indicates a severity of Hidden
  • :information_source: - indicates a severity of Info
  • :warning: - indicates a severity of Warning
  • :exclamation: - indicates a severity of Error

Structure Rules (NUnit1001 - )

Rules which enforce structural requirements on the test code.

IdTitle:mag::memo::bulb:
NUnit1001The individual arguments provided by a TestCaseAttribute must match the type of the corresponding parameter of the method:white_check_mark::exclamation::x:
NUnit1002The TestCaseSource should use nameof operator to specify target:white_check_mark::warning::white_check_mark:
NUnit1003The TestCaseAttribute provided too few arguments:white_check_mark::exclamation::x:
NUnit1004The TestCaseAttribute provided too many arguments:white_check_mark::exclamation::x:
NUnit1005The type of the value specified via ExpectedResult must match the return type of the method:white_check_mark::exclamation::x:
NUnit1006ExpectedResult must not be specified when the method returns void:white_check_mark::exclamation::x:
NUnit1007The method has non-void return type, but no result is expected in ExpectedResult:white_check_mark::exclamation::x:
NUnit1008Specifying ParallelScope.Self on assembly level has no effect:white_check_mark::warning::x:
NUnit1009One may not specify ParallelScope.Children on a non-parameterized test method:white_check_mark::exclamation::x:
NUnit1010One may not specify ParallelScope.Fixtures on a test method:white_check_mark::exclamation::x:
NUnit1011The TestCaseSource argument does not specify an existing member:white_check_mark::exclamation::x:
NUnit1012The async test method must have a non-void return type:white_check_mark::exclamation::x:
NUnit1013The async test method must have a non-generic Task return type when no result is expected:white_check_mark::exclamation::x:
NUnit1014The async test method must have a Task<T> return type when a result is expected:white_check_mark::exclamation::x:
NUnit1015The source type does not implement I(Async)Enumerable:white_check_mark::exclamation::x:
NUnit1016The source type does not have a default constructor:white_check_mark::exclamation::x:
NUnit1017The specified source is not static:white_check_mark::exclamation::x:
NUnit1018The number of parameters provided by the TestCaseSource does not match the number of parameters in the target method:white_check_mark::exclamation::x:
NUnit1019The source specified by the TestCaseSource does not return an I(Async)Enumerable or a type that implements I(Async)Enumerable:white_check_mark::exclamation::x:
NUnit1020The TestCaseSource provides parameters to a source - field or property - that expects no parameters:white_check_mark::exclamation::x:
NUnit1021The ValueSource should use nameof operator to specify target:white_check_mark::warning::white_check_mark:
NUnit1022The specified source is not static:white_check_mark::exclamation::x:
NUnit1023The target method expects parameters which cannot be supplied by the ValueSource:white_check_mark::exclamation::x:
NUnit1024The source specified by the ValueSource does not return an I(Async)Enumerable or a type that implements I(Async)Enumerable:white_check_mark::exclamation::x:
NUnit1025The ValueSource argument does not specify an existing member:white_check_mark::exclamation::x:
NUnit1026The test or setup/teardown method is not public:white_check_mark::exclamation::white_check_mark:
NUnit1027The test method has parameters, but no arguments are supplied by attributes:white_check_mark::exclamation::x:
NUnit1028The non-test method is public:white_check_mark::information_source::white_check_mark:
NUnit1029The number of parameters provided by the TestCaseSource does not match the number of parameters in the Test method:white_check_mark::exclamation::x:
NUnit1030The type of parameter provided by the TestCaseSource does not match the type of the parameter in the Test method:white_check_mark::exclamation::x:
NUnit1031The individual arguments provided by a ValuesAttribute must match the type of the corresponding parameter of the method:white_check_mark::exclamation::x:
NUnit1032An IDisposable field/property should be Disposed in a TearDown method:white_check_mark::exclamation::x:
NUnit1033The Write methods on TestContext will be marked as Obsolete and eventually removed:white_check_mark::warning::white_check_mark:
NUnit1034Base TestFixtures should be abstract:white_check_mark::warning::white_check_mark:
NUnit1035The 'step' parameter to Range cannot be zero:white_check_mark::exclamation::x:
NUnit1036The value for 'from' must be less than 'to' when 'step' is positive:white_check_mark::exclamation::x:
NUnit1037The value for 'from' must be greater than 'to' when 'step' is negative:white_check_mark::exclamation::x:
NUnit1038The type of the attribute values doesn't match the parameter type:white_check_mark::exclamation::x:

Assertion Rules (NUnit2001 - )

Rules which improve assertions in the test code.

IdTitle:mag::memo::bulb:
NUnit2001Consider using Assert.That(expr, Is.False) instead of ClassicAssert.False(expr):white_check_mark::information_source::white_check_mark:
NUnit2002Consider using Assert.That(expr, Is.False) instead of ClassicAssert.IsFalse(expr):white_check_mark::information_source::white_check_mark:
NUnit2003Consider using Assert.That(expr, Is.True) instead of ClassicAssert.IsTrue(expr):white_check_mark::information_source::white_check_mark:
NUnit2004Consider using Assert.That(expr, Is.True) instead of ClassicAssert.True(expr):white_check_mark::information_source::white_check_mark:
NUnit2005Consider using Assert.That(actual, Is.EqualTo(expected)) instead of ClassicAssert.AreEqual(expected, actual):white_check_mark::warning::white_check_mark:
NUnit2006Consider using Assert.That(actual, Is.Not.EqualTo(expected)) instead of ClassicAssert.AreNotEqual(expected, actual):white_check_mark::warning::white_check_mark:
NUnit2007The actual value should not be a constant:white_check_mark::warning::white_check_mark:
NUnit2008Incorrect IgnoreCase usage:white_check_mark::warning::x:
NUnit2009The same value has been provided as both the actual and the expected argument:white_check_mark::warning::x:
NUnit2010Use EqualConstraint for better assertion messages in case of failure:white_check_mark::information_source::white_check_mark:
NUnit2011Use ContainsConstraint for better assertion messages in case of failure:white_check_mark::information_source::white_check_mark:
NUnit2012Use StartsWithConstraint for better assertion messages in case of failure:white_check_mark::information_source::white_check_mark:
NUnit2013Use EndsWithConstraint for better assertion messages in case of failure:white_check_mark::information_source::white_check_mark:
NUnit2014Use SomeItemsConstraint for better assertion messages in case of failure:white_check_mark::information_source::white_check_mark:
NUnit2015Consider using Assert.That(actual, Is.SameAs(expected)) instead of ClassicAssert.AreSame(expected, actual):white_check_mark::warning::white_check_mark:
NUnit2016Consider using Assert.That(expr, Is.Null) instead of ClassicAssert.Null(expr):white_check_mark::information_source::white_check_mark:
NUnit2017Consider using Assert.That(expr, Is.Null) instead of ClassicAssert.IsNull(expr):white_check_mark::information_source::white_check_mark:
NUnit2018Consider using Assert.That(expr, Is.Not.Null) instead of ClassicAssert.NotNull(expr):white_check_mark::information_source::white_check_mark:
NUnit2019Consider using Assert.That(expr, Is.Not.Null) instead of ClassicAssert.IsNotNull(expr):white_check_mark::information_source::white_check_mark:
NUnit2020Incompatible types for SameAs constraint:white_check_mark::exclamation::x:
NUnit2021Incompatible types for EqualTo constraint:white_check_mark::exclamation::x:
NUnit2022Missing property required for constraint:white_check_mark::exclamation::white_check_mark:
NUnit2023Invalid NullConstraint usage:white_check_mark::exclamation::x:
NUnit2024Wrong actual type used with String Constraint:white_check_mark::exclamation::x:
NUnit2025Wrong actual type used with ContainsConstraint:x::thought_balloon::x:
NUnit2026Wrong actual type used with the SomeItemsConstraint with EqualConstraint:white_check_mark::exclamation::x:
NUnit2027Consider using Assert.That(actual, Is.GreaterThan(expected)) instead of ClassicAssert.Greater(actual, expected):white_check_mark::information_source::white_check_mark:
NUnit2028Consider using Assert.That(actual, Is.GreaterThanOrEqualTo(expected)) instead of ClassicAssert.GreaterOrEqual(actual, expected):white_check_mark::information_source::white_check_mark:
NUnit2029Consider using Assert.That(actual, Is.LessThan(expected)) instead of ClassicAssert.Less(actual, expected):white_check_mark::information_source::white_check_mark:
NUnit2030Consider using Assert.That(actual, Is.LessThanOrEqualTo(expected)) instead of ClassicAssert.LessOrEqual(actual, expected):white_check_mark::information_source::white_check_mark:
NUnit2031Consider using Assert.That(actual, Is.Not.SameAs(expected)) instead of ClassicAssert.AreNotSame(expected, actual):white_check_mark::warning::white_check_mark:
NUnit2032Consider using Assert.That(expr, Is.Zero) instead of ClassicAssert.Zero(expr):white_check_mark::information_source::white_check_mark:
NUnit2033Consider using Assert.That(expr, Is.Not.Zero) instead of ClassicAssert.NotZero(expr):white_check_mark::information_source::white_check_mark:
NUnit2034Consider using Assert.That(expr, Is.NaN) instead of ClassicAssert.IsNaN(expr):white_check_mark::information_source::white_check_mark:
NUnit2035Consider using Assert.That(collection, Is.Empty) instead of ClassicAssert.IsEmpty(collection):white_check_mark::information_source::white_check_mark:
NUnit2036Consider using Assert.That(collection, Is.Not.Empty) instead of ClassicAssert.IsNotEmpty(collection):white_check_mark::information_source::white_check_mark:
NUnit2037Consider using Assert.That(collection, Does.Contain(instance)) instead of ClassicAssert.Contains(instance, collection):white_check_mark::information_source::white_check_mark:
NUnit2038Consider using Assert.That(actual, Is.InstanceOf(expected)) instead of ClassicAssert.IsInstanceOf(expected, actual):white_check_mark::information_source::white_check_mark:
NUnit2039Consider using Assert.That(actual, Is.Not.InstanceOf(expected)) instead of ClassicAssert.IsNotInstanceOf(expected, actual):white_check_mark::information_source::white_check_mark:
NUnit2040Non-reference types for SameAs constraint:white_check_mark::exclamation::white_check_mark:
NUnit2041Incompatible types for comparison constraint:white_check_mark::exclamation::x:
NUnit2042Comparison constraint on object:white_check_mark::information_source::x:
NUnit2043Use ComparisonConstraint for better assertion messages in case of failure:white_check_mark::information_source::white_check_mark:
NUnit2044Non-delegate actual parameter:white_check_mark::exclamation::white_check_mark:
NUnit2045Use Assert.EnterMultipleScope or Assert.Multiple:white_check_mark::information_source::white_check_mark:
NUnit2046Use CollectionConstraint for better assertion messages in case of failure:white_check_mark::information_source::white_check_mark:
NUnit2047Incompatible types for Within constraint:white_check_mark::warning::white_check_mark:
NUnit2048Consider using Assert.That(...) instead of StringAssert(...):white_check_mark::warning::white_check_mark:
NUnit2049Consider using Assert.That(...) instead of CollectionAssert(...):white_check_mark::warning::white_check_mark:
NUnit2050NUnit 4 no longer supports string.Format specification:white_check_mark::exclamation::white_check_mark:
NUnit2051Consider using Assert.That(expr, Is.Positive) instead of ClassicAssert.Positive(expr):white_check_mark::information_source::white_check_mark:
NUnit2052Consider using Assert.That(expr, Is.Negative) instead of ClassicAssert.Negative(expr):white_check_mark::information_source::white_check_mark:
NUnit2053Consider using Assert.That(actual, Is.AssignableFrom(expected)) instead of ClassicAssert.IsAssignableFrom(expected, actual):white_check_mark::information_source::white_check_mark:
NUnit2054Consider using Assert.That(actual, Is.Not.AssignableFrom(expected)) instead of ClassicAssert.IsNotAssignableFrom(expected, actual):white_check_mark::information_source::white_check_mark:
NUnit2055Consider using Is.InstanceOf<T> constraint instead of an 'is T' expression:white_check_mark::information_source::white_check_mark:
NUnit2056Consider using Assert.EnterMultipleScope statement instead of Assert.Multiple/Assert.MultipleAsync:white_check_mark::information_source::white_check_mark:
NUnit2057Remove unnecessary lambda expression:white_check_mark::information_source::white_check_mark:
NUnit2058The constraint is misused:white_check_mark::warning::white_check_mark:

Suppressor Rules (NUnit3001 - )

Rules which suppress compiler errors based on context. Note that these rules are only available in the .NET Standard 2.0 builds (version 3.0.0 and above) which require Visual Studio 2019 (version 16.3) or newer.

IdTitle:mag::memo::bulb:
NUnit3001Expression was checked in an ClassicAssert.NotNull, ClassicAssert.IsNotNull or Assert.That call:white_check_mark::information_source::x:
NUnit3002Field/Property is initialized in SetUp or OneTimeSetUp method:white_check_mark::information_source::x:
NUnit3003Class is an NUnit TestFixture and is instantiated using reflection:white_check_mark::information_source::x:
NUnit3004Field should be Disposed in TearDown or OneTimeTearDown method:white_check_mark::information_source::x:

Style Rules (NUnit4001 - )

Rules which help you write concise and readable NUnit test code.

IdTitle:mag::memo::bulb:
NUnit4001Simplify the Values attribute:white_check_mark::information_source::white_check_mark:
NUnit4002Use Specific constraint:white_check_mark::information_source::white_check_mark: