Skip to content

Commit e57b257

Browse files
authored
Add ability to run only specific tests (#19)
Semver: minor Resolves #16
1 parent 81bf7d0 commit e57b257

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

src/Directory.Build.props

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="SourceLink.Create.CommandLine" Version="[2.5.0]" PrivateAssets="all" />
12-
<PackageReference Include="SourceLink.Test" Version="[2.5.0]" PrivateAssets="all" />
13-
<DotNetCliToolReference Include="dotnet-sourcelink" Version="[2.5.0]" />
11+
<PackageReference Include="SourceLink.Create.CommandLine" Version="[2.8.3]" PrivateAssets="all" />
12+
<PackageReference Include="SourceLink.Test" Version="[2.8.3]" PrivateAssets="all" />
13+
<DotNetCliToolReference Include="dotnet-sourcelink" Version="[2.8.3]" />
1414
</ItemGroup>
1515
</Project>

src/YoloDev.Expecto.TestSdk/adapter.fs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,22 @@ type VsTestAdapter () =
4646
interface ITestExecutor with
4747
member x.Cancel () = cts.Cancel ()
4848

49-
member x.RunTests (tests: TestCase seq, runContext: IRunContext, frameworkHandle: IFrameworkHandle) : unit = failwith "Not implemented (run testcases)"
49+
member x.RunTests (tests: TestCase seq, runContext: IRunContext, frameworkHandle: IFrameworkHandle) : unit =
50+
let tests = Guard.argNotNull "tests" tests
51+
let runContext = Guard.argNotNull "runContext" runContext
52+
let frameworkHandle = Guard.argNotNull "frameworkHandle" frameworkHandle
53+
54+
let stopwatch = Stopwatch.StartNew ()
55+
let logger = Logger (frameworkHandle, stopwatch)
56+
57+
let runSettings =
58+
Option.ofObj runContext
59+
|> Option.bind (fun c -> Option.ofObj c.RunSettings)
60+
|> Option.map RunSettings.read
61+
|> Option.defaultValue RunSettings.defaultSettings
62+
63+
Execution.runSpecifiedTests logger frameworkHandle tests
64+
|> Async.RunSynchronously
5065

5166
member x.RunTests (sources: string seq, runContext: IRunContext, frameworkHandle: IFrameworkHandle) : unit =
5267
let sources = Guard.argNotNull "sources" sources

src/YoloDev.Expecto.TestSdk/execution.fs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,15 @@ module Execution =
133133
let config = { ExpectoConfig.defaultConfig with printer = printAdapter }
134134
Expecto.Logging.Global.initialise <| { Expecto.Logging.Global.defaultConfig with getLogger = getLogger }
135135

136-
// todo: filter test
137-
let tests = ExpectoTest.test test
136+
let testNames =
137+
cases
138+
|> Seq.map (fun c -> c.DisplayName)
139+
|> Set.ofSeq
140+
141+
let tests =
142+
ExpectoTest.test test
143+
|> Expecto.Test.filter (fun name -> Seq.contains name testNames)
144+
138145
let duplicates = duplicatedNames tests
139146
match duplicates with
140147
| [] -> Expecto.Impl.runEval config tests |> Async.Ignore
@@ -152,9 +159,30 @@ module Execution =
152159
|> List.ofSeq
153160
runMatchingTests logger test cases frameworkHandle
154161

162+
let private runSpecifiedTestsForSource logger frameworkHandle source (tests: TestCase seq) =
163+
let nameSet = tests |> Seq.map (fun t -> t.FullyQualifiedName) |> Set.ofSeq
164+
match Discovery.discoverTestForSource logger source with
165+
| None -> async.Zero ()
166+
| Some test ->
167+
let cases =
168+
Discovery.getTestCasesFromTest logger test
169+
|> Seq.map ExpectoTestCase.case
170+
|> Seq.filter (fun c -> Set.contains c.FullyQualifiedName nameSet)
171+
|> List.ofSeq
172+
173+
runMatchingTests logger test cases frameworkHandle
174+
155175
let runTests logger frameworkHandle sources =
156176
async {
157177
for source in sources do
158178
do! runTestsForSource logger frameworkHandle source
159179
}
180+
181+
let runSpecifiedTests logger frameworkHandle (tests: TestCase seq) =
182+
let bySource = tests |> Seq.groupBy (fun t -> t.Source)
183+
184+
async {
185+
for source, tests in bySource do
186+
do! runSpecifiedTestsForSource logger frameworkHandle source tests
187+
}
160188

0 commit comments

Comments
 (0)