Back to Nunit

NUnit3004

docs/articles/nunit-analyzers/NUnit3004.md

latest2.5 KB
Original Source

NUnit3004

Field should be Disposed in TearDown or OneTimeTearDown method

TopicValue
IdNUnit3004
SeverityInfo
EnabledTrue
CategorySuppressor
CodeTypesThatOwnDisposableFieldsShouldBeDisposableSuppressor

Description

Field/Property is Disposed in TearDown or OneTimeTearDown method

Motivation

The Roslyn analyzer fires CA1001 for classes that have IDisposable members, but itself is not IDisposable.

Many NUnit tests initialize fields in tests or a SetUp method and then Dispose them in the TearDown method.

How to fix violations

Ensure that all IDisposable fields have a Dispose call in the TearDown method.

<!-- start generated config severity -->

Configure severity

The rule has no severity, but can be disabled.

Via ruleset file

To disable the rule for a project, you need to add a ruleset file

xml
<?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:

xml
<PropertyGroup>
  <CodeAnalysisRuleSet>NUnit.Analyzers.Suppressions.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

For more info about rulesets see MSDN.

Via .editorconfig file

This is currently not working. Waiting for Roslyn

ini
# NUnit3004: Field should be Disposed in TearDown or OneTimeTearDown method
dotnet_diagnostic.NUnit3004.severity = none
<!-- end generated config severity -->