NUnit3004
Field should be Disposed in TearDown or OneTimeTearDown method
| Topic | Value |
|---|---|
| Id | NUnit3004 |
| Severity | Info |
| Enabled | True |
| Category | Suppressor |
| Code | TypesThatOwnDisposableFieldsShouldBeDisposableSuppressor |
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.
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 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.
Via .editorconfig file
This is currently not working. Waiting for Roslyn
# NUnit3004: Field should be Disposed in TearDown or OneTimeTearDown method
dotnet_diagnostic.NUnit3004.severity = none