Merge pull request #4301 from Faizan2304/clitest3verb

Implementation of test3 verb
This commit is contained in:
Livar 2016-10-12 11:57:18 -07:00 committed by GitHub
commit 3e1eb008e2
12 changed files with 406 additions and 22 deletions

View file

@ -172,6 +172,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "dotnet-msbuild.Tests", "tes
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "dotnet-publish3.Tests", "test\dotnet-publish3.Tests\dotnet-publish3.Tests.xproj", "{FBE4F1D6-BA4C-46D9-8286-4EE4B032D01E}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "dotnet-test3.Tests", "test\dotnet-test3.Tests\dotnet-test3.Tests.xproj", "{90236A80-4B84-41D3-A161-AA20709776B1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -1000,6 +1002,22 @@ Global
{FBE4F1D6-BA4C-46D9-8286-4EE4B032D01E}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU
{FBE4F1D6-BA4C-46D9-8286-4EE4B032D01E}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU
{FBE4F1D6-BA4C-46D9-8286-4EE4B032D01E}.RelWithDebInfo|x64.Build.0 = Release|Any CPU
{90236A80-4B84-41D3-A161-AA20709776B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{90236A80-4B84-41D3-A161-AA20709776B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{90236A80-4B84-41D3-A161-AA20709776B1}.Debug|x64.ActiveCfg = Debug|Any CPU
{90236A80-4B84-41D3-A161-AA20709776B1}.Debug|x64.Build.0 = Debug|Any CPU
{90236A80-4B84-41D3-A161-AA20709776B1}.MinSizeRel|Any CPU.ActiveCfg = Debug|Any CPU
{90236A80-4B84-41D3-A161-AA20709776B1}.MinSizeRel|Any CPU.Build.0 = Debug|Any CPU
{90236A80-4B84-41D3-A161-AA20709776B1}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU
{90236A80-4B84-41D3-A161-AA20709776B1}.MinSizeRel|x64.Build.0 = Debug|Any CPU
{90236A80-4B84-41D3-A161-AA20709776B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{90236A80-4B84-41D3-A161-AA20709776B1}.Release|Any CPU.Build.0 = Release|Any CPU
{90236A80-4B84-41D3-A161-AA20709776B1}.Release|x64.ActiveCfg = Release|Any CPU
{90236A80-4B84-41D3-A161-AA20709776B1}.Release|x64.Build.0 = Release|Any CPU
{90236A80-4B84-41D3-A161-AA20709776B1}.RelWithDebInfo|Any CPU.ActiveCfg = Release|Any CPU
{90236A80-4B84-41D3-A161-AA20709776B1}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU
{90236A80-4B84-41D3-A161-AA20709776B1}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU
{90236A80-4B84-41D3-A161-AA20709776B1}.RelWithDebInfo|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -1064,5 +1082,6 @@ Global
{9F5AE280-A040-4160-9799-6504D907742D} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7}
{2FFCBDF0-BA36-4393-8DDB-1AE04AEA3CD6} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7}
{FBE4F1D6-BA4C-46D9-8286-4EE4B032D01E} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7}
{90236A80-4B84-41D3-A161-AA20709776B1} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7}
EndGlobalSection
EndGlobal

View file

@ -2,6 +2,14 @@
namespace TestNamespace
{
public class Program
{
public static void Main(string[] args)
{
}
}
[TestClass]
public class VSTestTests
{
@ -9,11 +17,11 @@ namespace TestNamespace
public void VSTestPassTest()
{
}
[TestMethod]
[TestMethod]
public void VSTestFailTest()
{
Assert.Fail();
Assert.Fail();
}
}
}

View file

