-
Notifications
You must be signed in to change notification settings - Fork 585
/
VSTest.fs
153 lines (144 loc) · 6.75 KB
/
VSTest.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/// Contains tasks to run [VSTest](https://msdn.microsoft.com/en-us/library/ms182486.aspx) unit tests.
[<System.Obsolete("use Fake.DotNet.Testing.VSTest instead")>]
module Fake.VSTest
open System
open System.Text
/// [omit]
[<System.Obsolete("use Fake.DotNet.Testing.VSTest instead")>]
let vsTestPaths =
[| @"[ProgramFilesX86]\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow";
@"[ProgramFilesX86]\Microsoft Visual Studio 12.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow";
@"[ProgramFilesX86]\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow" |]
/// [omit]
[<System.Obsolete("use Fake.DotNet.Testing.VSTest instead")>]
let vsTestExe =
if isMono then failwith "VSTest is not supported on the mono platform"
else "vstest.console.exe"
/// Option which allow to specify if a VSTest error should break the build.
[<System.Obsolete("use Fake.DotNet.Testing.VSTest instead")>]
type ErrorLevel = TestRunnerErrorLevel
/// Parameter type to configure [VSTest.Console.exe](https://msdn.microsoft.com/en-us/library/jj155800.aspx)
[<System.Obsolete("use Fake.DotNet.Testing.VSTest instead")>]
[<CLIMutable>]
type VSTestParams =
{ /// Path to the run settings file to run tests with additional settings such as data collectors (optional).
SettingsPath : string
/// Names of the tests that should be run (optional).
Tests : seq<string>
/// Enables code coverage collection (optional).
EnableCodeCoverage : bool
/// Run the tests in an isolated process (optional).
InIsolation : bool
/// Use installed VSIX extensions in VSTest (optional).
UseVsixExtensions : bool
/// Target platform architecture for test execution (optional). Valid options include "x86", "x64" and "ARM".
Platform : string
/// Target .NET framework version to use for test execution (optional).
Framework : string
/// Run tests that match the given expression (optional). Cannot be used with the Tests argument
TestCaseFilter : string
/// The logger to use for test results (optional).
Logger : string
/// List discovered tests from the given container path (optional).
ListTestsPath : string
/// List installed test discoverers (optional).
ListDiscoverers : bool
/// List installed test executors (optional).
ListExecutors : bool
/// List installed loggers (optional).
ListLoggers : bool
/// List installed settings providers (optional).
ListSettingsProviders : bool
/// Path to VSTest.Console.exe (optional). By default the default install location is searched.
ToolPath : string
/// Working directory (optional).
WorkingDir : string
/// A timeout for the test runner (optional).
TimeOut : TimeSpan
/// Error level for controlling how VSTest failures should break the build (optional).
ErrorLevel : ErrorLevel
/// Path to test adapter e.g. xUnit (optional)
TestAdapterPath: string}
/// VSTest default parameters.
[<System.Obsolete("use Fake.DotNet.Testing.VSTest instead")>]
let VSTestDefaults =
{ SettingsPath = null
Tests = []
EnableCodeCoverage = false
InIsolation = true
UseVsixExtensions = false
Platform = null
Framework = null
TestCaseFilter = null
Logger = null
ListTestsPath = null
ListDiscoverers = false
ListExecutors = false
ListLoggers = false
ListSettingsProviders = false
ToolPath =
match tryFindFile vsTestPaths vsTestExe with
| Some path -> path
| None -> ""
WorkingDir = null
TimeOut = TimeSpan.MaxValue
ErrorLevel = ErrorLevel.Error
TestAdapterPath = null }
/// Builds the command line arguments from the given parameter record and the given assemblies.
/// [omit]
[<System.Obsolete("use Fake.DotNet.Testing.VSTest instead")>]
let buildVSTestArgs (parameters : VSTestParams) assembly =
let testsToRun =
if not (Seq.isEmpty parameters.Tests) then
sprintf @"/Tests:%s" (parameters.Tests |> separated ",")
else null
new StringBuilder()
|> appendIfTrue (assembly <> null) assembly
|> appendIfNotNull parameters.SettingsPath "/Settings:"
|> appendIfTrue (testsToRun <> null) testsToRun
|> appendIfTrue parameters.EnableCodeCoverage "/EnableCodeCoverage"
|> appendIfTrue parameters.InIsolation "/InIsolation"
|> appendIfTrue parameters.UseVsixExtensions "/UseVsixExtensions:true"
|> appendIfNotNull parameters.Platform "/Platform:"
|> appendIfNotNull parameters.Framework "/Framework:"
|> appendIfNotNull parameters.TestCaseFilter "/TestCaseFilter:"
|> appendIfNotNull parameters.Logger "/Logger:"
|> appendIfNotNull parameters.ListTestsPath "/ListTests:"
|> appendIfTrue parameters.ListDiscoverers "/ListDiscoverers"
|> appendIfTrue parameters.ListExecutors "/ListExecutors"
|> appendIfTrue parameters.ListLoggers "/ListLoggers"
|> appendIfTrue parameters.ListSettingsProviders "/ListSettingsProviders"
|> appendIfNotNull parameters.TestAdapterPath "/TestAdapterPath:"
|> toText
/// Runs VSTest command line tool (VSTest.Console.exe) on a group of assemblies.
/// ## Parameters
///
/// - `setParams` - Function used to manipulate the default VSTestParams values.
/// - `assemblies` - Sequence of one or more assemblies containing Microsoft Visual Studio Unit Test Framework unit tests.
///
/// ## Sample usage
///
/// Target "Test" (fun _ ->
/// !! (testDir + @"\*.Tests.dll")
/// |> VSTest (fun p -> { p with SettingsPath = "Local.RunSettings" })
/// )
[<System.Obsolete("use Fake.DotNet.Testing.VSTest instead")>]
let VSTest (setParams : VSTestParams -> VSTestParams) (assemblies : string seq) =
let details = assemblies |> separated ", "
use __ = traceStartTaskUsing "VSTest" details
let parameters = VSTestDefaults |> setParams
if isNullOrEmpty parameters.ToolPath then failwith "VSTest: No tool path specified, or it could not be found automatically."
let assemblies = assemblies |> Seq.toArray
if Array.isEmpty assemblies then failwith "VSTest: cannot run tests (the assembly list is empty)."
let failIfError assembly exitCode =
if exitCode > 0 && parameters.ErrorLevel <> ErrorLevel.DontFailBuild then
let message = sprintf "%sVSTest test run failed for %s" Environment.NewLine assembly
traceError message
failwith message
for assembly in assemblies do
let args = buildVSTestArgs parameters assembly
ExecProcess (fun info ->
info.FileName <- parameters.ToolPath
info.WorkingDirectory <- parameters.WorkingDir
info.Arguments <- args) parameters.TimeOut
|> failIfError assembly