Skip to content

Commit bbbc5d2

Browse files
committed
docs: tunit and some adoptions for xunit.v3
1 parent 9080ccd commit bbbc5d2

File tree

9 files changed

+103
-68
lines changed

9 files changed

+103
-68
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,6 @@ jobs:
206206
dotnet restore ${{ github.workspace }}/TemplateTestMstest --source https://api.nuget.org/v3/index.json --source ${{ env.NUGET_DIRECTORY }}
207207
dotnet test ${{ github.workspace }}/TemplateTestMstest
208208
209-
- name: ✔ Verify TUnit template
210-
run: |
211-
dotnet new bunit --framework tunit --no-restore -o ${{ github.workspace }}/TemplateTestTUnit
212-
echo '<?xml version="1.0" encoding="utf-8"?><Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"></Project>' >> ${{ github.workspace }}/TemplateTestTUnit/Directory.Build.props
213-
echo '<Project><PropertyGroup><ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally></PropertyGroup></Project>' >> ${{ github.workspace }}/TemplateTestTUnit/Directory.Packages.props
214-
dotnet restore ${{ github.workspace }}/TemplateTestTUnit --source https://api.nuget.org/v3/index.json --source ${{ env.NUGET_DIRECTORY }}
215-
dotnet test ${{ github.workspace }}/TemplateTestTUnit
216-
217209
validate-docs:
218210
runs-on: ubuntu-latest
219211
steps:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
- Pass parameters, cascading values and inject services into components under test
1313
- Mock `IJSRuntime`, Blazor authentication and authorization, and others
1414

15-
bUnit builds on top of existing unit testing frameworks such as xUnit, NUnit, and MSTest, which run the Blazor component tests in just the same way as any normal unit test. bUnit runs a test in milliseconds, compared to browser-based UI tests which usually take seconds to run.
15+
bUnit builds on top of existing unit testing frameworks such as xUnit, NUnit, MSTest and TUnit, which run the Blazor component tests in just the same way as any normal unit test. bUnit runs a test in milliseconds, compared to browser-based UI tests which usually take seconds to run.
1616

