Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NUnit1032: Explain how to dispose fields when using InstancePerTestCase #757

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions documentation/NUnit1032.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,52 @@

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`](https://learn.microsoft.com/en-us/dotnet/api/system.idisposable?view=net-8.0) interface.

Dispose any fields/properties that are initialized at declaration or in the constructor in the
[`Dispose`](https://learn.microsoft.com/en-us/dotnet/api/system.idisposable.dispose?view=net-8.0) method.

The NUnit.Analyzer will not help you here as the functionality is available in a [Microsoft .NET Analyzers](https://www.nuget.org/packages/Microsoft.CodeAnalysis.NetAnalyzers)

These are the rules that will help you with this:
manfred-brands marked this conversation as resolved.
Show resolved Hide resolved

* [CA1001](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1001)
Types that own disposable fields should be disposable
* [CA2213](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2213)
Disposable fields should be disposed

Unfortunately, those rules are not enabled by default, you can enable them in your project in a
[`.editorconfig`](https://learn.microsoft.com/en-us/visualstudio/code-quality/use-roslyn-analyzers?view=vs-2022#manually-configure-rule-severity-in-an-editorconfig-file)
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
```

<!-- start generated config severity -->
## Configure severity

Expand Down