|
| 1 | +/// Contains tasks to run [MSTest](http://en.wikipedia.org/wiki/Visual_Studio_Unit_Testing_Framework/) unit tests. |
| 2 | +module Fake.DotNet.Testing.MSTest |
| 3 | + |
| 4 | +open System |
| 5 | +open System.Text |
| 6 | +open Fake.Core.String |
| 7 | +open Fake.Core.Process |
| 8 | +open Fake.Core |
| 9 | +open Fake.Testing.Common |
| 10 | + |
| 11 | +/// [omit] |
| 12 | +let mstestPaths = |
| 13 | + [| @"[ProgramFilesX86]\Microsoft Visual Studio 14.0\Common7\IDE"; |
| 14 | + @"[ProgramFilesX86]\Microsoft Visual Studio 12.0\Common7\IDE"; |
| 15 | + @"[ProgramFilesX86]\Microsoft Visual Studio 11.0\Common7\IDE"; |
| 16 | + @"[ProgramFilesX86]\Microsoft Visual Studio 10.0\Common7\IDE" |] |
| 17 | + |
| 18 | +/// [omit] |
| 19 | +let mstestexe = |
| 20 | + if Environment.isWindows then "mstest.exe" |
| 21 | + else failwith "MSTest is only supported on Windows platform" |
| 22 | + |
| 23 | +// TODO: try to use VSTest.Console.exe as well (VS2012 and up only) |
| 24 | +/// Option which allow to specify if a MSTest error should break the build. |
| 25 | +type ErrorLevel = TestRunnerErrorLevel |
| 26 | + |
| 27 | +/// Parameter type to configure the MSTest.exe. |
| 28 | +[<CLIMutable>] |
| 29 | +type MSTestParams = |
| 30 | + { /// Test category filter (optional). The test category filter consists of one or more test category names separated by the logical operators '&', '|', '!', '&!'. The logical operators '&' and '|' cannot be used together to create a test category filter. |
| 31 | + Category : string |
| 32 | + /// Test results directory (optional) |
| 33 | + ResultsDir : string |
| 34 | + /// Path to the Test Metadata file (.vsmdi) (optional) |
| 35 | + TestMetadataPath : string |
| 36 | + /// Path to the Test Settings file (.testsettings) (optional) |
| 37 | + TestSettingsPath : string |
| 38 | + /// Working directory (optional) |
| 39 | + WorkingDir : string |
| 40 | + /// List of tests be run (optional) |
| 41 | + Tests : string list |
| 42 | + /// A timeout for the test runner (optional) |
| 43 | + TimeOut : TimeSpan |
| 44 | + /// Path to MSTest.exe |
| 45 | + ToolPath : string |
| 46 | + /// Option which allow to specify if a MSTest error should break the build. |
| 47 | + ErrorLevel : ErrorLevel |
| 48 | + /// Run tests in isolation (optional). |
| 49 | + NoIsolation : bool } |
| 50 | + |
| 51 | +/// MSTest default parameters. |
| 52 | +let MSTestDefaults = |
| 53 | + { Category = null |
| 54 | + ResultsDir = null |
| 55 | + TestMetadataPath = null |
| 56 | + TestSettingsPath = null |
| 57 | + WorkingDir = null |
| 58 | + Tests = [] |
| 59 | + TimeOut = TimeSpan.FromMinutes 5. |
| 60 | + ToolPath = |
| 61 | + match tryFindFile mstestPaths mstestexe with |
| 62 | + | Some path -> path |
| 63 | + | None -> "" |
| 64 | + ErrorLevel = ErrorLevel.Error |
| 65 | + NoIsolation = true } |
| 66 | + |
| 67 | +/// Builds the command line arguments from the given parameter record and the given assemblies. |
| 68 | +/// [omit] |
| 69 | +let buildMSTestArgs parameters assembly = |
| 70 | + let testResultsFile = |
| 71 | + if parameters.ResultsDir <> null then |
| 72 | + sprintf @"%s\%s.trx" parameters.ResultsDir (DateTime.Now.ToString("yyyyMMdd-HHmmss.ff")) |
| 73 | + else null |
| 74 | + |
| 75 | + let builder = |
| 76 | + new StringBuilder() |
| 77 | + |> appendIfNotNull assembly "/testcontainer:" |
| 78 | + |> appendIfNotNull parameters.Category "/category:" |
| 79 | + |> appendIfNotNull parameters.TestMetadataPath "/testmetadata:" |
| 80 | + |> appendIfNotNull parameters.TestSettingsPath "/testsettings:" |
| 81 | + |> appendIfNotNull testResultsFile "/resultsfile:" |
| 82 | + |> appendIfTrue parameters.NoIsolation "/noisolation" |
| 83 | + |
| 84 | + parameters.Tests |
| 85 | + |> List.iter (fun t -> builder |> appendIfNotNullOrEmpty t "/test:" |> ignore) |
| 86 | + |
| 87 | + builder |> toText |
| 88 | + |
| 89 | +/// Runs MSTest command line tool on a group of assemblies. |
| 90 | +/// ## Parameters |
| 91 | +/// |
| 92 | +/// - `setParams` - Function used to manipulate the default MSTestParams value. |
| 93 | +/// - `assemblies` - Sequence of one or more assemblies containing Microsoft Visual Studio Unit Test Framework unit tests. |
| 94 | +/// |
| 95 | +/// ## Sample usage |
| 96 | +/// |
| 97 | +/// Target "Test" (fun _ -> |
| 98 | +/// !! (testDir + @"\*.Tests.dll") |
| 99 | +/// |> MSTest (fun p -> { p with Category = "group1" }) |
| 100 | +/// ) |
| 101 | +let MSTest (setParams : MSTestParams -> MSTestParams) (assemblies : string seq) = |
| 102 | + let details = assemblies |> separated ", " |
| 103 | + use __ = Trace.traceTask "MSTest" details |
| 104 | + let parameters = MSTestDefaults |> setParams |
| 105 | + let assemblies = assemblies |> Seq.toArray |
| 106 | + if Array.isEmpty assemblies then failwith "MSTest: cannot run tests (the assembly list is empty)." |
| 107 | + let failIfError assembly exitCode = |
| 108 | + if exitCode > 0 && parameters.ErrorLevel <> ErrorLevel.DontFailBuild then |
| 109 | + let message = sprintf "%sMSTest test run failed for %s" Environment.NewLine assembly |
| 110 | + Trace.traceError message |
| 111 | + failwith message |
| 112 | + for assembly in assemblies do |
| 113 | + let args = buildMSTestArgs parameters assembly |
| 114 | + ExecProcess (fun info -> |
| 115 | + info.FileName <- parameters.ToolPath |
| 116 | + info.WorkingDirectory <- parameters.WorkingDir |
| 117 | + info.Arguments <- args) parameters.TimeOut |
| 118 | + |> failIfError assembly |
0 commit comments