Skip to content

Commit ef61b9d

Browse files
committed
Updates doc to explain how to enable .razor files with TUnit
Explains that TUnit only works with Razor files in reflection mode due to source generator limitations. Provides instructions for setting up TUnit with .razor files using a .runsettings file and updating the .csproj. Adds a note recommending a separate test project for bUnit tests to avoid performance issues.
1 parent 715c554 commit ef61b9d

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

docs/site/docs/getting-started/create-test-project.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ dotnet add package bunit --version #{NBGV_NuGetPackageVersion}#
147147

148148
The test projects setting needs to be set to the following:
149149

150-
- the project's SDK needs to be set to `Microsoft.NET.Sdk.Razor` (this does not work with **TUnit** - a more detailed explanation can be found below)
150+
- the project's SDK needs to be set to `Microsoft.NET.Sdk.Razor` (for **TUnit** this only works in reflection mode - a more detailed explanation can be found below)
151151
- set the `<TargetFramework>` to `net8.0`
152152

153153
> [!NOTE]
@@ -313,7 +313,39 @@ The result should be a test project with a `.csproj` that looks like this (non b
313313
```
314314

315315
> [!WARNING]
316-
> **TUnit** and the `Microsoft.NET.Sdk.Razor` both utilize source code generators. Source generators can not see or interact with the output of another generator. Therefore **TUnit** does not work with `razor` files. Using `cs` based tests is working perfectly fine. For more information regarding the setup of **TUnit** head over to: https://github.com/thomhurst/TUnit
316+
> **TUnit** and the `Microsoft.NET.Sdk.Razor` SDK both utilize source code generators. Source generators cannot see or interact with the output of another generator. Therefore **TUnit** only works with `.razor` files in **reflection mode**. However `.cs`-based tests work as expected in the default mode. For more information regarding the setup of **TUnit** head over to: https://github.com/thomhurst/TUnit
317+
318+
To set up TUnit for use with `.razor` files, the `TUNIT_EXECUTION_MODE` environment variable must be set to `reflection` so that the TUnit test runner can discover these tests.
319+
320+
A straightforward way to configure this is by creating a `.runsettings` file in the test project and referencing it from the `.csproj`.
321+
322+
**.runsettings**
323+
324+
```xml
325+
<?xml version="1.0" encoding="utf-8"?>
326+
<RunSettings>
327+
<RunConfiguration>
328+
<EnvironmentVariables>
329+
<TUNIT_EXECUTION_MODE>reflection</TUNIT_EXECUTION_MODE>
330+
</EnvironmentVariables>
331+
</RunConfiguration>
332+
</RunSettings>
333+
```
334+
335+
Add the following `RunSettingsFilePath` property to the `.csproj`:
336+
337+
```xml
338+
<PropertyGroup>
339+
<RunSettingsFilePath>$(MSBuildProjectDirectory)\.runsettings</RunSettingsFilePath>
340+
</PropertyGroup>
341+
```
342+
343+
With this setup, all bUnit `.razor` tests are discovered and run consistently in Visual Studio and other supported environments.
344+
345+
> [!NOTE]
346+
> Running in reflection mode is slower than the default execution mode. It is therefore recommended to keep bUnit tests in a separate test project, so they don’t affect the performance of other test runs.
347+
348+
For more information on reflection mode, see: https://tunit.dev/docs/execution/engine-modes/.
317349

318350
***
319351

0 commit comments

Comments
 (0)