Skip to content

Commit bc8dfb5

Browse files
authored
Support XUnitV3 as test runner (#15671)
1 parent 87401be commit bc8dfb5

File tree

7 files changed

+157
-1
lines changed

7 files changed

+157
-1
lines changed

src/Microsoft.DotNet.Arcade.Sdk/tools/DefaultVersions.props

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@
8989
<XUnitRunnerConsoleVersion Condition="'$(XUnitRunnerConsoleVersion)' == ''">$(XUnitVersion)</XUnitRunnerConsoleVersion>
9090
<XUnitRunnerVisualStudioVersion Condition="'$(XUnitRunnerVisualStudioVersion)' == ''">3.0.2</XUnitRunnerVisualStudioVersion>
9191

92+
<XUnitV3Version Condition="'$(XUnitV3Version)' == ''">2.0.0</XUnitV3Version>
93+
<MicrosoftTestingPlatformVersion Condition="'$(MicrosoftTestingPlatformVersion)' == ''">1.6.3</MicrosoftTestingPlatformVersion>
94+
9295
<MSTestVersion Condition="'$(MSTestVersion)' == ''">3.8.3</MSTestVersion>
9396
<MSTestTestAdapterVersion Condition="'$(MSTestTestAdapterVersion)' == ''">$(MSTestVersion)</MSTestTestAdapterVersion>
9497
<MSTestTestFrameworkVersion Condition="'$(MSTestTestFrameworkVersion)' == ''">$(MSTestVersion)</MSTestTestFrameworkVersion>

src/Microsoft.DotNet.Arcade.Sdk/tools/Tests.props

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
3030
</ItemGroup>
3131

32-
<ItemGroup Condition="'$(IsTestProject)' == 'true' and '$(ExcludeMicrosoftNetTestSdk)' != 'true'">
32+
<!-- Microsoft.NET.Test.Sdk is VSTest-specific package. -->
33+
<!-- If EnableMSTestRunner, EnableNUnitRunner, or UseMicrosoftTestingPlatformRunner is true, then the repo is using Microsoft.Testing.Platform -->
34+
<!-- In that case, we don't need Microsoft.NET.Test.Sdk -->
35+
<ItemGroup Condition="'$(IsTestProject)' == 'true' and '$(ExcludeMicrosoftNetTestSdk)' != 'true' and '$(EnableMSTestRunner)' != 'true' and '$(EnableNUnitRunner)' != 'true' and '$(UseMicrosoftTestingPlatformRunner)' != 'true'">
3336
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkVersion)" IsImplicitlyDefined="true" />
3437
</ItemGroup>
3538

src/Microsoft.DotNet.Arcade.Sdk/tools/Tests.targets

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<Target Name="_InnerGetTestsToRun"
4545
Outputs="%(_TestArchitectureItems.Identity)"
4646
Returns="@(TestToRun)"
47+
DependsOnTargets="ComputeRunArguments"
4748
Condition="'$(TestRuntime)' != '' and '$(SkipTests)' != 'true' and
4849
('$(TestTargetFrameworks)' == '' or $([System.String]::new(';$(TestTargetFrameworks);').Contains(';$(TargetFramework);')))">
4950

@@ -71,6 +72,8 @@
7172
<ResultsStdOutPath>$(TestResultsLogDir)$(_ResultFileNameNoExt).log</ResultsStdOutPath>
7273
<TestRunSettingsFile>$(TestRunSettingsFile)</TestRunSettingsFile>
7374
<TestRunnerAdditionalArguments>$(TestRunnerAdditionalArguments)</TestRunnerAdditionalArguments>
75+
<RunArguments>$(RunArguments)</RunArguments>
76+
<RunCommand>$(RunCommand)</RunCommand>
7477
</TestToRun>
7578
</ItemGroup>
7679
</Target>

