-
Notifications
You must be signed in to change notification settings - Fork 834
Description
This API leads to bad coding patterns for test code. Test which has a dependency on the organization of source code is subject to break when running in a sandbox like Helix or when the source code is reorganized.
Let's obsolete this API and update test code to use a better pattern.
Motivation
A common cause of tests failing in Helix is due to undeclared dependencies on test assets. Distributed testing requires that test projects define their dependencies on test content which should exist in the test sandbox.
The better pattern
Generally, two kinds of tests rely on source code.
- Tests which build/run/read source code
- Tests which assert code generation is up to date
Tests which build/run/read source code
Tests which rely on the presence of source code that can be build, run, or read during test execution should be updated to declare their dependency on this content. To update tests of this nature,
- Edit the .csproj file for the test to declare which source files are required to be present on disk for the test to run
<ItemGroup> <Content Include="..\testassets\**\*" /> </ItemGroup>
- Update test code to look for these files in
System.AppContext.BaseDirectory
.
- var testCode = Path.Combine(TestPathUtilities.GetRepoRoot("Extensions"), "path", "to", "testassets");
+ var testCode = Path.Combine(AppContext.BaseDirectory, "testassets");
Tests which assert code generation is up to date
Tests of this nature should be moved to run during eng/scripts/CodeCheck.ps1. This script is meant to ensure that all generated content which is commit to source is up to date with the inputs that generates it.
Tip: consider making your code generation run on-build instead of checking in the result. This is not always possible, but when it is, it removes the need to have the test at all :)