NUnit1032
An IDisposable field/property should be Disposed in a TearDown method
| Topic | Value |
|---|---|
| Id | NUnit1032 |
| Severity | Error |
| Enabled | True |
| Category | Structure |
| Code | DisposeFieldsAndPropertiesInTearDownAnalyzer |
Description
An IDisposable field/property should be Disposed in a TearDown method.
This analyzer rule only applies to a TestFixture which is using the default
NUnit SingleInstance life cycle where the class is instantiated once for all tests.
If you are using LifeCycle.InstancePerTestCase you should dispose the fields/properties
in the Dispose method of the test class.
Motivation
Not Disposing fields/properties can cause memory leaks or failing tests.
How to fix violations
LifeCycle.SingleInstance
Dispose any fields/properties that are initialized in SetUp or Test methods in a TearDown method.
Fields/Properties that are initialized in OneTimeSetUp, or with initializers or in constructors
must be disposed in OneTimeTearDown.
LifeCycle.InstancePerTestCase
If you have IDisposable fields or properties, your class must implement the
IDisposable interface.
Dispose any fields/properties that are initialized at declaration or in the constructor in the
Dispose method.
The NUnit.Analyzer will not help you here as the functionality is available in Microsoft .NET Analyzers. These are the rules that will help you with this:
- CA1001 Types that own disposable fields should be disposable
- CA2213 Disposable fields should be disposed
Unfortunately, those rules are not enabled by default, you can enable them in your project in a
.editorconfig
file using the following content:
# CA1001: Types that own disposable fields should be disposable
dotnet_diagnostic.CA1001.severity = warning
# CA2213: Disposable fields should be disposed
dotnet_diagnostic.CA2213.severity = warning
Extending the list of names of disposing methods
The analyzer considers the following list of method names to be disposing: Dispose, DisposeAsync, Close, and
CloseAsync. It is possible to add method names to this list by using the configuration
dotnet_diagnostic.NUnit1032.additional_dispose_methods in the .editorconfig. The configuration accepts a
list of method names - separated by either comma, semicolon, or space. I.e. to add Quit and Exit to the list add
the following line.
dotnet_diagnostic.NUnit1032.additional_dispose_methods = Quit, Exit
Configure severity
Via ruleset file
Configure the severity per project, for more info see MSDN.
Via .editorconfig file
# NUnit1032: An IDisposable field/property should be Disposed in a TearDown method
dotnet_diagnostic.NUnit1032.severity = chosenSeverity
where chosenSeverity can be one of none, silent, suggestion, warning, or error.
Via #pragma directive
#pragma warning disable NUnit1032 // An IDisposable field/property should be Disposed in a TearDown method
Code violating the rule here
#pragma warning restore NUnit1032 // An IDisposable field/property should be Disposed in a TearDown method
Or put this at the top of the file to disable all instances.
#pragma warning disable NUnit1032 // An IDisposable field/property should be Disposed in a TearDown method
Via attribute [SuppressMessage]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
"NUnit1032:An IDisposable field/property should be Disposed in a TearDown method",
Justification = "Reason...")]