docs/articles/nunit-analyzers/NUnit3002.md
| Topic | Value |
|---|---|
| Id | NUnit3002 |
| Severity | Info |
| Enabled | True |
| Category | Suppressor |
| Code | NonNullableFieldOrPropertyIsUninitializedSuppressor |
This rule check diagnostics reported by the CS8618 compiler error:
CS8618: Non-nullable field '_name_' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.CS8618: Non-nullable property '_Name_' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.If the violating field/property is set in the SetUp or OneTimeSetUp method. The rule suppresses the error.
This allows for non-nullable fields/properties to be used in a TestFixture.
The rule does detect indirect calls, when the field is set in a method called by the SetUp or OneTimeSetUp methods.
[TestFixture]
internal sealed class SomeClassFixture
{
private SomeClass instance;
[SetUp]
public void Setup()
{
instance = new SomeClass();
}
[Test]
public void Test()
{
Assert.That(instance.MethodUnderTest(), Is.True)
}
}
In the above fixture the compiler would give a warning because instance is not set in the constructor.
The suggestion to mark instance as nullable would mean that we have to test for null in all Test methods
or use the null suppression operator (!) everywhere.
Initialize the field in the SetUp or OneTimeSetUp methods.
The analyzer can only analyze the current class and doesn't know if an overridden method
is called from the base class' (OneTime)SetUp method.
It is possible to specify those methods in the .editorconfig
and have the analyzer treat them like a SetUp method.
There are two configurations for this:
dotnet_diagnostic.NUnit.additional_setup_methodsdotnet_diagnostic.NUnit.additional_one_time_setup_methodsThe configuration accepts a list of method names - separated by either comma, semicolon, or space.
Note that this configuration is shared with the NUnit 1032 analyzer.
<!-- start generated config severity -->The rule has no severity, but can be disabled.
To disable the rule for a project, you need to add a ruleset file
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="NUnit.Analyzer Suppressions" Description="DiagnosticSuppression Rules" ToolsVersion="12.0">
<Rules AnalyzerId="DiagnosticSuppressors" RuleNamespace="NUnit.NUnitAnalyzers">
<Rule Id="NUnit3001" Action="Info" /> <!-- Possible Null Reference -->
<Rule Id="NUnit3002" Action="Info" /> <!-- NonNullableField/Property is Uninitialized -->
<Rule Id="NUnit3003" Action="Info" /> <!-- Avoid Uninstantiated Internal Classes -->
<Rule Id="NUnit3004" Action="Info" /> <!-- Types that own disposable fields should be disposable -->
</Rules>
</RuleSet>
and add it to the project like:
<PropertyGroup>
<CodeAnalysisRuleSet>NUnit.Analyzers.Suppressions.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
For more info about rulesets see MSDN.
This is currently not working. Waiting for Roslyn
# NUnit3002: Field/Property is initialized in SetUp or OneTimeSetUp method
dotnet_diagnostic.NUnit3002.severity = none