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 ;
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
{
2016-11-03 00:14:03 +05:30
// Copy VSTestDotNetCore project in output directory of project dotnet-vstest.Tests
string testAppName = "VSTestDotNetCore" ;
2016-10-04 15:37:36 +05:30
TestInstance testInstance = TestAssetsManager . CreateTestInstance ( testAppName ) ;
string testProjectDirectory = testInstance . TestRoot ;
2016-11-03 00:14:03 +05:30
// Restore project VSTestDotNetCore
2016-10-27 18:46:43 -07:00
new RestoreCommand ( )
2016-10-04 15:37:36 +05:30
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( )
. Should ( )
. Pass ( ) ;
2016-10-27 18:46:43 -07:00
// Call test
CommandResult result = new DotnetTestCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
2016-11-02 01:45:44 +05:30
. ExecuteWithCapturedOutput ( ) ;
2016-10-04 15:37:36 +05:30
// 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" ) ;
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
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
{
2016-11-03 00:14:03 +05:30
// Copy VSTestXunitDotNetCore project in output directory of project dotnet-vstest.Tests
string testAppName = "VSTestXunitDotNetCore" ;
2016-11-02 01:45:44 +05:30
TestInstance testInstance = TestAssetsManager . CreateTestInstance ( testAppName ) ;
string testProjectDirectory = testInstance . TestRoot ;
2016-11-03 00:14:03 +05:30
// Restore project VSTestXunitDotNetCore
2016-11-02 01:45:44 +05:30
new RestoreCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( )
. Should ( )
. Pass ( ) ;
// Call test
CommandResult result = new DotnetTestCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. ExecuteWithCapturedOutput ( ) ;
// Verify
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
}
2016-11-02 03:01:13 +05:30
[Fact]
2016-10-25 20:10:18 +05:30
public void TestWillNotBuildTheProjectIfNoBuildArgsIsGiven ( )
{
2016-11-03 00:14:03 +05:30
// Copy VSTestDotNetCore project in output directory of project dotnet-vstest.Tests
string testAppName = "VSTestDotNetCore" ;
2016-10-25 20:10:18 +05:30
TestInstance testInstance = TestAssetsManager . CreateTestInstance ( testAppName ) ;
string testProjectDirectory = testInstance . TestRoot ;
2016-11-03 00:14:03 +05:30
// Restore project VSTestDotNetCore
2016-10-28 20:15:38 +05:30
new RestoreCommand ( )
2016-10-25 20:10:18 +05:30
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( )
. Should ( )
. Pass ( ) ;
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" ,
2016-11-03 00:14:03 +05:30
configuration , "netcoreapp1.0" , "VSTestDotNetCore.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 )
2016-12-12 23:29:29 -08:00
. ExecuteWithCapturedOutput ( "--no-build" ) ;
2016-10-25 20:10:18 +05:30
// Verify
2016-10-26 16:53:34 +05:30
result . StdOut . Should ( ) . Contain ( expectedError ) ;
2016-10-25 20:10:18 +05:30
}
2016-11-24 14:18:54 +05:30
[Fact]
public void TestWillCreateTrxLogger ( )
2016-12-30 15:47:15 +05:30
{
2016-11-24 14:18:54 +05:30
// Copy VSTestDotNetCore project in output directory of project dotnet-vstest.Tests
string testAppName = "VSTestDotNetCore" ;
TestInstance testInstance = TestAssetsManager . CreateTestInstance ( testAppName ) ;
string testProjectDirectory = testInstance . TestRoot ;
// Restore project VSTestDotNetCore
new RestoreCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( )
. Should ( )
. Pass ( ) ;
string trxLoggerDirectory = Path . Combine ( testProjectDirectory , "TestResults" ) ;
// 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" ) ;
// 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]
public void ItCreatesTrxReportInTheSpecifiedResultsDirectory ( )
{
// Copy VSTestDotNetCore project in output directory of project dotnet-vstest.Tests
string testAppName = "VSTestDotNetCore" ;
TestInstance testInstance = TestAssetsManager . CreateTestInstance ( testAppName ) ;
string testProjectDirectory = testInstance . TestRoot ;
// Restore project VSTestDotNetCore
new RestoreCommand ( )
. WithWorkingDirectory ( testProjectDirectory )
. Execute ( )
. Should ( )
. Pass ( ) ;
string trxLoggerDirectory = Path . Combine ( testProjectDirectory , "ResultsDirectory" ) ;
// 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
2016-12-30 15:47:15 +05:30
String [ ] trxFiles = Directory . GetFiles ( trxLoggerDirectory , "custom.trx" ) ;
2016-12-30 11:24:03 +05:30
Assert . Equal ( 1 , trxFiles . Length ) ;
result . StdOut . Should ( ) . Contain ( trxFiles [ 0 ] ) ;
// Cleanup trxLoggerDirectory if it exist
if ( Directory . Exists ( trxLoggerDirectory ) )
{
Directory . Delete ( trxLoggerDirectory , true ) ;
}
}
2016-12-18 00:45:25 -08:00
[Fact(Skip = "https://github.com/dotnet/cli/issues/5035")]
public void ItBuildsAndTestsAppWhenRestoringToSpecificDirectory ( )
{
var rootPath = TestAssets . Get ( "VSTestDotNetCore" ) . CreateInstance ( ) . WithSourceFiles ( ) . Root . FullName ;
string dir = "pkgs" ;
string fullPath = Path . GetFullPath ( Path . Combine ( rootPath , dir ) ) ;
string args = $"--packages \" { dir } \ "" ;
new RestoreCommand ( )
. WithWorkingDirectory ( rootPath )
. Execute ( args )
. Should ( )
. Pass ( ) ;
new BuildCommand ( )
. WithWorkingDirectory ( rootPath )
. ExecuteWithCapturedOutput ( )
. Should ( )
. Pass ( )
. And . NotHaveStdErr ( ) ;
CommandResult result = new DotnetTestCommand ( )
. WithWorkingDirectory ( rootPath )
. ExecuteWithCapturedOutput ( ) ;
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" ) ;
}
2016-10-04 15:37:36 +05:30
}
2016-12-16 15:39:52 +05:30
}