2016-10-04 10:07:36 +00:00
|
|
|
|
// 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;
|
2016-10-25 14:40:18 +00:00
|
|
|
|
using System.IO;
|
2016-10-25 15:27:59 +00:00
|
|
|
|
using System;
|
2017-04-17 14:31:03 +00:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2016-10-04 10:07:36 +00:00
|
|
|
|
|
2016-10-28 14:45:38 +00:00
|
|
|
|
namespace Microsoft.DotNet.Cli.Test.Tests
|
2016-10-04 10:07:36 +00:00
|
|
|
|
{
|
2016-10-28 01:46:43 +00:00
|
|
|
|
public class GivenDotnettestBuildsAndRunsTestfromCsproj : TestBase
|
2016-10-04 10:07:36 +00:00
|
|
|
|
{
|
2016-11-01 21:31:13 +00:00
|
|
|
|
[Fact]
|
2016-11-02 18:08:20 +00:00
|
|
|
|
public void MSTestSingleTFM()
|
2016-10-04 10:07:36 +00:00
|
|
|
|
{
|
2017-05-03 04:30:51 +00:00
|
|
|
|
var testProjectDirectory = this.CopyAndRestoreVSTestDotNetCoreTestApp("3");
|
2016-10-04 10:07:36 +00:00
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
// Call test
|
|
|
|
|
CommandResult result = new DotnetTestCommand()
|
|
|
|
|
.WithWorkingDirectory(testProjectDirectory)
|
2017-02-18 17:35:33 +00:00
|
|
|
|
.ExecuteWithCapturedOutput(TestBase.ConsoleLoggerOutputNormal);
|
2016-10-04 10:07:36 +00:00
|
|
|
|
|
|
|
|
|
// Verify
|
2017-06-13 01:32:31 +00:00
|
|
|
|
if (!DotnetUnderTest.IsLocalized())
|
|
|
|
|
{
|
|
|
|
|
result.StdOut.Should().Contain("Total tests: 2. Passed: 1. Failed: 1. Skipped: 0.");
|
2017-10-11 06:51:59 +00:00
|
|
|
|
result.StdOut.Should().Contain("Passed VSTestPassTest");
|
|
|
|
|
result.StdOut.Should().Contain("Failed VSTestFailTest");
|
2017-06-13 01:32:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-16 10:09:52 +00:00
|
|
|
|
result.ExitCode.Should().Be(1);
|
2016-10-04 10:07:36 +00:00
|
|
|
|
}
|
2016-10-25 14:40:18 +00:00
|
|
|
|
|
2017-06-03 06:32:53 +00:00
|
|
|
|
[Fact]
|
|
|
|
|
public void ItImplicitlyRestoresAProjectWhenTesting()
|
|
|
|
|
{
|
|
|
|
|
string testAppName = "VSTestCore";
|
|
|
|
|
var testInstance = TestAssets.Get(testAppName)
|
|
|
|
|
.CreateInstance()
|
|
|
|
|
.WithSourceFiles();
|
|
|
|
|
|
|
|
|
|
var testProjectDirectory = testInstance.Root.FullName;
|
|
|
|
|
|
|
|
|
|
CommandResult result = new DotnetTestCommand()
|
|
|
|
|
.WithWorkingDirectory(testProjectDirectory)
|
|
|
|
|
.ExecuteWithCapturedOutput(TestBase.ConsoleLoggerOutputNormal);
|
|
|
|
|
|
2017-06-13 01:32:31 +00:00
|
|
|
|
if (!DotnetUnderTest.IsLocalized())
|
|
|
|
|
{
|
|
|
|
|
result.StdOut.Should().Contain("Total tests: 2. Passed: 1. Failed: 1. Skipped: 0.");
|
2017-10-11 06:51:59 +00:00
|
|
|
|
result.StdOut.Should().Contain("Passed VSTestPassTest");
|
|
|
|
|
result.StdOut.Should().Contain("Failed VSTestFailTest");
|
2017-06-13 01:32:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-03 06:32:53 +00:00
|
|
|
|
result.ExitCode.Should().Be(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ItDoesNotImplicitlyRestoreAProjectWhenTestingWithTheNoRestoreOption()
|
|
|
|
|
{
|
|
|
|
|
string testAppName = "VSTestCore";
|
|
|
|
|
var testInstance = TestAssets.Get(testAppName)
|
|
|
|
|
.CreateInstance()
|
|
|
|
|
.WithSourceFiles();
|
|
|
|
|
|
|
|
|
|
var testProjectDirectory = testInstance.Root.FullName;
|
|
|
|
|
|
|
|
|
|
new DotnetTestCommand()
|
|
|
|
|
.WithWorkingDirectory(testProjectDirectory)
|
|
|
|
|
.ExecuteWithCapturedOutput($"{TestBase.ConsoleLoggerOutputNormal} --no-restore")
|
|
|
|
|
.Should().Fail()
|
2017-06-24 02:48:07 +00:00
|
|
|
|
.And.HaveStdOutContaining("project.assets.json");
|
2017-06-03 06:32:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 21:31:13 +00:00
|
|
|
|
[Fact]
|
2016-11-02 18:08:20 +00:00
|
|
|
|
public void XunitSingleTFM()
|
2016-11-01 20:15:44 +00:00
|
|
|
|
{
|
2017-05-03 04:30:51 +00:00
|
|
|
|
// Copy XunitCore project in output directory of project dotnet-vstest.Tests
|
|
|
|
|
string testAppName = "XunitCore";
|
2017-02-15 23:35:03 +00:00
|
|
|
|
var testInstance = TestAssets.Get(testAppName)
|
2017-05-03 04:30:51 +00:00
|
|
|
|
.CreateInstance("4")
|
2017-02-15 23:35:03 +00:00
|
|
|
|
.WithSourceFiles();
|
2016-11-01 20:15:44 +00:00
|
|
|
|
|
2017-02-15 23:35:03 +00:00
|
|
|
|
var testProjectDirectory = testInstance.Root.FullName;
|
2016-11-01 20:15:44 +00:00
|
|
|
|
|
2017-05-03 04:30:51 +00:00
|
|
|
|
// Restore project XunitCore
|
2016-11-01 20:15:44 +00:00
|
|
|
|
new RestoreCommand()
|
|
|
|
|
.WithWorkingDirectory(testProjectDirectory)
|
|
|
|
|
.Execute()
|
|
|
|
|
.Should()
|
|
|
|
|
.Pass();
|
|
|
|
|
|
|
|
|
|
// Call test
|
|
|
|
|
CommandResult result = new DotnetTestCommand()
|
|
|
|
|
.WithWorkingDirectory(testProjectDirectory)
|
2017-02-18 17:35:33 +00:00
|
|
|
|
.ExecuteWithCapturedOutput(TestBase.ConsoleLoggerOutputNormal);
|
2016-11-01 20:15:44 +00:00
|
|
|
|
|
|
|
|
|
// Verify
|
2017-06-13 01:32:31 +00:00
|
|
|
|
if (!DotnetUnderTest.IsLocalized())
|
|
|
|
|
{
|
|
|
|
|
result.StdOut.Should().Contain("Total tests: 2. Passed: 1. Failed: 1. Skipped: 0.");
|
|
|
|
|
result.StdOut.Should().Contain("Passed TestNamespace.VSTestXunitTests.VSTestXunitPassTest");
|
|
|
|
|
result.StdOut.Should().Contain("Failed TestNamespace.VSTestXunitTests.VSTestXunitFailTest");
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-16 10:09:52 +00:00
|
|
|
|
result.ExitCode.Should().Be(1);
|
2016-11-01 20:15:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-12 00:19:26 +00:00
|
|
|
|
[Fact]
|
|
|
|
|
public void GivenAFailingTestItDisplaysFailureDetails()
|
|
|
|
|
{
|
|
|
|
|
var testInstance = TestAssets.Get("XunitCore")
|
|
|
|
|
.CreateInstance()
|
|
|
|
|
.WithSourceFiles();
|
|
|
|
|
|
|
|
|
|
var result = new DotnetTestCommand()
|
|
|
|
|
.WithWorkingDirectory(testInstance.Root.FullName)
|
|
|
|
|
.ExecuteWithCapturedOutput();
|
|
|
|
|
|
|
|
|
|
result.ExitCode.Should().Be(1);
|
|
|
|
|
|
|
|
|
|
if (!DotnetUnderTest.IsLocalized())
|
|
|
|
|
{
|
|
|
|
|
result.StdOut.Should().Contain("Failed TestNamespace.VSTestXunitTests.VSTestXunitFailTest");
|
|
|
|
|
result.StdOut.Should().Contain("Assert.Equal() Failure");
|
|
|
|
|
result.StdOut.Should().Contain("Total tests: 2. Passed: 1. Failed: 1. Skipped: 0.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 21:31:13 +00:00
|
|
|
|
[Fact]
|
2016-10-25 14:40:18 +00:00
|
|
|
|
public void TestWillNotBuildTheProjectIfNoBuildArgsIsGiven()
|
|
|
|
|
{
|
2017-05-03 04:30:51 +00:00
|
|
|
|
// Copy and restore VSTestCore project in output directory of project dotnet-vstest.Tests
|
|
|
|
|
var testProjectDirectory = this.CopyAndRestoreVSTestDotNetCoreTestApp("5");
|
2016-10-25 15:27:59 +00:00
|
|
|
|
string configuration = Environment.GetEnvironmentVariable("CONFIGURATION") ?? "Debug";
|
2016-10-26 11:23:34 +00:00
|
|
|
|
string expectedError = Path.Combine(testProjectDirectory, "bin",
|
2017-08-18 22:36:01 +00:00
|
|
|
|
configuration, "netcoreapp2.1", "VSTestCore.dll");
|
2016-10-26 11:23:34 +00:00
|
|
|
|
expectedError = "The test source file " + "\"" + expectedError + "\"" + " provided was not found.";
|
2016-10-25 14:40:18 +00:00
|
|
|
|
|
2016-11-01 16:31:30 +00:00
|
|
|
|
// Call test
|
2016-10-28 14:45:38 +00:00
|
|
|
|
CommandResult result = new DotnetTestCommand()
|
2016-10-26 11:23:34 +00:00
|
|
|
|
.WithWorkingDirectory(testProjectDirectory)
|
2017-11-26 00:45:38 +00:00
|
|
|
|
.ExecuteWithCapturedOutput("--no-build -v:m");
|
2016-10-25 14:40:18 +00:00
|
|
|
|
|
|
|
|
|
// Verify
|
2017-06-13 01:32:31 +00:00
|
|
|
|
if (!DotnetUnderTest.IsLocalized())
|
|
|
|
|
{
|
2017-11-26 00:45:38 +00:00
|
|
|
|
result.StdOut.Should().NotContain("Restore");
|
2017-06-13 01:32:31 +00:00
|
|
|
|
result.StdErr.Should().Contain(expectedError);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.ExitCode.Should().Be(1);
|
2016-10-25 14:40:18 +00:00
|
|
|
|
}
|
2016-11-24 08:48:54 +00:00
|
|
|
|
|
|
|
|
|
[Fact]
|
2017-03-25 09:48:14 +00:00
|
|
|
|
public void TestWillCreateTrxLoggerInTheSpecifiedResultsDirectoryBySwitch()
|
2016-12-30 10:17:15 +00:00
|
|
|
|
{
|
2017-05-03 04:30:51 +00:00
|
|
|
|
// Copy and restore VSTestCore project in output directory of project dotnet-vstest.Tests
|
|
|
|
|
var testProjectDirectory = this.CopyAndRestoreVSTestDotNetCoreTestApp("6");
|
2016-11-24 08:48:54 +00:00
|
|
|
|
|
2017-05-03 04:30:51 +00:00
|
|
|
|
string trxLoggerDirectory = Path.Combine(testProjectDirectory, "TR", "x.y");
|
2016-11-24 08:48:54 +00:00
|
|
|
|
|
|
|
|
|
// Delete trxLoggerDirectory if it exist
|
2017-02-15 23:35:03 +00:00
|
|
|
|
if (Directory.Exists(trxLoggerDirectory))
|
2016-11-24 08:48:54 +00:00
|
|
|
|
{
|
|
|
|
|
Directory.Delete(trxLoggerDirectory, true);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-25 09:48:14 +00:00
|
|
|
|
// Call test with trx logger enabled and results directory explicitly specified.
|
2016-11-24 08:48:54 +00:00
|
|
|
|
CommandResult result = new DotnetTestCommand()
|
|
|
|
|
.WithWorkingDirectory(testProjectDirectory)
|
2017-03-25 11:03:01 +00:00
|
|
|
|
.ExecuteWithCapturedOutput("--logger trx -r \"" + trxLoggerDirectory + "\"");
|
2016-11-24 08:48:54 +00:00
|
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
|
String[] trxFiles = Directory.GetFiles(trxLoggerDirectory, "*.trx");
|
|
|
|
|
Assert.Equal(1, trxFiles.Length);
|
|
|
|
|
result.StdOut.Should().Contain(trxFiles[0]);
|
2016-11-28 08:45:17 +00:00
|
|
|
|
|
|
|
|
|
// Cleanup trxLoggerDirectory if it exist
|
|
|
|
|
if(Directory.Exists(trxLoggerDirectory))
|
|
|
|
|
{
|
|
|
|
|
Directory.Delete(trxLoggerDirectory, true);
|
|
|
|
|
}
|
2016-11-24 08:48:54 +00:00
|
|
|
|
}
|
2016-12-18 08:45:25 +00:00
|
|
|
|
|
2016-12-30 05:54:03 +00:00
|
|
|
|
[Fact]
|
2017-03-25 09:48:14 +00:00
|
|
|
|
public void ItCreatesTrxReportInTheSpecifiedResultsDirectoryByArgs()
|
2016-12-30 05:54:03 +00:00
|
|
|
|
{
|
2017-05-03 04:30:51 +00:00
|
|
|
|
// Copy and restore VSTestCore project in output directory of project dotnet-vstest.Tests
|
|
|
|
|
var testProjectDirectory = this.CopyAndRestoreVSTestDotNetCoreTestApp("7");
|
2016-12-30 05:54:03 +00:00
|
|
|
|
|
2017-05-03 04:30:51 +00:00
|
|
|
|
string trxLoggerDirectory = Path.Combine(testProjectDirectory, "RD");
|
2016-12-30 05:54:03 +00:00
|
|
|
|
|
|
|
|
|
// Delete trxLoggerDirectory if it exist
|
|
|
|
|
if (Directory.Exists(trxLoggerDirectory))
|
|
|
|
|
{
|
|
|
|
|
Directory.Delete(trxLoggerDirectory, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Call test with logger enable
|
|
|
|
|
CommandResult result = new DotnetTestCommand()
|
|
|
|
|
.WithWorkingDirectory(testProjectDirectory)
|
2016-12-30 10:04:00 +00:00
|
|
|
|
.ExecuteWithCapturedOutput("--logger \"trx;logfilename=custom.trx\" -- RunConfiguration.ResultsDirectory=" + trxLoggerDirectory);
|
2016-12-30 05:54:03 +00:00
|
|
|
|
|
|
|
|
|
// Verify
|
2017-03-25 09:48:14 +00:00
|
|
|
|
var trxFilePath = Path.Combine(trxLoggerDirectory, "custom.trx");
|
|
|
|
|
Assert.True(File.Exists(trxFilePath));
|
|
|
|
|
result.StdOut.Should().Contain(trxFilePath);
|
2016-12-30 05:54:03 +00:00
|
|
|
|
|
|
|
|
|
// Cleanup trxLoggerDirectory if it exist
|
|
|
|
|
if (Directory.Exists(trxLoggerDirectory))
|
|
|
|
|
{
|
|
|
|
|
Directory.Delete(trxLoggerDirectory, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-03 04:30:51 +00:00
|
|
|
|
[Fact]
|
2016-12-18 08:45:25 +00:00
|
|
|
|
public void ItBuildsAndTestsAppWhenRestoringToSpecificDirectory()
|
|
|
|
|
{
|
2017-05-03 04:30:51 +00:00
|
|
|
|
// Creating folder with name short name "RestoreTest" to avoid PathTooLongException
|
|
|
|
|
var rootPath = TestAssets.Get("VSTestCore").CreateInstance("8").WithSourceFiles().Root.FullName;
|
2016-12-18 08:45:25 +00:00
|
|
|
|
|
2017-05-03 04:30:51 +00:00
|
|
|
|
// Moving pkgs folder on top to avoid PathTooLongException
|
|
|
|
|
string dir = @"..\..\..\..\pkgs";
|
2016-12-18 08:45:25 +00:00
|
|
|
|
string fullPath = Path.GetFullPath(Path.Combine(rootPath, dir));
|
|
|
|
|
|
|
|
|
|
string args = $"--packages \"{dir}\"";
|
|
|
|
|
new RestoreCommand()
|
|
|
|
|
.WithWorkingDirectory(rootPath)
|
|
|
|
|
.Execute(args)
|
|
|
|
|
.Should()
|
|
|
|
|
.Pass();
|
|
|
|
|
|
|
|
|
|
new BuildCommand()
|
|
|
|
|
.WithWorkingDirectory(rootPath)
|
2017-06-03 06:32:53 +00:00
|
|
|
|
.ExecuteWithCapturedOutput("--no-restore")
|
2016-12-18 08:45:25 +00:00
|
|
|
|
.Should()
|
|
|
|
|
.Pass()
|
|
|
|
|
.And.NotHaveStdErr();
|
|
|
|
|
|
|
|
|
|
CommandResult result = new DotnetTestCommand()
|
|
|
|
|
.WithWorkingDirectory(rootPath)
|
2017-06-03 06:32:53 +00:00
|
|
|
|
.ExecuteWithCapturedOutput($"{TestBase.ConsoleLoggerOutputNormal} --no-restore");
|
2016-12-18 08:45:25 +00:00
|
|
|
|
|
2017-06-13 01:32:31 +00:00
|
|
|
|
if (!DotnetUnderTest.IsLocalized())
|
|
|
|
|
{
|
|
|
|
|
result.StdOut.Should().Contain("Total tests: 2. Passed: 1. Failed: 1. Skipped: 0.");
|
2017-10-11 06:51:59 +00:00
|
|
|
|
result.StdOut.Should().Contain("Passed VSTestPassTest");
|
|
|
|
|
result.StdOut.Should().Contain("Failed VSTestFailTest");
|
2017-06-13 01:32:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.ExitCode.Should().Be(1);
|
2016-12-18 08:45:25 +00:00
|
|
|
|
}
|
2017-03-25 09:48:14 +00:00
|
|
|
|
|
2017-05-03 04:30:51 +00:00
|
|
|
|
[Fact]
|
|
|
|
|
public void ItUsesVerbosityPassedToDefineVerbosityOfConsoleLoggerOfTheTests()
|
|
|
|
|
{
|
|
|
|
|
// Copy and restore VSTestCore project in output directory of project dotnet-vstest.Tests
|
|
|
|
|
var testProjectDirectory = this.CopyAndRestoreVSTestDotNetCoreTestApp("9");
|
|
|
|
|
|
|
|
|
|
// Call test
|
|
|
|
|
CommandResult result = new DotnetTestCommand()
|
|
|
|
|
.WithWorkingDirectory(testProjectDirectory)
|
|
|
|
|
.ExecuteWithCapturedOutput("-v q");
|
|
|
|
|
|
|
|
|
|
// Verify
|
2017-06-13 01:32:31 +00:00
|
|
|
|
if (!DotnetUnderTest.IsLocalized())
|
|
|
|
|
{
|
|
|
|
|
result.StdOut.Should().Contain("Total tests: 2. Passed: 1. Failed: 1. Skipped: 0.");
|
|
|
|
|
result.StdOut.Should().NotContain("Passed TestNamespace.VSTestTests.VSTestPassTest");
|
|
|
|
|
result.StdOut.Should().NotContain("Failed TestNamespace.VSTestTests.VSTestFailTest");
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-03 04:30:51 +00:00
|
|
|
|
result.ExitCode.Should().Be(1);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-17 14:31:03 +00:00
|
|
|
|
private string CopyAndRestoreVSTestDotNetCoreTestApp([CallerMemberName] string callingMethod = "")
|
|
|
|
|
{
|
2017-05-03 04:30:51 +00:00
|
|
|
|
// Copy VSTestCore project in output directory of project dotnet-vstest.Tests
|
|
|
|
|
string testAppName = "VSTestCore";
|
2017-03-25 09:48:14 +00:00
|
|
|
|
var testInstance = TestAssets.Get(testAppName)
|
2017-04-17 14:31:03 +00:00
|
|
|
|
.CreateInstance(callingMethod)
|
2017-03-25 09:48:14 +00:00
|
|
|
|
.WithSourceFiles();
|
|
|
|
|
|
|
|
|
|
var testProjectDirectory = testInstance.Root.FullName;
|
|
|
|
|
|
2017-05-03 04:30:51 +00:00
|
|
|
|
// Restore project VSTestCore
|
2017-03-25 09:48:14 +00:00
|
|
|
|
new RestoreCommand()
|
|
|
|
|
.WithWorkingDirectory(testProjectDirectory)
|
|
|
|
|
.Execute()
|
|
|
|
|
.Should()
|
|
|
|
|
.Pass();
|
2017-06-03 06:32:53 +00:00
|
|
|
|
|
2017-03-25 09:48:14 +00:00
|
|
|
|
return testProjectDirectory;
|
|
|
|
|
}
|
2016-10-04 10:07:36 +00:00
|
|
|
|
}
|
2016-12-16 10:09:52 +00:00
|
|
|
|
}
|