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