@ -3,14 +3,13 @@
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
<PropertyGroup>
<OutputType>Library</OutputType>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp1.0</TargetFrameworks>
<OutputPath>bin\$(Configuration)</OutputPath>
</PropertyGroup>
<ItemGroup>
<Compile Include="**\*.cs" />
<None Include="project.json" />
</ItemGroup>
<ItemGroup>
@ -21,20 +20,15 @@
<Version>1.0.0-alpha-20161010-1</Version>
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="MSTest.TestAdapter">
<Version>1.0.3-preview</Version>
</PackageReference>
<PackageReference Include="MSTest.TestFramework">
<PackageReference Include="MSTest.TestFramework">
<Version>1.0.4-preview</Version>
</PackageReference>
<PackageReference Include="MSTest.TestAdapter">
<Version>1.1.3-preview</Version>
</PackageReference>
<PackageReference Include="Microsoft.TestPlatform.TestHost">
<Version>15.0.0-preview-20161005-01</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft.CSharp.targets" />
<Target Name="AfterBuild">
<Copy SourceFiles="$(NuGetPackageRoot)\mstest.testadapter\1.0.3-preview\build\_common\Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.dll" DestinationFolder="$(OutputPath)" SkipUnchangedFiles="true"/>
<Copy SourceFiles="$(NuGetPackageRoot)\mstest.testadapter\1.0.3-preview\build\dotnet\Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.dll" DestinationFolder="$(OutputPath)" SkipUnchangedFiles="true"/>
<Copy SourceFiles="$(NuGetPackageRoot)\mstest.testadapter\1.0.3-preview\build\_common\Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.dll" DestinationFolder="$(OutputPath)" SkipUnchangedFiles="true"/>
<Copy SourceFiles="$(NuGetPackageRoot)\mstest.testframework\1.0.4-preview\lib\dotnet\Microsoft.VisualStudio.TestPlatform.TestFramework.dll" DestinationFolder="$(OutputPath)" SkipUnchangedFiles="true"/>
<Copy SourceFiles="$(NuGetPackageRoot)\mstest.testframework\1.0.4-preview\lib\dotnet\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll" DestinationFolder="$(OutputPath)" SkipUnchangedFiles="true"/>
</Target>
</Project>

View file

@ -0,0 +1,19 @@
<!--
***********************************************************************************************
Microsoft.TestPlatform.ImportAfter.targets
WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and have
created a backup copy. Incorrect changes to this file will make it
impossible to load or build your projects from the command-line or the IDE.
Copyright (c) .NET Foundation. All rights reserved.
***********************************************************************************************
-->
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Import Microsoft.TestPlatform.targets for VSTest target -->
<PropertyGroup>
<VSTestTargets Condition="'$(VSTestTargets)'==''">$(MSBuildExtensionsPath)\Microsoft.TestPlatform.targets</VSTestTargets>
</PropertyGroup>
<Import Condition="Exists('$(VSTestTargets)')" Project="$(VSTestTargets)" />
</Project>

View file

@ -29,6 +29,7 @@ using Microsoft.DotNet.Tools.Restore3;
using Microsoft.DotNet.Tools.Run;
using Microsoft.DotNet.Tools.Test;
using Microsoft.DotNet.Tools.VSTest;
using Microsoft.DotNet.Tools.Test3;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Cli
@ -54,6 +55,7 @@ namespace Microsoft.DotNet.Cli
["restore3"] = Restore3Command.Run,
["publish3"] = Publish3Command.Run,
["vstest"] = VSTestCommand.Run,
["test3"] = Test3Command.Run,
["pack3"] = Pack3Command.Run,
["migrate"] = MigrateCommand.Run,
["projectmodel-server"] = ProjectModelServerCommand.Run,

View file