src/Microsoft.DotNet.Arcade.Sdk/tools/VSTest.targets

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
<Target Name="RunTests"
55
Outputs="%(TestToRun.ResultsStdOutPath)"
66
Condition="'$(SkipTests)' != 'true' and '@(TestToRun)' != ''">
7+
8+
<Error Text="UseVSTest shouldn't be used when using Microsoft.Testing.Platform" Condition="'$(EnableMSTestRunner)' == 'true' OR '$(EnableNUnitRunner)' == 'true' OR '$(UseMicrosoftTestingPlatformRunner)' == 'true'" File="VSTest" />
9+
710
<PropertyGroup>
811
<_TestEnvironment>%(TestToRun.EnvironmentDisplay)</_TestEnvironment>
912
<_TestAssembly>%(TestToRun.Identity)</_TestAssembly>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. -->
2+
<Project>
3+
4+
<Target Name="RunTests"
5+
Outputs="%(TestToRun.ResultsStdOutPath)"
6+
Condition="'@(TestToRun)' != ''">
7+
<Telemetry EventName="NETCORE_ENGINEERING_TELEMETRY" EventData="Category=Test" />
8+
9+
<PropertyGroup Condition="'$(UseMicrosoftTestingPlatformRunner)' == 'true'">
10+
<_TestResultDirectory>$([System.IO.Path]::GetDirectoryName('%(TestToRun.ResultsTrxPath)'))</_TestResultDirectory>
11+
<_TestResultTrxFileName>$([System.IO.Path]::GetFileName('%(TestToRun.ResultsTrxPath)'))</_TestResultTrxFileName>
12+
<_TestResultXmlFileName>$([System.IO.Path]::GetFileName('%(TestToRun.ResultsXmlPath)'))</_TestResultXmlFileName>
13+
<_TestResultHtmlFileName>$([System.IO.Path]::GetFileName('%(TestToRun.ResultsHtmlPath)'))</_TestResultHtmlFileName>
14+
</PropertyGroup>
15+
16+
<PropertyGroup>
17+
<_TestEnvironment>%(TestToRun.EnvironmentDisplay)</_TestEnvironment>
18+
<_TestAssembly>%(TestToRun.Identity)</_TestAssembly>
19+
<_TestRuntime>%(TestToRun.TestRuntime)</_TestRuntime>
20+
<_TestTimeout>%(TestToRun.TestTimeout)</_TestTimeout>
21+
<_TestRunnerAdditionalArguments>%(TestToRun.TestRunnerAdditionalArguments)</_TestRunnerAdditionalArguments>
22+
23+
<_TestRunner>%(TestToRun.RunCommand)</_TestRunner>
24+
<_TestRunnerArgs Condition="'$(UseMicrosoftTestingPlatformRunner)' != 'true'">%(TestToRun.RunArguments) $(_TestRunnerAdditionalArguments) -xml "%(TestToRun.ResultsXmlPath)" -html "%(TestToRun.ResultsHtmlPath)" -trx "%(TestToRun.ResultsTrxPath)"</_TestRunnerArgs>
25+
<_TestRunnerArgs Condition="'$(UseMicrosoftTestingPlatformRunner)' == 'true'">%(TestToRun.RunArguments) $(_TestRunnerAdditionalArguments) --results-directory "$(_TestResultDirectory)" --report-xunit --report-xunit-filename "$(_TestResultXmlFileName)" --report-xunit-html --report-xunit-html-filename "$(_TestResultHtmlFileName)" --report-trx --report-trx-filename "$(_TestResultTrxFileName)"</_TestRunnerArgs>
26+
27+
<TestDotNetRoot Condition="'$(TestDotNetRoot)' == ''">$(DotNetRoot)</TestDotNetRoot>
28+
</PropertyGroup>
29+
30+
<PropertyGroup Condition="'$(_TestRuntime)' == 'Core'">
31+
<_TestRunnerArgs Condition="'$(UseMicrosoftTestingPlatformRunner)' != 'true'">$(_TestRunnerArgs) -noAutoReporters</_TestRunnerArgs>
32+
<_TestRunnerArgs Condition="'$(UseMicrosoftTestingPlatformRunner)' == 'true'">$(_TestRunnerArgs) --auto-reporters off</_TestRunnerArgs>
33+
</PropertyGroup>
34+
35+
<PropertyGroup>
36+
<_TestRunnerCommand>"$(_TestRunner)" $(_TestRunnerArgs)</_TestRunnerCommand>
37+
38+
<!--
39+
Redirect std output of the runner.
40+
Note that xUnit outputs failure info to both STDOUT (stack trace, message) and STDERR (failed test name)
41+
-->
42+
<_TestRunnerCommand Condition="'$(TestCaptureOutput)' != 'false'">$(_TestRunnerCommand) >> "%(TestToRun.ResultsStdOutPath)" 2>&amp;1</_TestRunnerCommand>
43+
</PropertyGroup>
44+
45+
<ItemGroup>
46+
<_OutputFiles Include="%(TestToRun.ResultsXmlPath)" />
47+
<_OutputFiles Include="%(TestToRun.ResultsHtmlPath)" />
48+
<_OutputFiles Include="%(TestToRun.ResultsStdOutPath)" />
49+
</ItemGroup>
50+
51+
<MakeDir Directories="@(_OutputFiles->'%(RootDir)%(Directory)')"/>
52+
<Delete Files="@(_OutputFiles)" />
53+
54+
<Message Text="Running tests: $(_TestAssembly) [$(_TestEnvironment)]" Importance="high"/>
55+
56+
<!--
57+
Add command line to the log.
58+
-->
59+
<Exec Command="echo === COMMAND LINE === > %(TestToRun.ResultsStdOutPath)
60+
echo $(_TestRunnerCommand) >> %(TestToRun.ResultsStdOutPath)" />
61+
62+
<Exec Command='$(_TestRunnerCommand)'
63+
LogStandardErrorAsError="false"
64+
WorkingDirectory="$(_TargetDir)"
65+
IgnoreExitCode="true"
66+
Timeout="$(_TestTimeout)"
67+
EnvironmentVariables="DOTNET_ROOT=$(TestDotNetRoot);DOTNET_ROOT_X86=$(TestDotNetRoot)x86"
68+
ContinueOnError="WarnAndContinue">
69+
<Output TaskParameter="ExitCode" PropertyName="_TestErrorCode" />
70+
</Exec>
71+
72+
<!--
73+
Report test status.
74+
-->
75+
<Message Text="Tests succeeded: $(_TestAssembly) [$(_TestEnvironment)]" Condition="'$(_TestErrorCode)' == '0'" Importance="high" />
76+
77+
<PropertyGroup>
78+
<_ResultsFileToDisplay>%(TestToRun.ResultsHtmlPath)</_ResultsFileToDisplay>
79+
<_ResultsFileToDisplay Condition="!Exists('$(_ResultsFileToDisplay)')">%(TestToRun.ResultsStdOutPath)</_ResultsFileToDisplay>
80+
</PropertyGroup>
81+
82+
<!--
83+
Ideally we would set ContinueOnError="ErrorAndContinue" so that when a test fails in multi-targeted test project
84+
we'll still run tests for all target frameworks. ErrorAndContinue doesn't work well on Linux though: https://github.com/Microsoft/msbuild/issues/3961.
85+
-->
86+
<Error Text="Tests failed: $(_ResultsFileToDisplay) [$(_TestEnvironment)]" Condition="'$(_TestErrorCode)' != '0' and '$(_ErrorOnTestFailure)' != 'false'" File="XUnit" />
87+
88+
<ItemGroup>
89+
<FileWrites Include="@(_OutputFiles)"/>
90+
</ItemGroup>
91+
</Target>
92+
93+
</Project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. -->
2+
<Project>
3+
4+
<PropertyGroup>
5+
<UseVSTestRunner Condition="'$(UseVSTestRunner)' != 'true'">false</UseVSTestRunner>
6+
7+
<!-- Default to using MTP -->
8+
<UseMicrosoftTestingPlatformRunner Condition="'$(UseMicrosoftTestingPlatformRunner)' == ''">true</UseMicrosoftTestingPlatformRunner>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="xunit.v3" Version="$(XUnitV3Version)" IsImplicitlyDefined="true" PrivateAssets="all" Publish="true" />
13+
<!-- TODO -->
14+
<!-- <PackageReference Include="Microsoft.DotNet.XUnitV3Assert" Version="$(MicrosoftDotNetXUnitAssertVersion)" IsImplicitlyDefined="true" PrivateAssets="all" Publish="true" Condition="'$(UseDotNetXUnitAssert)' == 'true' and '$(TargetFrameworkIdentifier)' == '.NETCoreApp'" /> -->
15+
<PackageReference Include="xunit.runner.visualstudio" Version="$(XUnitRunnerVisualStudioVersion)" IsImplicitlyDefined="true" PrivateAssets="all" Publish="true"/>
16+
</ItemGroup>
17+
18+
<ItemGroup Condition="'$(UseMicrosoftTestingPlatformRunner)' == 'true'">
19+
<PackageReference Include="Microsoft.Testing.Extensions.TrxReport" Version="$(MicrosoftTestingPlatformVersion)" IsImplicitlyDefined="true" PrivateAssets="all" Publish="true" />
20+
<PackageReference Include="Microsoft.Testing.Platform.MSBuild" Version="$(MicrosoftTestingPlatformVersion)" IsImplicitlyDefined="true" PrivateAssets="all" Publish="true" />
21+
<PackageReference Include="Microsoft.Testing.Platform" Version="$(MicrosoftTestingPlatformVersion)" IsImplicitlyDefined="true" PrivateAssets="all" Publish="true" />
22+
</ItemGroup>
23+
24+
<PropertyGroup>
25+
<XUnitDesktopSettingsFile Condition="'$(XUnitDesktopSettingsFile)' == ''">$(MSBuildThisFileDirectory)xunit.runner.json</XUnitDesktopSettingsFile>
26+
<XUnitCoreSettingsFile Condition="'$(XUnitCoreSettingsFile)' == ''">$(MSBuildThisFileDirectory)xunit.runner.json</XUnitCoreSettingsFile>
27+
</PropertyGroup>
28+
29+
<!--
30+
Include settings file (xunit.runner.json) if specified.
31+
-->
32+
<ItemGroup>
33+
<None Include="$(XUnitDesktopSettingsFile)"
34+
CopyToOutputDirectory="PreserveNewest"
35+
Visible="false"
36+
Condition="'$(XUnitDesktopSettingsFile)' != '' and '$(TargetFrameworkIdentifier)' == '.NETFramework'" />
37+
38+
<None Include="$(XUnitCoreSettingsFile)"
39+
CopyToOutputDirectory="PreserveNewest"
40+
Visible="false"
41+
Condition="'$(XUnitCoreSettingsFile)' != '' and '$(TargetFrameworkIdentifier)' == '.NETCoreApp'" />
42+
</ItemGroup>
43+
44+
<Import Project="XUnitV3.Runner.targets" Condition="!$(UseVSTestRunner)"/>
45+
<Import Project="..\VSTest.targets" Condition="$(UseVSTestRunner)"/>
46+
47+
</Project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
3+
"shadowCopy": false
4+
}

0 commit comments

Comments
 (0)