Add tests for Code coverage with dotnet test (#9355)

Add tests for dotnet test --collect "Code Coverage" scenario.

Fixes Microsoft/vstest#981.
This commit is contained in:
Satya Madala 2018-07-17 22:24:10 +05:30 committed by Peter Huene
parent 05d4305fbe
commit a88a82734f
10 changed files with 166 additions and 8 deletions

View file

@ -7,8 +7,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MSTest.TestFramework" Version="1.0.6-preview" />
<PackageReference Include="MSTest.TestAdapter" Version="1.1.5-preview" />
<PackageReference Include="MSTest.TestFramework" Version="$(MSTestVersion)" />
<PackageReference Include="MSTest.TestAdapter" Version="$(MSTestVersion)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkPackageVersion)" />
</ItemGroup>
</Project>

View file

@ -10,8 +10,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MSTest.TestFramework" Version="1.0.6-preview" />
<PackageReference Include="MSTest.TestAdapter" Version="1.1.5-preview" />
<PackageReference Include="MSTest.TestFramework" Version="$(MSTestVersion)" />
<PackageReference Include="MSTest.TestAdapter" Version="$(MSTestVersion)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkPackageVersion)" />
</ItemGroup>
</Project>

View file

@ -8,8 +8,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkPackageVersion)" />
<PackageReference Include="xunit" Version="2.2.0-beta4-build3444" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0-beta4-build1194" />
<PackageReference Include="xunit" Version="$(XUnitVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XUnitVersion)" />
</ItemGroup>
</Project>

View file

@ -17,7 +17,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkPackageVersion)" />
<PackageReference Include="xunit" Version="2.2.0-beta4-build3444" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0-beta4-build1194" />
<PackageReference Include="xunit" Version="$(XUnitVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XUnitVersion)" />
</ItemGroup>
</Project>

View file

@ -58,6 +58,8 @@
<MicrosoftTestPlatformBuildPackageVersion>$(MicrosoftNETTestSdkPackageVersion)</MicrosoftTestPlatformBuildPackageVersion>
<XliffTasksPackageVersion>0.2.0-beta-000042</XliffTasksPackageVersion>
<MicroBuildCorePackageVersion>0.2.0</MicroBuildCorePackageVersion>
<MSTestVersion>1.3.2</MSTestVersion>
<XUnitVersion>2.3.1</XUnitVersion>
<NUnit3TemplatesVersion>1.5.1</NUnit3TemplatesVersion>
</PropertyGroup>

View file

