Skip to content

Commit 4760cf0

Browse files
authored
Introduce a BuildAllTestsAsStandalone environment variable to support some local dev scenarios. (dotnet#100709)
1 parent bd84734 commit 4760cf0

File tree

10 files changed

+44
-14
lines changed

10 files changed

+44
-14
lines changed

docs/workflow/testing/coreclr/testing.md

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
* [Building an Individual Test](#building-an-individual-test)
88
* [Building a Test Directory](#building-a-test-directory)
99
* [Building a Test Subtree](#building-a-test-subtree)
10+
* [Test Executors](#test-executors)
11+
* [The Standalone Test Runner and Build Time Test Filtering](#the-standalone-test-runner-and-build-time-test-filtering)
12+
* [Building all tests with the Standalone Runner](#building-all-tests-with-the-standalone-runner)
1013
* [Building C++/CLI Native Test Components Against the Live Ref Assemblies](#building-ccli-native-test-components-against-the-live-ref-assemblies)
1114
* [Test Priorities](#test-priorities)
1215
* [Running the Tests](#running-the-tests)
1316
* [Running Individual Tests](#running-individual-tests)
14-
* [Tests Without a Main Method](#tests-without-a-main-method)
1517
* [PAL Tests (macOS and Linux only)](#pal-tests-macos-and-linux-only)
1618
* [Building PAL Tests](#building-pal-tests)
1719
* [Running PAL Tests](#running-pal-tests)
@@ -62,7 +64,7 @@ This example assumes you built CoreCLR on _Debug_ mode and the Libraries on _Rel
6264

6365
The following subsections will explain how to segment the test suite according to your needs. There are three main scopes of building tests:
6466

65-
* Individual Test
67+
* Individual Test Runner
6668
* Full Directory
6769
* Entire Subtree
6870

@@ -83,19 +85,19 @@ To build an individual test, you have to pass the `-test` flag along with the pa
8385
On Windows:
8486

8587
```cmd
86-
.\src\tests\build.cmd test JIT\Intrinsics\MathRoundDouble_ro.csproj test JIT\Intrinsics\MathFloorDouble_ro.csproj
88+
.\src\tests\build.cmd test JIT\Methodical\Methodical_d1.csproj test JIT\JIT_ro.csproj
8789
```
8890

8991
On macOS and Linux:
9092

9193
```bash
92-
./src/tests/build.sh -test:JIT/Intrinsics/MathRoundDouble_ro.csproj -test:JIT/Intrinsics/MathFloorDouble_ro.csproj
94+
./src/tests/build.sh -test:JIT/Methodical/Methodical_d1.csproj -test:JIT/JIT_ro.csproj
9395
```
9496

9597
Alternatively, you can call _build_ directly using the `dotnet.cmd/dotnet.sh` script at the root of the repo and pass all arguments directly yourself:
9698

9799
```bash
98-
./dotnet.sh build -c <Your Configuration> src/tests/path/to/test/csproj
100+
./dotnet.sh build -c <Your Configuration> src/tests/path/to/test.csproj
99101
```
100102

101103
### Building a Test Directory
@@ -105,13 +107,13 @@ To build all the tests contained in an individual directory, you have to pass th
105107
On Windows:
106108

107109
```cmd
108-
.\src\tests\build.cmd dir JIT\Methodical\Arrays\lcs dir JIT\Methodical\cctor\misc\Desktop
110+
.\src\tests\build.cmd dir JIT dir Loader
109111
```
110112

111113
On macOS and Linux:
112114

113115
```bash
114-
./src/tests/build.sh -dir:JIT/Methodical/Arrays/lcs -dir:JIT/Methodical/cctor/misc/Desktop
116+
./src/tests/build.sh -dir:JIT -dir:Loader
115117
```
116118

117119
### Building a Test Subtree
@@ -130,6 +132,24 @@ On macOS and Linux:
130132
./src/tests/build.sh -tree:baseservices/exceptions -tree:JIT/Methodical
131133
```
132134

135+
### Test Executors
136+
137+
We have multiple different mechanisms of executing tests.
138+
139+
Our test entrypoints are generally what we call "merged test runners", as they provide an executable runner project for multiple different test assemblies. These projects can be identified by the `<Import Project="$(TestSourceDir)MergedTestRunner.targets" />` line in their .csproj file. These projects provide a simple experience for running tests. When executing a merged runner project, it will run each test sequentially and record if it passes or fails in an xunit results file. The merged test runner support runtime test filtering. If specified, the first argument to the test runner is treated as a `dotnet test --filter` argument following the xUnit rules in their documentation. Today, the runner only supports the simple form, a substring of a test's fully-qualified name, in the format `Namespace.ContainingTypeName.TypeName.Method`. If support for further filtering options is desired, please open an issue requesting it.
140+
141+
Some tests need to be run in their own process as they interact with global process state, they have a custom test entrypoint, or they interact poorly with other tests in the same process. These tests are generally marked with `<RequiresProcessIsolation>true</RequiresProcessIsolation>` in their project files. These tests can be run directly, but they can also be invoked through their corresponding merged test runner. The merged test runner will invoke them as a subprocess in the same manner as if they were run individually.
142+
143+
#### The Standalone Test Runner and Build Time Test Filtering
144+
145+
Sometimes you may want to run a test with the least amount of code before actually executing the test. In addition to the merged test runner, we have another runner mode known as the "Standalone" runner. This runner is used by default in tests that require process isolation. This runner consists of a simple `try-catch` around executing each test sequentially, with no test results file or runtime test filtering.
146+
147+
To filter tests on a merged test runner built as standalone, you can set the `TestFilter` property, like so: `./dotnet.sh build -c Checked src/tests/path/to/test.csproj -p:TestFilter=SubstringOfFullyQualifiedTestName`. This mechanism supports the same filtering as the runtime test filtering. Using this mechanism will allow you to skip individual test cases at build time instead of at runtime.
148+
149+
#### Building all tests with the Standalone Runner
150+
151+
If you wish to use the Standalone runner described in the [previous section](#the-standalone-test-runner-and-build-time-test-filtering), you can set the `BuildAllTestsAsStandalone` environment variable to `true` when invoking the `./src/tests/build.sh` or `./src/tests/build.cmd` scripts (for example, `export BuildAllTestsAsStandalone=true` or `set BuildAllTestsAsStandalone=true`). This will build all tests that are not directly in a merged test runner's project as separate executable tests and build only the tests that are compiled into the runner directly. If a runner has no tests that are built directly into the runner, then it will be excluded.
152+
133153
### Building C++/CLI Native Test Components Against the Live Ref Assemblies
134154

135155
By default, the _C++/CLI_ native test components build against the _ref pack_ from the SDK specified in the `global.json` file in the root of the repository. To build these components against the _ref assemblies_ produced in the build, pass the `-cmakeargs -DCPP_CLI_LIVE_REF_ASSEMBLIES=1` parameters to the test build. For example:
@@ -233,9 +253,7 @@ cd path/to/JIT/Intrinsics/MathRoundDouble_ro
233253
./MathRoundDouble_ro.sh -coreroot=<repo_root>/artifacts/tests/coreclr/<OS>.<Arch>.<Configuration>/Tests/Core_Root
234254
```
235255

236-
#### Tests Without a Main Method
237-
238-
Guide on how to run tests without a `Main()` Method coming soon!
256+
If you want to run an individual test from a test runner, use the filtering capabilities described in the [Test Executors section](#test-executors).
239257

240258
### PAL Tests (macOS and Linux only)
241259

src/tests/Directory.Build.targets

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434

3535
<!-- Default priority building values. -->
3636
<PropertyGroup>
37-
<DisableProjectBuild Condition="'$(IsMergedTestRunnerAssembly)' == 'true' and $(BuildAsStandalone)">true</DisableProjectBuild>
38-
<OutputType Condition="('$(IsMergedTestRunnerAssembly)' == 'true' and !$(BuildAsStandalone)) or ('$(RequiresProcessIsolation)' == 'true' and '$(CLRTestKind)' != 'SharedLibrary')">Exe</OutputType>
37+
<DisableProjectBuild Condition="'$(IsMergedTestRunnerAssembly)' == 'true' and '$(BuildAllTestsAsStandalone)' == 'true' and '$(HasMergedInTests)' != 'true'">true</DisableProjectBuild>
38+
<OutputType Condition="('$(IsMergedTestRunnerAssembly)' == 'true' and '$(BuildAllTestsAsStandalone)' != 'true') or ('$(RequiresProcessIsolation)' == 'true' and '$(CLRTestKind)' != 'SharedLibrary')">Exe</OutputType>
3939

4040
<CLRTestKind Condition="'$(CLRTestKind)' == '' and '$(OutputType)' == 'Exe'">BuildAndRun</CLRTestKind>
4141
<CLRTestKind Condition="'$(CLRTestKind)' == ''">SharedLibrary</CLRTestKind>
@@ -441,7 +441,7 @@
441441

442442
<ItemGroup>
443443
<Content Include="$(AssemblyName).reflect.xml" Condition="Exists('$(AssemblyName).reflect.xml')" CopyToOutputDirectory="PreserveNewest" />
444-
<MarkerFile Include="$(IntermediateOutputPath)\$(MSBuildProjectName).MergedTestAssembly" Condition="'$(IsMergedTestRunnerAssembly)' == 'true'">
444+
<MarkerFile Include="$(IntermediateOutputPath)\$(MSBuildProjectName).MergedTestAssembly" Condition="'$(IsMergedTestRunnerAssembly)' == 'true' and '$(BuildAllTestsAsStandalone)' != 'true'">
445445
<Type>MergedTestAssembly</Type>
446446
</MarkerFile>
447447
<MarkerFile Include="$(IntermediateOutputPath)\$(AssemblyName).NoMonoAot" Condition="'$(MonoAotIncompatible)' == 'true'">

src/tests/Directory.Merged.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
22
<PropertyGroup>
33
<InMergedTestDirectory>true</InMergedTestDirectory>
4+
<BuildAsStandalone Condition="'$(BuildAllTestsAsStandalone)' == 'true'">true</BuildAsStandalone>
45
<BuildAsStandalone Condition="'$(BuildAsStandalone)' == ''">false</BuildAsStandalone>
56
</PropertyGroup>
67
</Project>

src/tests/Interop/Interop.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<Configurations>Debug;Release;Checked</Configurations>
77
<!-- Tracking issue: https://github.com/dotnet/runtime/issues/90427 -->
88
<CLRTestTargetUnsupported Condition="'$(RuntimeFlavor)' == 'mono' and '$(RuntimeVariant)' == 'minifullaot'">true</CLRTestTargetUnsupported>
9+
<HasMergedInTests>true</HasMergedInTests>
910
</PropertyGroup>
1011
<ItemGroup>
1112
<SupportProject Include="$(TestLibraryProjectPath)" />

src/tests/Interop/StringMarshalling/AnsiBSTR/AnsiBStrTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
</ItemGroup>
1414
<ItemGroup>
1515
<ProjectReference Include="$(TestLibraryProjectPath)" />
16+
<CMakeProjectReference Include="./CMakeLists.txt" />
1617
</ItemGroup>
1718
</Project>

src/tests/Interop/StringMarshalling/LPTSTR/LPTSTRTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
</ItemGroup>
1313
<ItemGroup>
1414
<ProjectReference Include="$(TestLibraryProjectPath)" />
15+
<CMakeProjectReference Include="./CMakeLists.txt" />
1516
</ItemGroup>
1617
</Project>

src/tests/Interop/StringMarshalling/UTF8/UTF8Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
</ItemGroup>
88
<ItemGroup>
99
<ProjectReference Include="$(TestLibraryProjectPath)" />
10+
<CMakeProjectReference Include="./CMakeLists.txt" />
1011
</ItemGroup>
1112
</Project>

src/tests/Interop/StringMarshalling/VBByRefStr/VBByRefStrTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
</PropertyGroup>
77
<ItemGroup>
88
<Compile Include="*.cs" />
9+
<CMakeProjectReference Include="./CMakeLists.txt" />
910
</ItemGroup>
1011
</Project>

src/tests/MergedTestRunner.targets

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
<!-- string.Concat("_", string.Copy("%(Filename)").Replace("-", "_").Replace(".", "_")) -->
1818
<MergedWrapperProjectReference Update="**" Aliases="$([System.String]::Concat(&quot;_&quot;,$([System.String]::Copy(&quot;%(Filename)&quot;).Replace(&quot;-&quot;,&quot;_&quot;).Replace(&quot;.&quot;,&quot;_&quot;))))" />
1919

20-
<ProjectReference Include="@(MergedWrapperProjectReference)" />
20+
<!--
21+
If the merged test runner was specified to be built as Standalone, don't force the referenced projects to be built that way as well.
22+
If we're building all tests as standalone, then don't reference the other test projects from the merged wrapper. We'll only build the tests in the wrapper itself
23+
into it.
24+
-->
25+
<ProjectReference Include="@(MergedWrapperProjectReference)" UndefineProperties="BuildAsStandalone" Condition="'$(BuildAllTestsAsStandalone)' != 'true'" />
2126
</ItemGroup>
2227
</Project>

src/tests/build.proj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@
567567

568568
<Target Name="BuildManagedTestGroups" DependsOnTargets="RestorePackages;ResolveDisabledProjects;BuildNativeAotFrameworkObjects">
569569
<Message Importance="High" Text="$(MsgPrefix)Building managed test components" />
570+
<Warning Text="Building the whole test suite with the BuildAsStandalone=true environment variable will cause some tests to be executed twice. Use the BuildAllTestsAsStandalone=true environment variable instead." Condition="'$(BuildAsStandalone)' == 'true'" />
570571
<!-- Execute msbuild test build in stages - workaround for excessive data retention in MSBuild ConfigCache -->
571572
<!-- See https://github.com/Microsoft/msbuild/issues/2993 -->
572573

0 commit comments

Comments
 (0)