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

Add test script to run unit tests using local vstest.console runner. #6

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 172 additions & 0 deletions scripts/test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# Copyright (c) Microsoft. All rights reserved.
# Build script for Test Platform.

[CmdletBinding()]
Param(
[Parameter(Mandatory=$false)]
[ValidateSet("Debug", "Release")]
[Alias("c")]
[System.String] $Configuration = "Debug",

# Only test sources matching the pattern are run.
# Use End2End to run E2E tests. Or to run any one assembly tests, use the
# assembly name. E.g. test -p Microsoft.TestPlatform.CoreUtilities.UnitTests
[Parameter(Mandatory=$false)]
[Alias("p")]
[System.String] $Pattern = "Unit",

# Stop test run on first failure
[Parameter(Mandatory=$false)]
[Alias("ff")]
[Switch] $FailFast = $false
)

$ErrorActionPreference = "Stop"

#
# Variables
#
Write-Verbose "Setup environment variables."
$env:TP_ROOT_DIR = (Get-Item (Split-Path $MyInvocation.MyCommand.Path)).Parent.FullName
$env:TP_TOOLS_DIR = Join-Path $env:TP_ROOT_DIR "tools"
$env:TP_PACKAGES_DIR = Join-Path $env:TP_ROOT_DIR "packages"
$env:TP_OUT_DIR = Join-Path $env:TP_ROOT_DIR "artifacts"

#
# Dotnet configuration
#
# Disable first run since we want to control all package sources
Write-Verbose "Setup dotnet configuration."
$env:DOTNET_SKIP_FIRST_TIME_EXPERIENCE = 1
# Dotnet build doesn't support --packages yet. See https://github.com/dotnet/cli/issues/2712
$env:NUGET_PACKAGES = $env:TP_PACKAGES_DIR

#
# Test configuration
#
# Folders to build. TODO move to props
Write-Verbose "Setup build configuration."
$Script:TPT_Configuration = $Configuration
$Script:TPT_SourceFolders = @("test")
$Script:TPT_TargetFramework = "net46"
$Script:TPT_TargetRuntime = "win7-x64"
$Script:TPT_SkipProjects = @("Microsoft.TestPlatform.CoreUtilities.UnitTests")
$Script:TPT_Pattern = $Pattern
$Script:TPT_FailFast = $FailFast

function Write-Log ([string] $message)
{
$currentColor = $Host.UI.RawUI.ForegroundColor
$Host.UI.RawUI.ForegroundColor = "Green"
if ($message)
{
Write-Output "... $message"
}
$Host.UI.RawUI.ForegroundColor = $currentColor
}

function Write-VerboseLog([string] $message)
{
Write-Verbose $message
}

function Invoke-Test
{
$timer = Start-Timer
Write-Log "Invoke-Test: Start test."
$dotnetExe = Get-DotNetPath

foreach ($src in $Script:TPT_SourceFolders) {
# Invoke test for each project.json since we want a custom output
# path.
$vstestConsolePath = Join-Path (Get-PackageDirectory) "vstest.console.exe"
if (!(Test-Path $vstestConsolePath)) {
Write-Log "Unable to find vstest.console.exe at $vstestConsolePath. Did you run build.cmd?"
Write-Error "Test aborted."
}

foreach ($fx in $Script:TPT_TargetFramework) {
Get-ChildItem -Recurse -Path $src -Include "project.json" | ForEach-Object {
Write-Log ".. Test: Source: $_"

# Tests are only built for x86 at the moment, though we don't have architecture requirement
$testContainerName = $_.Directory.Name
$testOutputPath = Join-Path $_.Directory.FullName "bin/$($Script:TPT_Configuration)/$($Script:TPT_TargetFramework)/win7-x86"
$testContainerPath = Join-Path $testOutputPath "$($testContainerName).dll"

if ($Script:TPT_SkipProjects.Contains($testContainerName)) {
Write-Log ".. . $testContainerName is in skipped test list."
} elseif (!($testContainerName -match $Script:TPT_Pattern)) {
Write-Log ".. . $testContainerName doesn't match test container pattern '$($Script:TPT_Pattern)'. Skipped from run."
} else {
Write-Verbose "vstest.console.exe $testContainerPath /testAdapterPath:$testOutputPath"
$output = & $vstestConsolePath $testContainerPath /testAdapterPath:"$testOutputPath"

#Write-Verbose "$dotnetExe test $_ --configuration $Configuration"
#& $dotnetExe test $_ --configuration $Configuration

if ($output[-2].Contains("Test Run Successful.")) {
Write-Log ".. . $($output[-3])"
} else {
Write-Log ".. . $($output[-2])"
Write-Log ".. . Failed tests:"
Write-Log ".. . $($output -match '^Failed')"

if ($Script:TPT_FailFast) {
Write-Log ".. Stop execution since fail fast is enabled."
continue
}
}
}

Write-Log ".. Test: Complete."
}
}
#Write-Verbose "$dotnetExe test $src\**\project.json --configuration $Configuration"
#& $dotnetExe test $_ $src\**\project.json --configuration $Configuration
}

Write-Log "Invoke-Test: Complete. {$(Get-ElapsedTime($timer))}"
}

