/
TestPlatformNugetPackageTests.cs
116 lines (93 loc) · 4.69 KB
/
TestPlatformNugetPackageTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.TestPlatform.AcceptanceTests
{
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using Microsoft.TestPlatform.TestUtilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class TestPlatformNugetPackageTests : CodeCoverageAcceptanceTestBase
{
private static string nugetPackageFolder;
private string resultsDirectory;
[ClassInitialize]
public static void ClassInit(TestContext testContext)
{
var packageLocation = Path.Combine(IntegrationTestEnvironment.TestPlatformRootDirectory, "artifacts", IntegrationTestEnvironment.BuildConfiguration, "packages");
var nugetPackage = Directory.EnumerateFiles(packageLocation, "Microsoft.TestPlatform.*.nupkg").OrderBy(a => a).FirstOrDefault();
nugetPackageFolder = Path.Combine(packageLocation, Path.GetFileNameWithoutExtension(nugetPackage));
ZipFile.ExtractToDirectory(nugetPackage, nugetPackageFolder);
TryMoveDirectory(
sourceDirName: Path.Combine(nugetPackageFolder, "tools", "net451", "Team%20Tools"),
destDirName: Path.Combine(nugetPackageFolder, "tools", "net451", "Team Tools")
);
TryMoveDirectory(
sourceDirName: Path.Combine(nugetPackageFolder, "tools", "net451", "Team Tools", "Dynamic%20Code%20Coverage%20Tools"),
destDirName: Path.Combine(nugetPackageFolder, "tools", "net451", "Team Tools", "Dynamic Code Coverage Tools")
);
}
[ClassCleanup]
public static void ClassCleanup()
{
Directory.Delete(nugetPackageFolder, true);
}
[TestInitialize]
public void SetUp()
{
this.resultsDirectory = GetResultsDirectory();
}
[TestCleanup]
public void CleanUp()
{
TryRemoveDirectory(resultsDirectory);
}
[TestMethod]
[TestCategory("Windows-Review")]
[NetFullTargetFrameworkDataSource(useCoreRunner: false)]
[NetCoreTargetFrameworkDataSource(useCoreRunner: false)]
public void RunMultipleTestAssembliesWithCodeCoverage(RunnerInfo runnerInfo)
{
AcceptanceTestBase.SetTestEnvironment(this.testEnvironment, runnerInfo);
var assemblyPaths = this.BuildMultipleAssemblyPath("SimpleTestProject.dll", "SimpleTestProject2.dll").Trim('\"');
var arguments = CreateCodeCoverageArguments(runnerInfo, assemblyPaths, out var trxFilePath);
this.InvokeVsTest(arguments);
this.ExitCodeEquals(1); // failing tests
var actualCoverageFile = CodeCoverageTests.GetCoverageFileNameFromTrx(trxFilePath, resultsDirectory);
Console.WriteLine($@"Coverage file: {actualCoverageFile} Results directory: {resultsDirectory} trxfile: {trxFilePath}");
Assert.IsTrue(File.Exists(actualCoverageFile), "Coverage file not found: {0}", actualCoverageFile);
}
public override string GetConsoleRunnerPath()
{
string consoleRunnerPath = string.Empty;
if (this.IsDesktopRunner())
{
consoleRunnerPath = Path.Combine(nugetPackageFolder, "tools", "net451", "Common7", "IDE", "Extensions", "TestPlatform", "vstest.console.exe");
}
Assert.IsTrue(File.Exists(consoleRunnerPath), "GetConsoleRunnerPath: Path not found: {0}", consoleRunnerPath);
return consoleRunnerPath;
}
private string CreateCodeCoverageArguments(
RunnerInfo runnerInfo,
string assemblyPaths,
out string trxFilePath)
{
string diagFileName = Path.Combine(this.resultsDirectory, "diaglog.txt");
var arguments = PrepareArguments(assemblyPaths, this.GetTestAdapterPath(), string.Empty,
this.FrameworkArgValue, runnerInfo.InIsolationValue, resultsDirectory: resultsDirectory);
arguments = string.Concat(arguments, $" /Diag:{diagFileName}", $" /EnableCodeCoverage");
trxFilePath = Path.Combine(this.resultsDirectory, Guid.NewGuid() + ".trx");
arguments = string.Concat(arguments, " /logger:trx;logfilename=" + trxFilePath);
return arguments;
}
private static void TryMoveDirectory(string sourceDirName, string destDirName)
{
if (Directory.Exists(sourceDirName))
{
Directory.Move(sourceDirName, destDirName);
}
}
}
}