Skip to content

Commit

Permalink
Setups are considered harmful
Browse files Browse the repository at this point in the history
  • Loading branch information
fakefeik committed Jun 15, 2024
1 parent 8f0980d commit 2826fd7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
1 change: 1 addition & 0 deletions NUnit.Analyzers/Constants/AnalyzerIdentifiers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
internal static class AnalyzerIdentifiers
{
internal const string TestFieldIsNotReadonly = "NU0001";
internal const string TestUsesSetupAttributes = "NU0002";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Collections.Immutable;
using System.Linq;

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;

using NUnit.Analyzers.Constants;
using NUnit.Analyzers.Extensions;

namespace NUnit.Analyzers.TestUsesSetupMethods
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class TestUsesSetupMethodsAnalyzer : DiagnosticAnalyzer
{
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterSymbolAction(AnalyzeMethod, SymbolKind.Method);
}

private static void AnalyzeMethod(SymbolAnalysisContext context)
{
var methodSymbol = (IMethodSymbol)context.Symbol;
if (IsSetUpTearDownMethod(context.Compilation, methodSymbol))
{
context.ReportDiagnostic(Diagnostic.Create(testUsesSetupMethods, methodSymbol.Locations[0]));
}
}

private static bool IsSetUpTearDownMethod(Compilation compilation, IMethodSymbol methodSymbol)
{
return methodSymbol.GetAttributes().Any(a => a.IsSetUpOrTearDownMethodAttribute(compilation));
}

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
ImmutableArray.Create(testUsesSetupMethods);

private static readonly DiagnosticDescriptor testUsesSetupMethods = new DiagnosticDescriptor(
AnalyzerIdentifiers.TestUsesSetupAttributes,

Check warning on line 40 in NUnit.Analyzers/TestUsesSetupMethods/TestUsesSetupMethodsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / test

Enable analyzer release tracking for the analyzer project containing rule 'NU0002' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 40 in NUnit.Analyzers/TestUsesSetupMethods/TestUsesSetupMethodsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / test

Enable analyzer release tracking for the analyzer project containing rule 'NU0002' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)
TestUsesSetupMethodsConstants.TestUsesSetupMethodsTitle,
TestUsesSetupMethodsConstants.TestUsesSetupMethodsMessage,
Categories.ParallelExecution,
DiagnosticSeverity.Warning,
true,
TestUsesSetupMethodsConstants.TestUsesSetupMethodsDescription,
TestUsesSetupMethodsConstants.TestUsesSetupMethodsUri
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace NUnit.Analyzers.TestUsesSetupMethods
{
internal class TestUsesSetupMethodsConstants
{
internal const string TestUsesSetupMethodsTitle = "Test uses setup methods";
internal const string TestUsesSetupMethodsMessage = "Setup methods are considered harmful";
internal const string TestUsesSetupMethodsDescription = "If you require a similar object or state for your tests, prefer a helper method than using Setup and Teardown attributes.";
internal const string TestUsesSetupMethodsUri = "https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-best-practices#prefer-helper-methods-to-setup-and-teardown";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using FluentAssertions;

using NUnit.Framework;
using NUnit.Framework.Internal.Execution;

using SkbKontur.NUnit.Middlewares;

Expand All @@ -24,18 +23,16 @@ namespace SkbKontur.NUnit.Extensions.Tests.Middlewares
[Parallelizable(ParallelScope.Self)]
public class ParallelParametrizedTestFixtureTest : SimpleTestBase
{
private event Action E;
private readonly int i1;
private int i2;
private const int i3 = 0;

public ParallelParametrizedTestFixtureTest(int i, int i2)
{
this.i = i;
this.i1 = 1;
E += () => {};
}

private event Action E;
private const int i3 = 0;

protected override void Configure(ISetupBuilder fixture, ISetupBuilder test)
{
i2 = 3;
Expand All @@ -44,6 +41,11 @@ protected override void Configure(ISetupBuilder fixture, ISetupBuilder test)
.Use(t => t.GetFromThisOrParentContext<Counter>().InvocationsCount += i);
}

[SetUp]
public void F()

Check warning on line 45 in NUnit.Extensions.Tests/Middlewares/ParallelParametrizedTestFixtureTest.cs

View workflow job for this annotation

GitHub Actions / test

Setup methods are considered harmful (https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-best-practices#prefer-helper-methods-to-setup-and-teardown)

Check warning on line 45 in NUnit.Extensions.Tests/Middlewares/ParallelParametrizedTestFixtureTest.cs

View workflow job for this annotation

GitHub Actions / test

Setup methods are considered harmful (https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-best-practices#prefer-helper-methods-to-setup-and-teardown)

Check warning on line 45 in NUnit.Extensions.Tests/Middlewares/ParallelParametrizedTestFixtureTest.cs

View workflow job for this annotation

GitHub Actions / test

Setup methods are considered harmful (https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-best-practices#prefer-helper-methods-to-setup-and-teardown)

Check warning on line 45 in NUnit.Extensions.Tests/Middlewares/ParallelParametrizedTestFixtureTest.cs

View workflow job for this annotation

GitHub Actions / test

Setup methods are considered harmful (https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-best-practices#prefer-helper-methods-to-setup-and-teardown)
{
}

[Test]
public void Test()
{
Expand All @@ -57,6 +59,9 @@ public void Test()
counter.InvocationsCount.Should().Be(i);
}

private readonly int i1;
private int i2;

Check warning on line 63 in NUnit.Extensions.Tests/Middlewares/ParallelParametrizedTestFixtureTest.cs

View workflow job for this annotation

GitHub Actions / test

Fields in test classes should be readonly

Check warning on line 63 in NUnit.Extensions.Tests/Middlewares/ParallelParametrizedTestFixtureTest.cs

View workflow job for this annotation

GitHub Actions / test

Fields in test classes should be readonly

Check warning on line 63 in NUnit.Extensions.Tests/Middlewares/ParallelParametrizedTestFixtureTest.cs

View workflow job for this annotation

GitHub Actions / test

Fields in test classes should be readonly

Check warning on line 63 in NUnit.Extensions.Tests/Middlewares/ParallelParametrizedTestFixtureTest.cs

View workflow job for this annotation

GitHub Actions / test

Fields in test classes should be readonly

private readonly int i;

private static readonly TestFixtureData[] testCases = Enumerable.Range(0, 100).Select(x => new TestFixtureData(x)).ToArray();
Expand Down

0 comments on commit 2826fd7

Please sign in to comment.