@ -18,6 +18,7 @@
<NugetConfigCLIFeeds>
<![CDATA[
<add key="myget-vstest" value="https://dotnet.myget.org/F/vstest/api/v3/index.json" />
<add key="BlobFeed" value="https://dotnetfeed.blob.core.windows.net/dotnet-core/index.json" />
<add key="aspnetcore-dev" value="https://dotnet.myget.org/F/aspnetcore-dev/api/v3/index.json" />
<add key="templating" value="https://dotnet.myget.org/F/templating/api/v3/index.json" />

View file

@ -0,0 +1,25 @@
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0">
<Configuration>
<CodeCoverage>
<!-- Match assembly file paths: -->
<ModulePaths>
<Exclude>
<ModulePath>.*Test.dll</ModulePath>
</Exclude>
</ModulePaths>
<!-- We recommend you do not change the following values: -->
<UseVerifiableInstrumentation>True</UseVerifiableInstrumentation>
<AllowLowIntegrityProcesses>True</AllowLowIntegrityProcesses>
<CollectFromChildProcesses>True</CollectFromChildProcesses>
<CollectAspDotNet>False</CollectAspDotNet>
</CodeCoverage>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>

View file

@ -84,6 +84,36 @@ namespace Microsoft.DotNet.Cli.Test.Tests
result.ExitCode.Should().Be(1);
}
[WindowsOnlyFact]
public void ItCreatesTwoCoverageFilesForMultiTargetedProject()
{
// Copy XunitMulti project in output directory of project dotnet-test.Tests
string testAppName = "XunitMulti";
var testInstance = TestAssets.Get(testAppName)
.CreateInstance("3")
.WithSourceFiles();
var testProjectDirectory = testInstance.Root.FullName;
string resultsDirectory = Path.Combine(testProjectDirectory, "RD");
// Delete resultsDirectory if it exist
if (Directory.Exists(resultsDirectory))
{
Directory.Delete(resultsDirectory, true);
}
// Call test
CommandResult result = new DotnetTestCommand()
.WithWorkingDirectory(testProjectDirectory)
.ExecuteWithCapturedOutput($"{TestBase.ConsoleLoggerOutputNormal} --collect \"Code Coverage\" --results-directory {resultsDirectory}");
// Verify
DirectoryInfo d = new DirectoryInfo(resultsDirectory);
FileInfo[] coverageFileInfos = d.GetFiles("*.coverage", SearchOption.AllDirectories);
Assert.Equal(2, coverageFileInfos.Length);
}
[Fact]
public void ItCanTestAMultiTFMProjectWithImplicitRestore()
{

View file

@ -9,6 +9,7 @@ using Microsoft.DotNet.Cli.Utils;
using System.IO;
using System;
using System.Runtime.CompilerServices;
using System.Xml;
namespace Microsoft.DotNet.Cli.Test.Tests
{
@ -311,6 +312,100 @@ namespace Microsoft.DotNet.Cli.Test.Tests
result.ExitCode.Should().Be(1);
}
[WindowsOnlyFact]
public void ItCreatesCoverageFileWhenCodeCoverageEnabledByRunsettings()
{
var testProjectDirectory = this.CopyAndRestoreVSTestDotNetCoreTestApp("11");
string resultsDirectory = Path.Combine(testProjectDirectory, "RD");
// Delete resultsDirectory if it exist
if (Directory.Exists(resultsDirectory))
{
Directory.Delete(resultsDirectory, true);
}
var settingsPath =Path.Combine(AppContext.BaseDirectory, "CollectCodeCoverage.runsettings");
// Call test
CommandResult result = new DotnetTestCommand()
.WithWorkingDirectory(testProjectDirectory)
.ExecuteWithCapturedOutput(
"--settings " + settingsPath
+ " --results-directory " + resultsDirectory);
// Verify test results
if (!DotnetUnderTest.IsLocalized())
{
result.StdOut.Should().Contain("Total tests: 2. Passed: 1. Failed: 1. Skipped: 0.");
}
// Verify coverage file.
DirectoryInfo d = new DirectoryInfo(resultsDirectory);
FileInfo[] coverageFileInfos = d.GetFiles("*.coverage", SearchOption.AllDirectories);
Assert.Equal(1, coverageFileInfos.Length);
result.ExitCode.Should().Be(1);
}
[WindowsOnlyFact]
public void ItCreatesCoverageFileInResultsDirectory()
{
var testProjectDirectory = this.CopyAndRestoreVSTestDotNetCoreTestApp("12");
string resultsDirectory = Path.Combine(testProjectDirectory, "RD");
// Delete resultsDirectory if it exist
if (Directory.Exists(resultsDirectory))
{
Directory.Delete(resultsDirectory, true);
}
// Call test
CommandResult result = new DotnetTestCommand()
.WithWorkingDirectory(testProjectDirectory)
.ExecuteWithCapturedOutput(
"--collect \"Code Coverage\" "
+ "--results-directory " + resultsDirectory);
// Verify test results
if (!DotnetUnderTest.IsLocalized())
{
result.StdOut.Should().Contain("Total tests: 2. Passed: 1. Failed: 1. Skipped: 0.");
}
// Verify coverage file.
DirectoryInfo d = new DirectoryInfo(resultsDirectory);
FileInfo[] coverageFileInfos = d.GetFiles("*.coverage", SearchOption.AllDirectories);
Assert.Equal(1, coverageFileInfos.Length);
result.ExitCode.Should().Be(1);
}
[UnixOnlyFact]
public void ItShouldShowWarningMessageOnCollectCodeCoverage()
{
var testProjectDirectory = this.CopyAndRestoreVSTestDotNetCoreTestApp("13");
// Call test
CommandResult result = new DotnetTestCommand()
.WithWorkingDirectory(testProjectDirectory)
.ExecuteWithCapturedOutput(
"--collect \"Code Coverage\" "
+ "--filter VSTestPassTest");
// Verify test results
if (!DotnetUnderTest.IsLocalized())
{
result.StdOut.Should().Contain("No code coverage data available. Code coverage is currently supported only on Windows.");
result.StdOut.Should().Contain("Total tests: 1. Passed: 1. Failed: 0. Skipped: 0.");
result.StdOut.Should().Contain("Test Run Successful.");
}
result.ExitCode.Should().Be(0);
}
private string CopyAndRestoreVSTestDotNetCoreTestApp([CallerMemberName] string callingMethod = "")
{
// Copy VSTestCore project in output directory of project dotnet-vstest.Tests

View file

@ -16,4 +16,9 @@
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="xunit" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<None Update="CollectCodeCoverage.runsettings">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>