Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSTest: add Tests parameter to specify list of tests #1615

Merged
merged 5 commits into from Jul 28, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.fsx
Expand Up @@ -34,6 +34,7 @@ open Fake.DotNet.Cli
open Fake.Testing.Common
open Fake.DotNet.Testing.MSpec
open Fake.DotNet.Testing.XUnit2
open Fake.DotNet.Testing.MSTest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a small bootstrapping problem with this ;). I think we can remove those lines for now.

open Fake.DotNet.Testing.NUnit3
open Fake.DotNet.NuGet.NuGet
open Fake.Core.Globbing.Tools
Expand Down Expand Up @@ -69,6 +70,7 @@ open SourceLink
open Fake.ReleaseNotesHelper
open Fake.AssemblyInfoFile
open Fake.Testing.XUnit2
open Fake.MSTest
open Fake.Testing.NUnit3
#endif

Expand Down Expand Up @@ -233,6 +235,7 @@ let dotnetAssemblyInfos =
"Fake.DotNet.Testing.MSpec", "Running mspec test runner"
"Fake.DotNet.Testing.NUnit", "Running nunit test runner"
"Fake.DotNet.Testing.XUnit2", "Running xunit test runner"
"Fake.DotNet.Testing.MSTest", "Running mstest test runner"
"Fake.IO.FileSystem", "Core Filesystem utilities"
"Fake.IO.Zip", "Core Zip functionality"
"Fake.netcore", "Command line tool"
Expand Down
14 changes: 14 additions & 0 deletions src/Fake-netcore.sln
Expand Up @@ -43,6 +43,8 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Fake.DotNet.Testing.NUnit",
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Fake.DotNet.Testing.XUnit2", "app\Fake.DotNet.Testing.XUnit2\Fake.DotNet.Testing.XUnit2.fsproj", "{21E2FE31-4E7C-489E-8215-9303108A2F39}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Fake.DotNet.Testing.MSTest", "app\Fake.DotNet.Testing.MSTest\Fake.DotNet.Testing.MSTest.fsproj", "{21E2FE31-4E7C-489E-8215-9303108A2F30}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Fake.IO.FileSystem", "app\Fake.IO.FileSystem\Fake.IO.FileSystem.fsproj", "{4B1416CD-C7CB-4670-8EFE-871ED316D51D}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Fake.IO.Zip", "app\Fake.IO.Zip\Fake.IO.Zip.fsproj", "{46ED6A9C-C5BF-4495-924E-478736FC280E}"
Expand Down Expand Up @@ -306,6 +308,18 @@ Global
{21E2FE31-4E7C-489E-8215-9303108A2F39}.Release|x64.Build.0 = Release|x64
{21E2FE31-4E7C-489E-8215-9303108A2F39}.Release|x86.ActiveCfg = Release|x86
{21E2FE31-4E7C-489E-8215-9303108A2F39}.Release|x86.Build.0 = Release|x86
{21E2FE31-4E7C-489E-8215-9303108A2F30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{21E2FE31-4E7C-489E-8215-9303108A2F30}.Debug|Any CPU.Build.0 = Debug|Any CPU
{21E2FE31-4E7C-489E-8215-9303108A2F30}.Debug|x64.ActiveCfg = Debug|x64
{21E2FE31-4E7C-489E-8215-9303108A2F30}.Debug|x64.Build.0 = Debug|x64
{21E2FE31-4E7C-489E-8215-9303108A2F30}.Debug|x86.ActiveCfg = Debug|x86
{21E2FE31-4E7C-489E-8215-9303108A2F30}.Debug|x86.Build.0 = Debug|x86
{21E2FE31-4E7C-489E-8215-9303108A2F30}.Release|Any CPU.ActiveCfg = Release|Any CPU
{21E2FE31-4E7C-489E-8215-9303108A2F30}.Release|Any CPU.Build.0 = Release|Any CPU
{21E2FE31-4E7C-489E-8215-9303108A2F30}.Release|x64.ActiveCfg = Release|x64
{21E2FE31-4E7C-489E-8215-9303108A2F30}.Release|x64.Build.0 = Release|x64
{21E2FE31-4E7C-489E-8215-9303108A2F30}.Release|x86.ActiveCfg = Release|x86
{21E2FE31-4E7C-489E-8215-9303108A2F30}.Release|x86.Build.0 = Release|x86
{4B1416CD-C7CB-4670-8EFE-871ED316D51D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4B1416CD-C7CB-4670-8EFE-871ED316D51D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4B1416CD-C7CB-4670-8EFE-871ED316D51D}.Debug|x64.ActiveCfg = Debug|x64
Expand Down
17 changes: 17 additions & 0 deletions src/app/Fake.DotNet.Testing.MSTest/AssemblyInfo.fs
@@ -0,0 +1,17 @@
// Auto-Generated by FAKE; do not edit
namespace System
open System.Reflection

[<assembly: AssemblyTitleAttribute("FAKE - F# Make Running mstest test runner")>]
[<assembly: AssemblyProductAttribute("FAKE - F# Make")>]
[<assembly: AssemblyVersionAttribute("5.0.0")>]
[<assembly: AssemblyInformationalVersionAttribute("5.0.0")>]
[<assembly: AssemblyFileVersionAttribute("5.0.0")>]
do ()

module internal AssemblyVersionInformation =
let [<Literal>] AssemblyTitle = "FAKE - F# Make Running mstest test runner"
let [<Literal>] AssemblyProduct = "FAKE - F# Make"
let [<Literal>] AssemblyVersion = "5.0.0"
let [<Literal>] AssemblyInformationalVersion = "5.0.0"
let [<Literal>] AssemblyFileVersion = "5.0.0"
@@ -0,0 +1,55 @@
<Project Sdk="FSharp.NET.Sdk;Microsoft.NET.Sdk" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VersionPrefix>1.0.0-alpha-10</VersionPrefix>
<!--<TargetFrameworks>net46;netstandard1.6</TargetFrameworks>-->
<TargetFramework>netstandard1.6</TargetFramework>
<DefineConstants>$(DefineConstants);NO_DOTNETCORE_BOOTSTRAP</DefineConstants>
<DebugType>pdbonly</DebugType>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>Fake.DotNet.Testing.MSTest</AssemblyName>
<OutputType>Library</OutputType>
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netstandard1.6' ">$(PackageTargetFallback);portable-net45+win8;dnxcore50</PackageTargetFallback>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.6' ">
<DefineConstants>$(DefineConstants);NETSTANDARD;USE_HTTPCLIENT</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.fs" />
<Compile Include="MSTest.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fake.Core.Environment\Fake.Core.Environment.fsproj" />
<ProjectReference Include="..\Fake.Core.Tracing\Fake.Core.Tracing.fsproj" />
<ProjectReference Include="..\Fake.Core.Process\Fake.Core.Process.fsproj" />
<ProjectReference Include="..\Fake.Core.String\Fake.Core.String.fsproj" />
<ProjectReference Include="..\Fake.Core.Globbing\Fake.Core.Globbing.fsproj" />
<ProjectReference Include="..\Fake.IO.FileSystem\Fake.IO.FileSystem.fsproj" />
<ProjectReference Include="..\Fake.Core.BuildServer\Fake.Core.BuildServer.fsproj" />
<ProjectReference Include="..\Fake.Testing.Common\Fake.Testing.Common.fsproj">
<FromP2P>true</FromP2P>
</ProjectReference>
<ProjectReference Include="..\Fake.Core.Context\Fake.Core.Context.fsproj">
<FromP2P>true</FromP2P>
</ProjectReference>
</ItemGroup>
<!--<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
<Reference Include="mscorlib">
<FromP2P>true</FromP2P>
</Reference>
<Reference Include="System">
<FromP2P>true</FromP2P>
</Reference>
<Reference Include="System.Core">
<FromP2P>true</FromP2P>
</Reference>
</ItemGroup>-->
<ItemGroup>
<DotNetCliToolReference Include="dotnet-compile-fsc">
<Version>1.0.0-preview2-020000</Version>
</DotNetCliToolReference>
</ItemGroup>
<Import Project="..\..\..\.paket\Paket.Restore.targets" />
</Project>
118 changes: 118 additions & 0 deletions src/app/Fake.DotNet.Testing.MSTest/MSTest.fs
@@ -0,0 +1,118 @@
/// Contains tasks to run [MSTest](http://en.wikipedia.org/wiki/Visual_Studio_Unit_Testing_Framework/) unit tests.
module Fake.DotNet.Testing.MSTest

open System
open System.Text
open Fake.Core.String
open Fake.Core.Process
open Fake.Core
open Fake.Testing.Common

/// [omit]
let mstestPaths =
[| @"[ProgramFilesX86]\Microsoft Visual Studio 14.0\Common7\IDE";
@"[ProgramFilesX86]\Microsoft Visual Studio 12.0\Common7\IDE";
@"[ProgramFilesX86]\Microsoft Visual Studio 11.0\Common7\IDE";
@"[ProgramFilesX86]\Microsoft Visual Studio 10.0\Common7\IDE" |]

/// [omit]
let mstestexe =
if Environment.isMono then failwith "MSTest is not supported on mono platform"
else "mstest.exe"

// TODO: try to use VSTest.Console.exe as well (VS2012 and up only)
/// Option which allow to specify if a MSTest error should break the build.
type ErrorLevel = TestRunnerErrorLevel

/// Parameter type to configure the MSTest.exe.
[<CLIMutable>]
type MSTestParams =
{ /// 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.
Category : string
/// Test results directory (optional)
ResultsDir : string
/// Path to the Test Metadata file (.vsmdi) (optional)
TestMetadataPath : string
/// Path to the Test Settings file (.testsettings) (optional)
TestSettingsPath : string
/// Working directory (optional)
WorkingDir : string
/// List of tests be run (optional)
Tests : string list
/// A timeout for the test runner (optional)
TimeOut : TimeSpan
/// Path to MSTest.exe
ToolPath : string
/// Option which allow to specify if a MSTest error should break the build.
ErrorLevel : ErrorLevel
/// Run tests in isolation (optional).
NoIsolation : bool }

/// MSTest default parameters.
let MSTestDefaults =
{ Category = null
ResultsDir = null
TestMetadataPath = null
TestSettingsPath = null
WorkingDir = null
Tests = []
TimeOut = TimeSpan.FromMinutes 5.
ToolPath =
match tryFindFile mstestPaths mstestexe with
| Some path -> path
| None -> ""
ErrorLevel = ErrorLevel.Error
NoIsolation = true }

/// Builds the command line arguments from the given parameter record and the given assemblies.
/// [omit]
let buildMSTestArgs parameters assembly =
let testResultsFile =
if parameters.ResultsDir <> null then
sprintf @"%s\%s.trx" parameters.ResultsDir (DateTime.Now.ToString("yyyyMMdd-HHmmss.ff"))
else null

let builder =
new StringBuilder()
|> appendIfNotNull assembly "/testcontainer:"
|> appendIfNotNull parameters.Category "/category:"
|> appendIfNotNull parameters.TestMetadataPath "/testmetadata:"
|> appendIfNotNull parameters.TestSettingsPath "/testsettings:"
|> appendIfNotNull testResultsFile "/resultsfile:"
|> appendIfTrue parameters.NoIsolation "/noisolation"

parameters.Tests
|> List.iter (fun t -> builder |> appendIfNotNullOrEmpty t "/test:" |> ignore)

builder |> toText

/// Runs MSTest command line tool on a group of assemblies.
/// ## Parameters
///
/// - `setParams` - Function used to manipulate the default MSTestParams value.
/// - `assemblies` - Sequence of one or more assemblies containing Microsoft Visual Studio Unit Test Framework unit tests.
///
/// ## Sample usage
///
/// Target "Test" (fun _ ->
/// !! (testDir + @"\*.Tests.dll")
/// |> MSTest (fun p -> { p with Category = "group1" })
/// )
let MSTest (setParams : MSTestParams -> MSTestParams) (assemblies : string seq) =
let details = assemblies |> separated ", "
use __ = Trace.traceTask "MSTest" details
let parameters = MSTestDefaults |> setParams
let assemblies = assemblies |> Seq.toArray
if Array.isEmpty assemblies then failwith "MSTest: cannot run tests (the assembly list is empty)."
let failIfError assembly exitCode =
if exitCode > 0 && parameters.ErrorLevel <> ErrorLevel.DontFailBuild then
let message = sprintf "%sMSTest test run failed for %s" Environment.NewLine assembly
Trace.traceError message
failwith message
for assembly in assemblies do
let args = buildMSTestArgs parameters assembly
ExecProcess (fun info ->
info.FileName <- parameters.ToolPath
info.WorkingDirectory <- parameters.WorkingDir
info.Arguments <- args) parameters.TimeOut
|> failIfError assembly
4 changes: 4 additions & 0 deletions src/app/Fake.DotNet.Testing.MSTest/paket.references
@@ -0,0 +1,4 @@
group netcore
FSharp.NET.Sdk
FSharp.Core
NETStandard.Library
3 changes: 3 additions & 0 deletions src/app/FakeLib/FakeLib.fsproj
Expand Up @@ -306,6 +306,9 @@
<Compile Include="UnitTest\MSTest.fs" />
<Compile Include="UnitTest\ProcessTestRunner.fs" />
<Compile Include="UnitTest\VSTest.fs" />
<Compile Include="..\Fake.DotNet.Testing.MSTest\MSTest.fs">
<Link>Fake.DotNet.Testing.MSTest/MSTest.fs</Link>
</Compile>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move that up to the other "Modules"? This prevents accidental usage of "old" members (and makes the IDE help you)

<Compile Include="AssemblyInfoFile.fs" />
<Compile Include="AssemblyInfoHelper.fs" />
<Compile Include="FXCopHelper.fs" />
Expand Down
32 changes: 24 additions & 8 deletions src/app/FakeLib/UnitTest/MSTest.fs
Expand Up @@ -5,22 +5,26 @@ open System
open System.Text

/// [omit]
[<System.Obsolete("use Fake.DotNet.Testing.MSTest instead")>]
let mstestPaths =
[| @"[ProgramFilesX86]\Microsoft Visual Studio 14.0\Common7\IDE";
@"[ProgramFilesX86]\Microsoft Visual Studio 12.0\Common7\IDE";
@"[ProgramFilesX86]\Microsoft Visual Studio 11.0\Common7\IDE";
@"[ProgramFilesX86]\Microsoft Visual Studio 10.0\Common7\IDE" |]

/// [omit]
[<System.Obsolete("use Fake.DotNet.Testing.MSTest instead")>]
let mstestexe =
if isMono then failwith "MSTest is not supported on mono platform"
else "mstest.exe"

// TODO: try to use VSTest.Console.exe as well (VS2012 and up only)
/// Option which allow to specify if a MSTest error should break the build.
[<System.Obsolete("use Fake.DotNet.Testing.MSTest instead")>]
type ErrorLevel = TestRunnerErrorLevel

/// Parameter type to configure the MSTest.exe.
[<System.Obsolete("use Fake.DotNet.Testing.MSTest instead")>]
[<CLIMutable>]
type MSTestParams =
{ /// 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.
Expand All @@ -33,6 +37,8 @@ type MSTestParams =
TestSettingsPath : string
/// Working directory (optional)
WorkingDir : string
/// List of tests be run (optional)
Tests : string list
/// A timeout for the test runner (optional)
TimeOut : TimeSpan
/// Path to MSTest.exe
Expand All @@ -43,12 +49,14 @@ type MSTestParams =
NoIsolation : bool }

/// MSTest default parameters.
[<System.Obsolete("use Fake.DotNet.Testing.MSTest instead")>]
let MSTestDefaults =
{ Category = null
ResultsDir = null
TestMetadataPath = null
TestSettingsPath = null
WorkingDir = null
Tests = []
TimeOut = TimeSpan.FromMinutes 5.
ToolPath =
match tryFindFile mstestPaths mstestexe with
Expand All @@ -59,19 +67,26 @@ let MSTestDefaults =

/// Builds the command line arguments from the given parameter record and the given assemblies.
/// [omit]
[<System.Obsolete("use Fake.DotNet.Testing.MSTest instead")>]
let buildMSTestArgs parameters assembly =
let testResultsFile =
if parameters.ResultsDir <> null then
sprintf @"%s\%s.trx" parameters.ResultsDir (DateTime.Now.ToString("yyyyMMdd-HHmmss.ff"))
else null
new StringBuilder()
|> appendIfNotNull assembly "/testcontainer:"
|> appendIfNotNull parameters.Category "/category:"
|> appendIfNotNull parameters.TestMetadataPath "/testmetadata:"
|> appendIfNotNull parameters.TestSettingsPath "/testsettings:"
|> appendIfNotNull testResultsFile "/resultsfile:"
|> appendIfTrue parameters.NoIsolation "/noisolation"
|> toText

let builder =
new StringBuilder()
|> appendIfNotNull assembly "/testcontainer:"
|> appendIfNotNull parameters.Category "/category:"
|> appendIfNotNull parameters.TestMetadataPath "/testmetadata:"
|> appendIfNotNull parameters.TestSettingsPath "/testsettings:"
|> appendIfNotNull testResultsFile "/resultsfile:"
|> appendIfTrue parameters.NoIsolation "/noisolation"

parameters.Tests
|> List.iter (fun t -> builder |> appendIfNotNullOrEmpty t "/test:" |> ignore)

builder |> toText

/// Runs MSTest command line tool on a group of assemblies.
/// ## Parameters
Expand All @@ -85,6 +100,7 @@ let buildMSTestArgs parameters assembly =
/// !! (testDir + @"\*.Tests.dll")
/// |> MSTest (fun p -> { p with Category = "group1" })
/// )
[<System.Obsolete("use Fake.DotNet.Testing.MSTest instead")>]
let MSTest (setParams : MSTestParams -> MSTestParams) (assemblies : string seq) =
let details = assemblies |> separated ", "
use __ = traceStartTaskUsing "MSTest" details
Expand Down