docs/design/features/host-testing.md
The hosting layer of .NET Core consists of several native libraries and executables (entirely written in C++). The full end to end features sometimes require code in the coreclr repo, but testing of that is outside of the scope of this document. For description of the various hosting components, please see host-components.
Testing these comes with certain challenges which this document tries to address.
Almost all tests for the hosting components are currently basically End-to-End tests which execute the entire product (with very limited amount of "mocking").
This is almost all tests in the HostActivation test project currently.
The tests prepare folders on the disk which emulate the product installation by having
dotnet or apphosthostfxr in the right placeFor the most part all these are real components. The shared frameworks contain a real Microsoft.NETCore.App copy, only higher level frameworks are "mocked" by creating only the necessary files.
Almost all tests then execute a test application using this test product installation and observe the behavior of the app. This is done by
Pros:
Cons:
Going forward we would keep these tests and add new ones to provide true End-to-End coverage.
Small portion of the tests actually call specific exports on hostfxr or hostpolicy directly to test these. All these tests are in the HostActivation project under the NativeHostApis test class.
These tests use a special test application (managed) which invokes the selected exports through PInvokes and performs the testing. The HostActivation test only prepares this app and executes it, looking for pieces of its output to verify the outcome.
Ideally we would migrate these over time to the proposed Component tests infra.
Pros:
Cons:
Add the ability to write true unit tests. So tests are written in C++ which can directly call methods/classes from the product. These tests would be compiled as native executables. For simplicity of the build system, they would live in the product source tree (src folder) and would be compiled during the product build. They would combine the test source files with product source files into one executable. The intent is to keep them in a separate subdirectory with clear separation from the product, just the build would be used by both.
For integration purposes there would be managed wrappers in the HostActivation test project which would just call the native executable and check the exit code. As this is the simplest way to integrate these tests into all our test infrastructure.
Unit tests should be used to test self-contained bits of functionality. So for example helper classes and such.
First unit test is being introduced in this PR: dotnet/core-setup#4953.
Pros:
Cons:
Testing larger functionality blocks is probably easier by testing entire components.
These tests would call entry points on the host components directly (so hostfxr and hostpolicy mostly) and observe the resulting behavior. The tests would not try to emulate real product installation. Instead they would rely on mocks.
The core of this proposed test approach is
hostfxr for example) is loaded directly into the test process - so it would be running on some toolset version of .NET Core and not the tested version of .NET Core - but that should not matter as all of that should be abstracted out.coreclr) as necessaryProduct changes would be required to allow for mocking OS services. This would be achieved by having test-only exports/env variables which would turn on the mocking. For example file system mocking would be done by abstracting file system access through and interface and having an export which would let the test provide custom implementation of that interface. There are two way to do this:
Pros:
Cons: