Skip to content

Commit 3db578b

Browse files
authored
Update xUnit1004 to not trigger with SkipWhen or SkipUnless (#193)
1 parent 99fcb51 commit 3db578b

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/xunit.analyzers.tests/Analyzers/X1000/TestMethodShouldNotBeSkippedTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Threading.Tasks;
2+
using Microsoft.CodeAnalysis.CSharp;
23
using Xunit;
34
using Verify = CSharpVerifier<Xunit.Analyzers.TestMethodShouldNotBeSkipped>;
45

@@ -33,4 +34,36 @@ public void TestMethod() {{ }}
3334

3435
await Verify.VerifyAnalyzer(source);
3536
}
37+
38+
[Theory]
39+
[InlineData("Fact")]
40+
[InlineData("Theory")]
41+
public async Task SkippedTestWhenConditionIsTrue_V3_DoesNotTrigger(string attribute)
42+
{
43+
var source = string.Format(/* lang=c#-test */ """
44+
public class TestClass {{
45+
public static bool Condition {{ get; set; }}
46+
[Xunit.{0}(Skip="Lazy", SkipWhen=nameof(Condition))]
47+
public void TestMethod() {{ }}
48+
}}
49+
""", attribute);
50+
51+
await Verify.VerifyAnalyzerV3(LanguageVersion.CSharp7, source);
52+
}
53+
54+
[Theory]
55+
[InlineData("Fact")]
56+
[InlineData("Theory")]
57+
public async Task SkippedTestUnlessConditionIsTrue_V3_DoesNotTrigger(string attribute)
58+
{
59+
var source = string.Format(/* lang=c#-test */ """
60+
public class TestClass {{
61+
public static bool Condition {{ get; set; }}
62+
[Xunit.{0}(Skip="Lazy", SkipUnless=nameof(Condition))]
63+
public void TestMethod() {{ }}
64+
}}
65+
""", attribute);
66+
67+
await Verify.VerifyAnalyzerV3(LanguageVersion.CSharp7, source);
68+
}
3669
}

src/xunit.analyzers/X1000/TestMethodShouldNotBeSkipped.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Linq;
21
using Microsoft.CodeAnalysis;
32
using Microsoft.CodeAnalysis.CSharp;
43
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -26,8 +25,21 @@ public override void AnalyzeCompilation(
2625
return;
2726
if (context.Node is not AttributeSyntax attribute)
2827
return;
28+
if (attribute.ArgumentList is null)
29+
return;
30+
31+
var skipArgument = default(AttributeArgumentSyntax);
32+
foreach (var argument in attribute.ArgumentList.Arguments)
33+
{
34+
var valueText = argument.NameEquals?.Name?.Identifier.ValueText;
35+
36+
if (valueText == "SkipWhen" || valueText == "SkipUnless")
37+
return;
38+
39+
if (valueText == "Skip")
40+
skipArgument = argument;
41+
}
2942

30-
var skipArgument = attribute.ArgumentList?.Arguments.FirstOrDefault(arg => arg.NameEquals?.Name?.Identifier.ValueText == "Skip");
3143
if (skipArgument is null)
3244
return;
3345

0 commit comments

Comments
 (0)