From 5dd57aad5f5d011a18ac0ee93643271d5eae9b45 Mon Sep 17 00:00:00 2001 From: Manfred Brands Date: Fri, 7 Jun 2024 12:36:30 +0800 Subject: [PATCH 1/4] NUnit1032: Explain how to dispose fields when using InstancePerTestCase --- documentation/NUnit1032.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/documentation/NUnit1032.md b/documentation/NUnit1032.md index 3d5db4d3..ec0a225c 100644 --- a/documentation/NUnit1032.md +++ b/documentation/NUnit1032.md @@ -14,16 +14,48 @@ 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 propererties, 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: + +* [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 +``` + ## Configure severity From 01a70dd7e9caa9e3884df907b6809be72e58f72d Mon Sep 17 00:00:00 2001 From: Manfred Brands Date: Fri, 7 Jun 2024 15:56:18 +0800 Subject: [PATCH 2/4] Fix linting and spell check errors --- documentation/NUnit1032.md | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/documentation/NUnit1032.md b/documentation/NUnit1032.md index ec0a225c..dca071df 100644 --- a/documentation/NUnit1032.md +++ b/documentation/NUnit1032.md @@ -14,9 +14,11 @@ 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. +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. +If you are using `LifeCycle.InstancePerTestCase` you should dispose the fields/properties +in the `Dispose` method of the test class. ## Motivation @@ -32,7 +34,7 @@ must be disposed in `OneTimeTearDown`. ### LifeCycle.InstancePerTestCase -If you have `IDisposable` fields or propererties, your class must implement the +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 @@ -42,8 +44,10 @@ The NUnit.Analyzer will not help you here as the functionality is available in a These are the rules that will help you with this: -* [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 +* [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) From 40ce030de93842059b29f6c916ed76236ee5bfc6 Mon Sep 17 00:00:00 2001 From: Manfred Brands Date: Sun, 9 Jun 2024 12:04:15 +0800 Subject: [PATCH 3/4] Update documentation/NUnit1032.md Co-authored-by: Mikkel Nylander Bundgaard --- documentation/NUnit1032.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/documentation/NUnit1032.md b/documentation/NUnit1032.md index dca071df..f494729a 100644 --- a/documentation/NUnit1032.md +++ b/documentation/NUnit1032.md @@ -40,9 +40,7 @@ If you have `IDisposable` fields or properties, your class must implement the 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: +The NUnit.Analyzer will not help you here as the functionality is available in [Microsoft .NET Analyzers](https://www.nuget.org/packages/Microsoft.CodeAnalysis.NetAnalyzers). These are the rules that will help you with this: * [CA1001](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1001) Types that own disposable fields should be disposable From decd7121ba0cab24eb0a2dd0c859dbd536ebf561 Mon Sep 17 00:00:00 2001 From: Manfred Brands Date: Sun, 9 Jun 2024 12:08:17 +0800 Subject: [PATCH 4/4] Wrapped line --- documentation/NUnit1032.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/documentation/NUnit1032.md b/documentation/NUnit1032.md index f494729a..ea9bb6f1 100644 --- a/documentation/NUnit1032.md +++ b/documentation/NUnit1032.md @@ -40,7 +40,9 @@ If you have `IDisposable` fields or properties, your class must implement the 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 [Microsoft .NET Analyzers](https://www.nuget.org/packages/Microsoft.CodeAnalysis.NetAnalyzers). These are the rules that will help you with this: +The NUnit.Analyzer will not help you here as the functionality is available in +[Microsoft .NET Analyzers](https://www.nuget.org/packages/Microsoft.CodeAnalysis.NetAnalyzers). +These are the rules that will help you with this: * [CA1001](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1001) Types that own disposable fields should be disposable