Back to Nunit

NUnit1032

docs/articles/nunit-analyzers/NUnit1032.md

latest5.2 KB
Original Source

NUnit1032

An IDisposable field/property should be Disposed in a TearDown method

TopicValue
IdNUnit1032
SeverityError
EnabledTrue
CategoryStructure
CodeDisposeFieldsAndPropertiesInTearDownAnalyzer

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:

xml
# 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.

ini
dotnet_diagnostic.NUnit1032.additional_dispose_methods = Quit, Exit

Specifying additional SetUp and TearDown 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 and (OneTime)TearDown methods. It is possible to specify those methods in the .editorconfig and have the analyzer treat them like SetUp and TearDown methods. There are four configurations for this:

  • dotnet_diagnostic.NUnit.additional_setup_methods
  • dotnet_diagnostic.NUnit.additional_teardown_methods
  • dotnet_diagnostic.NUnit.additional_one_time_setup_methods
  • dotnet_diagnostic.NUnit.additional_one_time_teardown_methods

The configuration accepts a list of method names - separated by either comma, semicolon, or space.

Note that this configuration is shared with the NUnit 3002 analyzer.

<!-- start generated config severity -->

Configure severity

Via ruleset file

Configure the severity per project, for more info see MSDN.

Via .editorconfig file

ini
# 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

csharp
#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.

csharp
#pragma warning disable NUnit1032 // An IDisposable field/property should be Disposed in a TearDown method

Via attribute [SuppressMessage]

csharp
[System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
    "NUnit1032:An IDisposable field/property should be Disposed in a TearDown method",
    Justification = "Reason...")]
<!-- end generated config severity -->