Skip to content

Commit

Permalink
TestMethodWithoutTestAttribute: Don't trigger when NUnit.TestCase` or…
Browse files Browse the repository at this point in the history
… `NUnit.TestCaseSource` is present on a method (#361)
  • Loading branch information
Vannevelj authored Feb 22, 2025
1 parent 091c39d commit 5c923e4
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# CHANGELOG
https://keepachangelog.com/en/1.0.0/

## [1.27.1] - 2025-02-22
- `TestMethodWithoutTestAttribute`: Don't trigger when `NUnit.TestCase` or `NUnit.TestCaseSource` is present on a method

## [1.27.0] - 2025-01-09
- `ElementaryMethodsOfTypeInCollectionNotOverridden`: Don't exclude all `System` assembly types, exclude only structs

Expand Down
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<LangVersion>11.0</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AnalysisLevel>latest</AnalysisLevel>
<NoWarn>NU1902;NU1903;NU1904</NoWarn>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ or add a reference yourself:

```xml
<ItemGroup>
<PackageReference Include="SharpSource" Version="1.27.0" PrivateAssets="All" />
<PackageReference Include="SharpSource" Version="1.27.1" PrivateAssets="All" />
</ItemGroup>
```

Expand Down
2 changes: 1 addition & 1 deletion SharpSource/SharpSource.Package/SharpSource.Package.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<PropertyGroup>
<PackageId>SharpSource</PackageId>
<PackageVersion>1.27.0</PackageVersion>
<PackageVersion>1.27.1</PackageVersion>
<Authors>Jeroen Vannevel</Authors>
<PackageLicenseUrl>https://github.com/Vannevelj/SharpSource/blob/master/LICENSE.md</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/Vannevelj/SharpSource</PackageProjectUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public IEnumerator<object[]> GetEnumerator()
}

[TestMethod]
public async Task TestMethodWithoutTestAttribute_OtherAttributeIsTestRelated_TestCase()
public async Task TestMethodWithoutTestAttribute_Nunit_TestCase()
{
var original = @"
using System;
Expand All @@ -321,11 +321,11 @@ class MyClass
}
";

await VerifyCS.VerifyDiagnosticWithoutFix(original, VerifyCS.Diagnostic().WithMessage("Method MyMethod might be missing a test attribute"));
await VerifyCS.VerifyNoDiagnostic(original);
}

[TestMethod]
public async Task TestMethodWithoutTestAttribute_OtherAttributeIsTestRelated_TestCaseSource()
public async Task TestMethodWithoutTestAttribute_NUnit_TestCaseSource()
{
var original = @"
using System;
Expand All @@ -348,7 +348,27 @@ private static IEnumerable<TestCaseData> DataSource()
}
";

await VerifyCS.VerifyDiagnosticWithoutFix(original, VerifyCS.Diagnostic().WithMessage("Method MyMethod might be missing a test attribute"));
await VerifyCS.VerifyNoDiagnostic(original);
}

[TestMethod]
public async Task TestMethodWithoutTestAttribute_Nunit_WithoutTestFixture_TestCase()
{
var original = @"
using System;
using NUnit.Framework;
class MyClass
{
[TestCase(3)]
public void {|#0:MyMethod|}(int x)
{
}
}
";

await VerifyCS.VerifyNoDiagnostic(original);
}

[BugVerificationTest(IssueUrl = "https://github.com/Vannevelj/SharpSource/issues/22")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public override void Initialize(AnalysisContext context)

var testMethodAttributeSymbols = ImmutableArray.Create(
compilationContext.Compilation.GetTypeByMetadataName("Xunit.FactAttribute"),
compilationContext.Compilation.GetTypeByMetadataName("Xunit.TheoryAttribute")
compilationContext.Compilation.GetTypeByMetadataName("Xunit.TheoryAttribute"),
compilationContext.Compilation.GetTypeByMetadataName("NUnit.Framework.TestCaseAttribute"),
compilationContext.Compilation.GetTypeByMetadataName("NUnit.Framework.TestCaseSourceAttribute")
);

var taskTypes = ImmutableArray.Create(
Expand All @@ -45,9 +47,7 @@ public override void Initialize(AnalysisContext context)
var allowedAdditionalAttributes = ImmutableArray.Create(
compilationContext.Compilation.GetTypeByMetadataName("Xunit.ClassDataAttribute"),
compilationContext.Compilation.GetTypeByMetadataName("Xunit.InlineDataAttribute"),
compilationContext.Compilation.GetTypeByMetadataName("Microsoft.VisualStudio.TestTools.UnitTesting.DataRowAttribute"),
compilationContext.Compilation.GetTypeByMetadataName("NUnit.Framework.TestCaseAttribute"),
compilationContext.Compilation.GetTypeByMetadataName("NUnit.Framework.TestCaseSourceAttribute")
compilationContext.Compilation.GetTypeByMetadataName("Microsoft.VisualStudio.TestTools.UnitTesting.DataRowAttribute")
);

compilationContext.RegisterSymbolAction(context => Analyze(context, testClassAttributeSymbols, testMethodAttributeSymbols, taskTypes, allowedAdditionalAttributes), SymbolKind.Method);
Expand Down

0 comments on commit 5c923e4

Please # to comment.