Skip to content

Commit 1a63409

Browse files
committed
Workaround for NUnit not respecting test filters
NUnit doesn't respect test filters when VSTest is in DesignMode, which it is by default with VsTestConsoleWrapper ionide#1383 (comment)
1 parent 7d03e4a commit 1a63409

File tree

6 files changed

+85
-9
lines changed

6 files changed

+85
-9
lines changed

src/FsAutoComplete.Core/TestServer.fs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,15 @@ module VSTestWrapper =
115115
module TestPlatformOptions =
116116
let withTestCaseFilter (options: TestPlatformOptions) filterExpression = options.TestCaseFilter <- filterExpression
117117

118-
119-
/// attachDebugger assumes that the debugger is attached when the method returns. The test project will continue execution as soon as attachDebugger returns
118+
module RunSettings =
119+
let defaultRunSettings =
120+
"<RunSettings>
121+
<RunConfiguration>
122+
<DesignMode>False</DesignMode>
123+
</RunConfiguration>
124+
</RunSettings>"
125+
126+
/// onAttachDebugger assumes that the debugger is attached when the method returns. The test project will continue execution as soon as attachDebugger returns
120127
let runTestsAsync
121128
(vstestPath: string)
122129
(onTestRunProgress: TestRunUpdate -> unit)
@@ -129,6 +136,7 @@ module VSTestWrapper =
129136
let consoleParams = ConsoleParameters()
130137
let vstest = new VsTestConsoleWrapper(vstestPath, consoleParams)
131138
let runHandler = TestRunHandler(onTestRunProgress)
139+
let runSettings = RunSettings.defaultRunSettings
132140

133141
let options = new TestPlatformOptions()
134142
testCaseFilter |> Option.iter (TestPlatformOptions.withTestCaseFilter options)
@@ -141,9 +149,9 @@ module VSTestWrapper =
141149

142150
if shouldDebug then
143151
let hostLauncher = TestHostLauncher(shouldDebug, onAttachDebugger)
144-
vstest.RunTestsWithCustomTestHost(sources, null, options, runHandler, hostLauncher)
152+
vstest.RunTestsWithCustomTestHost(sources, runSettings, options, runHandler, hostLauncher)
145153
else
146-
vstest.RunTests(sources, null, options, runHandler)
154+
vstest.RunTests(sources, runSettings, options, runHandler)
147155

148156
return runHandler.TestResults |> List.ofSeq
149157
}

test/FsAutoComplete.Tests.TestExplorer/FsAutoComplete.Tests.TestExplorer.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<PostBuildEvent>
1818
dotnet build $(MSBuildProjectDirectory)/SampleTestProjects/VSTest.XUnit.Tests/
1919
dotnet build $(MSBuildProjectDirectory)/SampleTestProjects/VSTest.XUnit.RunResults/
20+
dotnet build $(MSBuildProjectDirectory)/SampleTestProjects/VSTest.NUnit/
2021
</PostBuildEvent>
2122
</PropertyGroup>
2223
<Import Project="..\..\.paket\Paket.Restore.targets" />
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module VSTest.NUnit
2+
3+
open NUnit.Framework
4+
5+
[<SetUp>]
6+
let Setup () = ()
7+
8+
[<Test>]
9+
let Test1 () = Assert.Pass()
10+
11+
[<Test>]
12+
let Test2 () = Assert.Pass()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<LangVersion>latest</LangVersion>
6+
<IsPackable>false</IsPackable>
7+
<GenerateProgramFile>true</GenerateProgramFile>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<Compile Include="UnitTest1.fs"/>
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="FSharp.Core" Version="9.0.303" />
16+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
17+
<PackageReference Include="NUnit" Version="4.4.0" />
18+
<PackageReference Include="NUnit.Analyzers" Version="4.10.0">
19+
<PrivateAssets>all</PrivateAssets>
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
</PackageReference>
22+
<PackageReference Include="NUnit3TestAdapter" Version="5.1.0" />
23+
</ItemGroup>
24+
</Project>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SampleTestProjects aren't managed with paket
2+
# This is here to prevent the FsAutoComplete paket from trying manage sample project dependencies

test/FsAutoComplete.Tests.TestExplorer/TestRunTests.fs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ let nullAttachDebugger _ = false
1313
let tests =
1414
testList
1515
"VSTestWrapper Test Run"
16-
[ testCaseAsync "should return an empty list if given no projects"
16+
[ testCaseAsync "it should return an empty list if given no projects"
1717
<| async {
1818
let expected = []
1919
let! actual = VSTestWrapper.runTestsAsync vstestPath ignore nullAttachDebugger [] None false
2020
Expect.equal actual expected ""
2121
}
2222

23-
testCaseAsync "should be able to report basic test run outcomes"
23+
testCaseAsync "it should be able to report basic test run outcomes"
2424
<| async {
2525
let expected =
2626
[ "Tests.My test", TestOutcome.Passed
@@ -45,7 +45,7 @@ let tests =
4545
Expect.equal (set actual) (set expected) ""
4646
}
4747

48-
testCaseAsync "should run only tests that match the case filter"
48+
testCaseAsync "it should run only tests that match the case filter"
4949
<| async {
5050
let expected =
5151
[ ("Tests+Nested.Test 1", TestOutcome.Passed)
@@ -72,7 +72,36 @@ let tests =
7272
Expect.equal (set actual) (set expected) ""
7373
}
7474

75-
testCaseAsync "should report processIds when debugging is on"
75+
testCaseAsync "it should respect test filters on NUnit projects"
76+
<| async {
77+
// NOTE: This is an NUnit bug. NUnit doesn't respect filters when VSTest is in Design Mode, which VsTestConsoleWrapper is by default
78+
// https://github.com/ionide/FsAutoComplete/pull/1383#issuecomment-3245590606
79+
let expected = [ "VSTest.NUnit.Test1", TestOutcome.Passed ]
80+
81+
let sources =
82+
[ Path.Combine(ResourceLocators.sampleProjectsRootDir, "VSTest.NUnit/bin/Debug/net8.0/VSTest.NUnit.dll") ]
83+
84+
let onTestRunProgress (progress: VSTestWrapper.TestRunUpdate) =
85+
match progress with
86+
| VSTestWrapper.TestRunUpdate.LogMessage message -> printfn $"Sya: {message}"
87+
| _ -> ()
88+
89+
let! runResults =
90+
VSTestWrapper.runTestsAsync
91+
vstestPath
92+
onTestRunProgress
93+
nullAttachDebugger
94+
sources
95+
(Some "FullyQualifiedName~Test1")
96+
false
97+
98+
let likenessOfTestResult (result: TestResult) = (result.TestCase.FullyQualifiedName, result.Outcome)
99+
let actual = runResults |> List.map likenessOfTestResult
100+
101+
Expect.equal (set actual) (set expected) ""
102+
}
103+
104+
testCaseAsync "it should report processIds when debugging is on"
76105
<| async {
77106
use tokenSource = new System.Threading.CancellationTokenSource(2000)
78107

@@ -103,7 +132,7 @@ let tests =
103132
Expect.isSome actualProcessId "Expected runTest to report a processId"
104133
}
105134

106-
testCaseAsync "should report a processId only once per process"
135+
testCaseAsync "it should report a processId only once per process"
107136
<| async {
108137
use tokenSource = new System.Threading.CancellationTokenSource(1000)
109138

0 commit comments

Comments
 (0)