#
# Helper functions
#
function Get-DotNetPath
{
$dotnetPath = Join-Path $env:TP_TOOLS_DIR "dotnet\dotnet.exe"
if (-not (Test-Path $dotnetPath)) {
Write-Error "Dotnet.exe not found at $dotnetPath. Did the dotnet cli installation succeed?"
}

return $dotnetPath
}

function Get-PackageDirectory
{
return $(Join-Path $env:TP_OUT_DIR "$($Script:TPT_Configuration)\$($Script:TPT_TargetFramework)\$($Script:TPT_TargetRuntime)")
}

function Start-Timer
{
return [System.Diagnostics.Stopwatch]::StartNew()
}

function Get-ElapsedTime([System.Diagnostics.Stopwatch] $timer)
{
$timer.Stop()
return $timer.Elapsed
}

# Execute build
$timer = Start-Timer
Write-Log "Build started: args = '$args'"
Write-Log "Test platform environment variables: "
Get-ChildItem env: | Where-Object -FilterScript { $_.Name.StartsWith("TP_") } | Format-Table

Write-Log "Test run configuration: "
Get-Variable | Where-Object -FilterScript { $_.Name.StartsWith("TPT_") } | Format-Table

Invoke-Test

Write-Log "Build complete. {$(Get-ElapsedTime($timer))}"
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public static TestLoggerManager Instance
}
return testLoggerManager;
}

protected set
{
testLoggerManager = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
},

"dependencies": {

"Microsoft.TestPlatform.ObjectModel": "15.0.0-*",
"Microsoft.TestPlatform.CoreUtilities": "15.0.0-*"
},
Expand Down
14 changes: 5 additions & 9 deletions src/vstest.console/Internal/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ internal class ConsoleLogger : ITestLogger

#region Fields

private static IOutput output;

private TestOutcome testOutcome = TestOutcome.None;
private int testsTotal = 0;
private int testsPassed = 0;
Expand All @@ -62,7 +60,7 @@ public ConsoleLogger()
/// <param name="output"></param>
internal ConsoleLogger(IOutput output)
{
ConsoleLogger.output = output;
ConsoleLogger.Output = output;
}

#endregion
Expand All @@ -74,10 +72,8 @@ internal ConsoleLogger(IOutput output)
/// <remarks>Protected so this can be detoured for testing purposes.</remarks>
protected static IOutput Output
{
get
{
return output;
}
get;
private set;
}
#endregion

Expand All @@ -95,9 +91,9 @@ public void Initialize(TestLoggerEvents events, string testRunDirectory)
throw new ArgumentNullException("events");
}

if (output == null)
if (ConsoleLogger.Output == null)
{
output = ConsoleOutput.Instance;
ConsoleLogger.Output = ConsoleOutput.Instance;
}

// Register for the events.
Expand Down
6 changes: 6 additions & 0 deletions test.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@echo off

REM Copyright (c) Microsoft. All rights reserved.