1717
**Go to [bUnit.dev](https://bunit.dev) to learn more.**
1818

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

Lines changed: 95 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ These steps look like this from the `dotnet` CLI:
2525

2626
Install the template from NuGet using this command:
2727

28-
```dotnetcli
29-
dotnet new --install bunit.template
30-
```
31-
32-
Or, since .NET 7 onwards:
33-
3428
```dotnetcli
3529
dotnet new install bunit.template
3630
```
@@ -49,12 +43,19 @@ the framework of your choice:
4943
dotnet new bunit --framework xunit -o <NAME OF TEST PROJECT>
5044
```
5145

46+
# [xUnit v3](#tab/xunitv3)
47+
48+
```dotnetcli
49+
dotnet new bunit --framework xunitv3 -o <NAME OF TEST PROJECT>
50+
```
51+
5252
# [NUnit](#tab/nunit)
5353

5454
```dotnetcli
5555
dotnet new bunit --framework nunit -o <NAME OF TEST PROJECT>
5656
```
5757

58+
5859
# [MSTest](#tab/mstest)
5960

6061
```dotnetcli
@@ -65,7 +66,7 @@ dotnet new bunit --framework mstest -o <NAME OF TEST PROJECT>
6566

6667
The `--framework` option in the `dotnet new` command above is used to specify the unit testing framework used by the test project. If the `--framework` option is omitted, the default test framework `xunit` will be configured. Currently supported options are the following:
6768

68-
- `xunit` - [xUnit](https://xunit.net/),
69+
- `xunit` and `xunitv3` - [xUnit](https://xunit.net/),
6970
- `nunit` - [NUnit](https://nunit.org/),
7071
- `mstest` - [MSTest](https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-with-mstest)
7172

@@ -84,7 +85,7 @@ This will allow the test project to see and test the components in the component
8485

8586
This section will take you through the steps required to create a project for testing Blazor components using bUnit. Any of the three general-purpose test frameworks shown in step 1 below can be used. Briefly, here is what we will do:
8687

87-
1. Create a new xUnit/NUnit/MSTest testing project
88+
1. Create a new xUnit/NUnit/MSTest/TUnit testing project
8889
2. Add bUnit to the test project
8990
3. Configure project settings
9091
4. Add the test project to your existing solution
@@ -101,6 +102,12 @@ Use the following command (_click on the tab for the test framework of choice_):
101102
dotnet new xunit -o <NAME OF TEST PROJECT>
102103
```
103104

105+
# [xUnit v3](#tab/xunitv3)
106+
107+
```dotnetcli
108+
dotnet new xunit3 -o <NAME OF TEST PROJECT>
109+
```
110+
104111
# [NUnit](#tab/nunit)
105112

106113
```dotnetcli
@@ -113,6 +120,12 @@ dotnet new nunit -o <NAME OF TEST PROJECT>
113120
dotnet new mstest -o <NAME OF TEST PROJECT>
114121
```
115122

123+
# [TUnit](#tab/tunit)
124+
125+
```dotnetcli
126+
dotnet new TUnit -o <NAME OF TEST PROJECT>
127+
```
128+
116129
***
117130

118131
The `-o` option in the `dotnet new` command above is used to specify the name of the test project.
@@ -130,11 +143,11 @@ dotnet add package bunit --version #{NBGV_NuGetPackageVersion}#
130143

131144
The test projects setting needs to be set to the following:
132145

133-
- the project's SDK needs to be set to `Microsoft.NET.Sdk.Razor`
146+
- 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)
134147
- set the `<TargetFramework>` to `net8.0`
135148

136149
> [!NOTE]
137-
> bUnit works with `net7.0`, `net6.0`, `net5.0` and `netcoreapp3.1`/`netstandard2.1` test projects as well.
150+
> bUnit works with `net8.0` and above as well.
138151
139152
To do so, change the first part of the test projects `.csproj` file to look like this.:
140153

@@ -172,13 +185,43 @@ The result should be a test project with a `.csproj` that looks like this (non b
172185

173186
<ItemGroup>
174187
<PackageReference Include="bunit" Version="#{RELEASE_VERSION}#" />
175-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
176-
<PackageReference Include="xunit" Version="2.8.1" />
177-
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
188+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
189+
<PackageReference Include="xunit" Version="2.9.4" />
190+
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1">
191+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
192+
<PrivateAssets>all</PrivateAssets>
193+
</PackageReference>
194+
<PackageReference Include="coverlet.collector" Version="6.0.4" />
195+
</ItemGroup>
196+
197+
<ItemGroup>
198+
<ProjectReference Include="<PATH TO COMPONENT LIB>.csproj" />
199+
</ItemGroup>
200+
201+
</Project>
202+
```
203+
204+
# [xUnit v3](#tab/xunitv3)
205+
206+
```xml
207+
<Project Sdk="Microsoft.NET.Sdk.Razor">
208+
209+
<PropertyGroup>
210+
<TargetFramework>net8.0</TargetFramework>
211+
<Nullable>enable</Nullable>
212+
<IsPackable>false</IsPackable>
213+
<OutputType>Exe</OutputType>
214+
</PropertyGroup>
215+
216+
<ItemGroup>
217+
<PackageReference Include="bunit" Version="#{RELEASE_VERSION}#" />
218+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
219+
<PackageReference Include="xunit.v3" Version="1.0.1" />
220+
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1">
178221
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
179222
<PrivateAssets>all</PrivateAssets>
180223
</PackageReference>
181-
<PackageReference Include="coverlet.collector" Version="6.0.0" />
224+
<PackageReference Include="coverlet.collector" Version="6.0.4" />
182225
</ItemGroup>
183226

184227
<ItemGroup>
@@ -201,10 +244,10 @@ The result should be a test project with a `.csproj` that looks like this (non b
201244

202245
<ItemGroup>
203246
<PackageReference Include="bunit" Version="#{RELEASE_VERSION}#" />
204-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
205-
<PackageReference Include="NUnit" Version="3.14.0" />
206-
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
207-
<PackageReference Include="coverlet.collector" Version="6.0.0" />
247+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
248+
<PackageReference Include="NUnit" Version="4.3.2" />
249+
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
250+
<PackageReference Include="coverlet.collector" Version="6.0.4" />
208251
</ItemGroup>
209252

210253
<ItemGroup>
@@ -227,10 +270,35 @@ The result should be a test project with a `.csproj` that looks like this (non b
227270

228271
<ItemGroup>
229272
<PackageReference Include="bunit" Version="#{RELEASE_VERSION}#" />
230-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
231-
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
232-
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
233-
<PackageReference Include="coverlet.collector" Version="6.0.0" />
273+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
274+
<PackageReference Include="MSTest.TestAdapter" Version="3.7.1" />
275+
<PackageReference Include="MSTest.TestFramework" Version="3.7.1" />
276+
<PackageReference Include="coverlet.collector" Version="6.0.4" />
277+
</ItemGroup>
278+
279+
<ItemGroup>
280+
<ProjectReference Include="<PATH TO COMPONENT LIB>.csproj" />
281+
</ItemGroup>
282+
283+
</Project>
284+
```
285+
286+
# [TUnit](#tab/tunit)
287+
288+
```xml
289+
<Project Sdk="Microsoft.NET.Sdk">
290+
291+
<PropertyGroup>
292+
<TargetFramework>net8.0</TargetFramework>
293+
<Nullable>enable</Nullable>
294+
<IsPackable>false</IsPackable>
295+
<IsTestingPlatformApplication>false</IsTestingPlatformApplication>
296+
</PropertyGroup>
297+
298+
<ItemGroup>
299+
<PackageReference Include="bunit" Version="#{RELEASE_VERSION}#" />
300+
<PackageReference Include="TUnit" Version="0.6.154" />
301+
<PackageReference Include="coverlet.collector" Version="6.0.4" />
234302
</ItemGroup>
235303

236304
<ItemGroup>
@@ -240,6 +308,11 @@ The result should be a test project with a `.csproj` that looks like this (non b
240308
</Project>
241309
```
242310

311+
> [!WARNING]
312+
> **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
313+
314+
***
315+
243316
## Further reading
244317

245318
To start creating tests, continue reading the <xref:writing-tests> page.

docs/site/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ title: bUnit - a testing library for Blazor components
1717
- Pass parameters, cascading values and inject services into components under test
1818
- Mock `IJSRuntime`, Blazor authentication and authorization, and others
1919

20-
bUnit builds on top of existing unit testing frameworks such as xUnit, NUnit, and MSTest, which run the Blazor components tests in just the same way as any normal unit test. bUnit runs a test in milliseconds, compared to browser-based UI tests which usually take seconds to run.
20+
bUnit builds on top of existing unit testing frameworks such as xUnit, NUnit, MSTest and TUnit, which run the Blazor components tests in just the same way as any normal unit test. bUnit runs a test in milliseconds, compared to browser-based UI tests which usually take seconds to run.
2121

2222
**Go to the [Documentation](xref:getting-started) pages to learn more.**
2323

src/bunit.template/bunit.template.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,4 @@
4141
<None Remove="template\CounterRazorTests.razor" />
4242
</ItemGroup>
4343

44-
<ItemGroup>
45-
<Folder Include="temp\" />
46-
</ItemGroup>
47-
4844
</Project>

src/bunit.template/template/.template.config/dotnetcli.host.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"--framework xunit --sdk net8.0",
1818
"--framework xunitv3 --sdk net8.0",
1919
"--framework nunit --sdk net8.0",
20-
"--framework mstest --sdk net8.0",
21-
"--framework tunit --sdk net8.0"
20+
"--framework mstest --sdk net8.0"
2221
]
2322
}

src/bunit.template/template/.template.config/template.json

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
{
2424
"modifiers": [
2525
{
26-
"exclude": [ "_Imports.razor", "CounterRazorTests.razor" ],
27-
"condition": "(testFramework_tunit)"
26+
"exclude": [ "BunitTestContext.cs" ],
27+
"condition": "(testFramework_xunit || testFramework_xunitv3)"
2828
}
2929
]
3030
}
@@ -67,11 +67,6 @@
6767
"choice": "mstest",
6868
"description": "MSTest unit testing framework",
6969
"displayName": "MSTest"
70-
},
71-
{
72-
"choice": "tunit",
73-
"description": "TUnit unit testing framework",
74-
"displayName": "TUnit"
7570
}
7671
]
7772
},
@@ -91,10 +86,6 @@
9186
"type": "computed",
9287
"value": "UnitTestFramework == \"mstest\""
9388
},
94-
"testFramework_tunit": {
95-
"type": "computed",
96-
"value": "UnitTestFramework == \"tunit\""
97-
},
9889
"targetSdk": {
9990
"type": "parameter",
10091
"description": "The target framework sdk for the project.",

src/bunit.template/template/Company.BlazorTests1.csproj

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
<!--#if (testFramework_tunit) -->
2-
<Project Sdk="Microsoft.NET.Sdk">
3-
<!--#else -->
41
<Project Sdk="Microsoft.NET.Sdk.Razor">
5-
<!--#endif -->
2+
63
<PropertyGroup>
74
<TargetFramework>targetSdk</TargetFramework>
85
<Nullable>enable</Nullable>
96
<IsPackable>false</IsPackable>
10-
<OutputType Condition="'$(testFramework_tunit)' == 'true'">Exe</OutputType>
11-
<IsTestingPlatformApplication Condition="'$(testFramework_tunit)' == 'true'">false</IsTestingPlatformApplication>
127
</PropertyGroup>
138

149
<ItemGroup>
@@ -23,8 +18,8 @@
2318

2419
<ItemGroup>
2520
<PackageReference Include="bunit" Version="#{RELEASE_VERSION}#" />
26-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" Condition="'$(testFramework_tunit)' != 'true'"/>
27-
<PackageReference Include="coverlet.collector" Version="6.0.3" Condition="'$(testFramework_tunit)' != 'true'">
21+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
22+
<PackageReference Include="coverlet.collector" Version="6.0.3">
2823
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2924
<PrivateAssets>all</PrivateAssets>
3025
</PackageReference>
@@ -56,8 +51,4 @@
5651
<PackageReference Include="MSTest.TestFramework" Version="3.6.4" />
5752
</ItemGroup>
5853

59-
<ItemGroup Condition="'$(testFramework_tunit)' == 'true'">
60-
<PackageReference Include="TUnit" Version="0.6.154" />
61-
</ItemGroup>
62-
6354
</Project>

src/bunit.template/template/CounterCSharpTest.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
#if (testFramework_tunit)
2-
using TUnit;
3-
#endif
4-
51
namespace Company.BlazorTests1;
62

73
/// <summary>
@@ -18,9 +14,6 @@ public class CounterCSharpTest : BunitContext
1814
#elif (testFramework_mstest)
1915
[TestClass]
2016
public class CounterCSharpTest : BunitContext
21-
#elif (testFramework_tunit)
22-
[Test]
23-
public class CounterCSharpTest : BunitContext
2417
#endif
2518
{
2619
#if (testFramework_xunit)

0 commit comments

Comments
 (0)