@ -0,0 +1,229 @@
// Copyright(c) .NET Foundation and contributors.All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.MSBuild;
using System.IO;
using System.Text.RegularExpressions;
namespace Microsoft.DotNet.Tools.Test3
{
public class Test3Command
{
public static int Run(string[] args)
{
DebugHelper.HandleDebugSwitch(ref args);
var cmd = new CommandLineApplication(throwOnUnexpectedArg: false)
{
Name = "dotnet test3",
FullName = ".NET Test Driver",
Description = "Test Driver for the .NET Platform"
};
cmd.HelpOption("-h|--help");
var argRoot = cmd.Argument(
"<PROJECT>",
"The project to test, defaults to the current directory.",
multipleValues: false);
var settingOption = cmd.Option(
"-s|--settings <SettingsFile>",
"Settings to use when running tests.",
CommandOptionType.SingleValue);
var listTestsOption = cmd.Option(
"-lt|--listTests",
@"Lists discovered tests",
CommandOptionType.NoValue);
var testCaseFilterOption = cmd.Option(
"-tcf|--testCaseFilter <Expression>",
@"Run tests that match the given expression.
Examples:
--testCaseFilter:""Priority = 1""
--testCaseFilter: ""(FullyQualifiedName~Nightly | Name = MyTestMethod)""",
CommandOptionType.SingleValue);
var testAdapterPathOption = cmd.Option(
"-tap|--testAdapterPath",
@"Use custom adapters from the given path in the test run.
Example: --testAdapterPath:<pathToCustomAdapters>",
CommandOptionType.SingleValue);
var loggerOption = cmd.Option(
"-l|--logger <LoggerUri/FriendlyName>",
@"Specify a logger for test results.
Example: --logger:trx",
CommandOptionType.SingleValue);
var configurationOption = cmd.Option(
"-c|--Configuration <Configuration>",
@"Configuration under which to build, i.e. Debug/Release",
CommandOptionType.SingleValue);
var frameworkOption = cmd.Option(
"-f|--framework <FrameworkVersion>",
@"Looks for test binaries for a specific framework",
CommandOptionType.SingleValue);
var outputOption = cmd.Option(
"-o|--output <OotputDir>",
@"Directory in which to find the binaries to be run",
CommandOptionType.SingleValue);
var noBuildtOption = cmd.Option(
"--noBuild",
@"Do not build project before testing.",
CommandOptionType.NoValue);
cmd.OnExecute(() =>
{
var msbuildArgs = new List<string>()
{
"/t:VSTest"
};
msbuildArgs.Add("/verbosity:quiet");
msbuildArgs.Add("/nologo");
if (settingOption.HasValue())
{
msbuildArgs.Add($"/p:VSTestSetting={settingOption.Value()}");
}
if (listTestsOption.HasValue())
{
msbuildArgs.Add($"/p:VSTestListTests=true");
}
if (testCaseFilterOption.HasValue())
{
msbuildArgs.Add($"/p:VSTestTestCaseFilter={testCaseFilterOption.Value()}");
}
if (testAdapterPathOption.HasValue())
{
msbuildArgs.Add($"/p:VSTestTestAdapterPath={testAdapterPathOption.Value()}");
}
if (loggerOption.HasValue())
{
msbuildArgs.Add($"/p:VSTestLogger={string.Join(";", loggerOption.Values)}");
}
if (configurationOption.HasValue())
{
msbuildArgs.Add($"/p:Configuration={configurationOption.Value()}");
}
if (frameworkOption.HasValue())
{
msbuildArgs.Add($"/p:TargetFramework={frameworkOption.Value()}");
}
if (outputOption.HasValue())
{
msbuildArgs.Add($"/p:OutputPath={outputOption.Value()}");
}
if (noBuildtOption.HasValue())
{
msbuildArgs.Add($"/p:VSTestNoBuild=true");
}
string defaultproject = GetSingleTestProjectToRunTestIfNotProvided(argRoot.Value, cmd.RemainingArguments);
if(!string.IsNullOrEmpty(defaultproject))
{
msbuildArgs.Add(defaultproject);
}
if (!string.IsNullOrEmpty(argRoot.Value))
{
msbuildArgs.Add(argRoot.Value);
}
// Add remaining arguments that the parser did not understand,
msbuildArgs.AddRange(cmd.RemainingArguments);
return new MSBuildForwardingApp(msbuildArgs).Execute();
});
return cmd.Execute(args);
}
private static string GetSingleTestProjectToRunTestIfNotProvided(string args, List<string> remainingArguments)
{
string result = string.Empty;
int projectFound = NumberOfTestProjectInRemainingArgs(remainingArguments) + NumberOfTestProjectInArgsRoot(args);
if (projectFound > 1)
{
throw new GracefulException(
$"Specify a single project file to run tests from.");
}
else if (projectFound == 0)
{
result = GetDefaultTestProject();
}
return result;
}
private static int NumberOfTestProjectInArgsRoot(string args)
{
Regex pattern = new Regex(@"^.*\..*proj$");
if (!string.IsNullOrEmpty(args))
{
return pattern.IsMatch(args) ? 1 : 0;
}
return 0;
}
private static int NumberOfTestProjectInRemainingArgs(List<string> remainingArguments)
{
int count = 0;
if (remainingArguments.Count != 0)
{
Regex pattern = new Regex(@"^.*\..*proj$");
foreach (var x in remainingArguments)
{
if (pattern.IsMatch(x))
{
count++;
}
}
}
return count;
}
private static string GetDefaultTestProject()
{
string directory = Directory.GetCurrentDirectory();
string[] projectFiles = Directory.GetFiles(directory, "*.*proj");
if (projectFiles.Length == 0)
{
throw new GracefulException(
$"Couldn't find a project to run test from. Ensure a project exists in {directory}." + Environment.NewLine +
"Or pass the path to the project");
}
else if (projectFiles.Length > 1)
{
throw new GracefulException(
$"Specify which project file to use because this '{directory}' contains more than one project file.");
}
return projectFiles[0];
}
}
}

View file

@ -23,7 +23,8 @@
"Microsoft.CodeAnalysis.Build.Tasks": "2.0.0-beta6-60922-08",
"System.Runtime.Serialization.Xml": "4.1.1",
"NuGet.Build.Tasks": "3.6.0-rc-1979",
"Microsoft.TestPlatform.CLI": "15.0.0-preview-20160915-01"
"Microsoft.TestPlatform.CLI": "15.0.0-preview-20161005-01",
"Microsoft.TestPlatform.Build": "15.0.0-preview-20161005-01"
},
"frameworks": {
"netcoreapp1.0": {

View file

@ -0,0 +1,27 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.DotNet.Cli.Utils;
namespace Microsoft.DotNet.Tools.Test.Utilities
{
public sealed class Test3Command : TestCommand
{
public Test3Command()
: base("dotnet")
{
}
public override CommandResult Execute(string args = "")
{
args = $"test3 {args}";
return base.Execute(args);
}
public override CommandResult ExecuteWithCapturedOutput(string args = "")
{
args = $"test3 {args}";
return base.ExecuteWithCapturedOutput(args);
}
}
}

View file

@ -0,0 +1,39 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.DotNet.Tools.Test.Utilities;
using Xunit;
using FluentAssertions;
using Microsoft.DotNet.TestFramework;
using Microsoft.DotNet.Cli.Utils;
namespace Microsoft.DotNet.Cli.Test3.Tests
{
public class GivenDotnetTest3BuildsAndRunsTestfromCsproj : TestBase
{
[Fact]
public void TestsFromAGivenProjectShouldRunWithExpectedOutput()
{
// Copy DotNetCoreTestProject project in output directory of project dotnet-vstest.Tests
string testAppName = "VSTestDotNetCoreProject";
TestInstance testInstance = TestAssetsManager.CreateTestInstance(testAppName);
string testProjectDirectory = testInstance.TestRoot;
// Restore project VSTestDotNetCoreProject
new Restore3Command()
.WithWorkingDirectory(testProjectDirectory)
.Execute()
.Should()
.Pass();
// Call test3
CommandResult result = new Test3Command().WithWorkingDirectory(testProjectDirectory).ExecuteWithCapturedOutput("/p:TargetFramework=netcoreapp1.0");
// Verify
result.StdOut.Should().Contain("Total tests: 2. Passed: 1. Failed: 1. Skipped: 0.");
result.StdOut.Should().Contain("Passed TestNamespace.VSTestTests.VSTestPassTest");
result.StdOut.Should().Contain("Failed TestNamespace.VSTestTests.VSTestFailTest");
}
}
}

View file

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0.23107" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0.23107</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>90236a80-4b84-41d3-a161-aa20709776b1</ProjectGuid>
<RootNamespace>Microsoft.DotNet.Cli.Test3.Tests</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View file

@ -0,0 +1,24 @@
{
"version": "1.0.0-*",
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
},
"System.Runtime.Serialization.Primitives": "4.1.1",
"Microsoft.DotNet.Tools.Tests.Utilities": {
"target": "project"
},
"xunit": "2.2.0-beta3-build3330",
"dotnet-test-xunit": "1.0.0-rc2-330423-54"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.4",
"portable-net451+win8"
]
}
},
"testRunner": "xunit"
}

View file

@ -11,24 +11,25 @@ using Microsoft.DotNet.Cli.Utils;
namespace Microsoft.DotNet.Cli.VSTest.Tests
{
public class VSTestTests: TestBase
public class VSTestTests : TestBase
{
[Fact]
public void TestsFromAGivenContainerShouldRunWithExpectedOutput()
{
// Copy DotNetCoreTestProject project in output directory of project dotnet-vstest.Tests
string testAppName = "VSTestDotNetCoreProject";
TestInstance testInstance = TestAssetsManager.CreateTestInstance(testAppName).WithLockFiles();
TestInstance testInstance = TestAssetsManager.CreateTestInstance(testAppName);
string testProjectDirectory = testInstance.TestRoot;
// Restore project VSTestDotNetCoreProject
new Restore3Command()
.WithWorkingDirectory(testProjectDirectory)
.Execute()
.Should()
.Pass();
// Build project DotNetCoreTestProject
// Build project VSTestDotNetCoreProject
new Build3Command()
.WithWorkingDirectory(testProjectDirectory)
.Execute()
@ -39,7 +40,7 @@ namespace Microsoft.DotNet.Cli.VSTest.Tests
string configuration = Environment.GetEnvironmentVariable("CONFIGURATION") ?? "Debug";
string testAdapterPath = Path.Combine(testProjectDirectory, "bin", configuration, "netcoreapp1.0");
string outputDll = Path.Combine(testAdapterPath, $"{testAppName}.dll");
string argsForVstest = string.Concat("\"", outputDll, "\"", " --TestAdapterPath:", "\"", testAdapterPath, "\"");
string argsForVstest = string.Concat("\"", outputDll, "\"");
// Call vstest
CommandResult result = new VSTestCommand().ExecuteWithCapturedOutput(argsForVstest);