powershell -NoProfile -NoLogo -Command "%~dp0scripts\test.ps1 %*; exit $LastExitCode;"
if %errorlevel% neq 0 exit /b %errorlevel%
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace Microsoft.VisualStudio.TestPlatform.Client.UnitTests.DesignMode
{
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks;
using Microsoft.VisualStudio.TestPlatform.Client.DesignMode; using Microsoft.VisualStudio.TestPlatform.Client.RequestHelper; using Microsoft.VisualStudio.TestPlatform.ObjectModel; using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.Interfaces; using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Microsoft.VisualStudio.TestPlatform.Client.DesignMode;
using Microsoft.VisualStudio.TestPlatform.Client.RequestHelper;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Moq;

[TestClass]
public class DesignModeTestHostLauncherFactoryTests
Expand All @@ -13,32 +16,20 @@ public class DesignModeTestHostLauncherFactoryTests
public void DesignModeTestHostFactoryShouldReturnNonDebugLauncherIfDebuggingDisabled()
{
var mockDesignModeClient = new Mock<IDesignModeClient>();
var testRunRequestPayload = new TestRunRequestPayload() { DebuggingEnabled = false };
var testRunRequestPayload = new TestRunRequestPayload { DebuggingEnabled = false };
var launcher = DesignModeTestHostLauncherFactory.GetCustomHostLauncherForTestRun(mockDesignModeClient.Object, testRunRequestPayload);

Assert.IsFalse(launcher.IsDebug, "Factory must not return debug launcher if debugging is disabled.");

var testProcessStartInfo = new TestProcessStartInfo();

launcher.LaunchTestHost(testProcessStartInfo);

mockDesignModeClient.Verify(md => md.LaunchCustomHost(testProcessStartInfo), Times.Once, "Launcher should use provided design mode client");
}

[TestMethod]
public void DesignModeTestHostFactoryShouldReturnDebugLauncherIfDebuggingEnabled()
{
var mockDesignModeClient = new Mock<IDesignModeClient>();
var testRunRequestPayload = new TestRunRequestPayload() { DebuggingEnabled = true };
var testRunRequestPayload = new TestRunRequestPayload { DebuggingEnabled = true };
var launcher = DesignModeTestHostLauncherFactory.GetCustomHostLauncherForTestRun(mockDesignModeClient.Object, testRunRequestPayload);

Assert.IsTrue(launcher.IsDebug, "Factory must not return debug launcher if debugging is disabled.");

var testProcessStartInfo = new TestProcessStartInfo();

launcher.LaunchTestHost(testProcessStartInfo);

mockDesignModeClient.Verify(md => md.LaunchCustomHost(testProcessStartInfo), Times.Once, "Launcher should use provided design mode client");
}
}
}
33 changes: 23 additions & 10 deletions test/Microsoft.TestPlatform.Client.UnitTests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@
},

"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
},
"dotnet-test-mstest": {
"version": "1.0.1-preview",
"MSTest.TestFramework": "1.0.0-preview",
"MSTest.TestAdapter": {
"version": "1.0.3-preview",
"exclude": "compile"
},
"MSTest.TestFramework": "1.0.0-preview",
"moq.netcore": "4.4.0-beta8",
"System.Diagnostics.TraceSource": "4.0.0-rc2-24015",
"Microsoft.TestPlatform.Client": "15.0.0-*"
Expand All @@ -27,9 +23,26 @@
"imports": [
"dnxcore50",
"portable-net45+win8"
]
}
],

"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
},
"dotnet-test-mstest": {
"version": "1.0.1-preview",
"exclude": "compile"
}
}
},

"net46": {
"frameworkAssemblies": {
"System.Runtime": ""
}
}
},

"testRunner": "mstest"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,18 @@ public void UpdateAdditionalExtensionsShouldResetExtensionsDiscoveredFlag()
[TestMethod]
public void GetDefaultResolutionPathsShouldReturnCurrentDirectoryByDefault()
{
var resolutionPaths = TestPluginCache.Instance.GetDefaultResolutionPaths();

var currentDirectory = Path.GetDirectoryName(typeof(TestPluginCache).GetTypeInfo().Assembly.Location);
var defaultExtensionsDirectory = Path.Combine(currentDirectory, "Extensions");
var expectedDirectories = new List<string> { currentDirectory };
if (Directory.Exists(defaultExtensionsDirectory))
{
expectedDirectories.Add(defaultExtensionsDirectory);
}

var resolutionPaths = TestPluginCache.Instance.GetDefaultResolutionPaths();

Assert.IsNotNull(resolutionPaths);
CollectionAssert.AreEqual(new List<string> { currentDirectory }, resolutionPaths.ToList());
CollectionAssert.AreEqual(expectedDirectories, resolutionPaths.ToList());
}

[TestMethod]
Expand Down
Loading