Skip to content

Commit 70609eb

Browse files
committed
Updated create a test project page
1 parent be5e934 commit 70609eb

7 files changed

Lines changed: 232 additions & 136 deletions

File tree

docs/docs/create-test-project.md

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
---
2+
uid: create-test-project
3+
title: Creating a new bUnit test project
4+
---
5+
6+
# Creating a new bUnit test project
7+
8+
Before you can write any tests, you need a place to put them - a test project. bUnit is not a unit test runner, so you need a general purpose test framework, like xUnit, NUnit, or MSTest, in addition to bUnit, to run your tests, and write your assertions.
9+
10+
If you prefer xUnit, you can use the bUnit project template approached described in the [Create a test project with bUnit template](#create-a-test-project-with-bunit-template) section further down the page. If you want to use another general purpose testing framework, read the following section.
11+
12+
## Create a test project manually
13+
14+
To create a project for testing you Blazor components that uses either of three general purpose test frameworks, you need to go through these steps:
15+
16+
1. Create a new xUnit/NUnit/MSTest testing project
17+
2. Add bUnit to the test project
18+
3. Change project type to `Microsoft.NET.Sdk.Razor`
19+
4. Add the test project to your solution
20+
21+
These steps look like this from the `dotnet` CLI:
22+
23+
**1. Create a new test project**
24+
25+
Use the following command:
26+
27+
# [xUnit](#tab/xunit)
28+
29+
```bash
30+
dotnet new xunit -o <NAME OF TEST PROJECT>
31+
```
32+
33+
# [NUnit](#tab/nunit)
34+
35+
```bash
36+
dotnet new nunit -o <NAME OF TEST PROJECT>
37+
```
38+
39+
# [MSTest](#tab/mstest)
40+
41+
```bash
42+
dotnet new mstest -o <NAME OF TEST PROJECT>
43+
```
44+
45+
***
46+
47+
where `-o <NAME OF PROJECT>` is used to name the test project.
48+
49+
**2. Add bUnit to the test project**
50+
51+
To add bUnit to your test project, first change to the newly created test projects folder, and then use the following command:
52+
53+
# [xUnit](#tab/xunit)
54+
55+
```bash
56+
cd <NAME OF PROJECT>
57+
dotnet add package bunit.web --version 1.0.0-beta-7#{VERSION}
58+
dotnet add package bunit.xunit --version 1.0.0-beta-7#{VERSION}#
59+
```
60+
61+
# [NUnit](#tab/nunit)
62+
63+
```bash
64+
cd <NAME OF PROJECT>
65+
dotnet add package bunit.web --version 1.0.0-beta-7#{VERSION}#
66+
```
67+
68+
# [MSTest](#tab/mstest)
69+
70+
```bash
71+
cd <NAME OF PROJECT>
72+
dotnet add package bunit.web --version 1.0.0-beta-7#{VERSION}#
73+
```
74+
75+
***
76+
77+
**3. Change project type to `Microsoft.NET.Sdk.Razor`**
78+
79+
Change the first line in project type in the test project's `.csproj` file to:
80+
81+
`<Project Sdk="Microsoft.NET.Sdk.Razor">`
82+
83+
**4. Add the test project to your solution**
84+
85+
Then you need to add your test project to your solution (`.sln`) and add a reference between your test project and component project:
86+
87+
```bash
88+
dotnet sln <NAME OF PROJECT>.sln add <NAME OF TEST PROJECT>
89+
dotnet add <NAME OF COMPONENT PROJECT>.csproj reference <NAME OF TEST PROJECT>.csproj
90+
```
91+
92+
The end result should be a test project with a `.csproj` that looks like this (other packages than bUnit might have different version numbers):
93+
94+
95+
# [xUnit](#tab/xunit)
96+
97+
```xml
98+
<Project Sdk="Microsoft.NET.Sdk.Razor">
99+
100+
<PropertyGroup>
101+
<TargetFramework>netcoreapp3.1</TargetFramework>
102+
<RazorLangVersion>3.0</RazorLangVersion>
103+
</PropertyGroup>
104+
105+
<ItemGroup>
106+
<PackageReference Include="bunit.web" Version="#{VERSION}#" />
107+
<PackageReference Include="bunit.xunit" Version="#{VERSION}#" />
108+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
109+
<PackageReference Include="xunit" Version="2.4.1" />
110+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
111+
<PrivateAssets>all</PrivateAssets>
112+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
113+
</PackageReference>
114+
</ItemGroup>
115+
116+
</Project>
117+
```
118+
119+
# [NUnit](#tab/nunit)
120+
121+
```xml
122+
<Project Sdk="Microsoft.NET.Sdk.Razor">
123+
124+
<PropertyGroup>
125+
<TargetFramework>netcoreapp3.1</TargetFramework>
126+
<IsPackable>false</IsPackable>
127+
</PropertyGroup>
128+
129+
<ItemGroup>
130+
<PackageReference Include="bunit.web" Version="#{VERSION}#" />
131+
<PackageReference Include="nunit" Version="3.12.0" />
132+
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1">
133+
<PrivateAssets>all</PrivateAssets>
134+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
135+
</PackageReference>
136+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
137+
</ItemGroup>
138+
139+
<ItemGroup>
140+
<ProjectReference Include="<PATH TO COMPONENT LIB>.csproj" />
141+
</ItemGroup>
142+
143+
</Project>
144+
```
145+
146+
# [MSTest](#tab/mstest)
147+
148+
```xml
149+
<Project Sdk="Microsoft.NET.Sdk.Razor">
150+
151+
<PropertyGroup>
152+
<TargetFramework>netcoreapp3.1</TargetFramework>
153+
<IsPackable>false</IsPackable>
154+
</PropertyGroup>
155+
156+
<ItemGroup>
157+
<PackageReference Include="bunit.web" Version="#{VERSION}#" />
158+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
159+
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
160+
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
161+
<PackageReference Include="coverlet.collector" Version="1.2.0" />
162+
</ItemGroup>
163+
164+
<ItemGroup>
165+
<ProjectReference Include="<PATH TO COMPONENT LIB>.csproj" />
166+
</ItemGroup>
167+
168+
</Project>
169+
```
170+
171+
***
172+
173+
## Create a test project with bUnit template
174+
175+
If you want to skip a few steps in the guide above, you can use the [bUnit test project template](https://www.nuget.org/packages/bunit.template/). The bUNit project template is only available for using with xUnit as the general purpose testing framework, but that will change in the future.
176+
177+
The steps are as follows:
178+
179+
1. Install the template (only needed the first time)
180+
2. Create a new test project
181+
3. Add the test project to your solution
182+
183+
184+
These steps look like this from the `dotnet` CLI:
185+
186+
**1. Install the template**
187+
188+
Install the template from NuGet using this command:
189+
190+
```bash
191+
dotnet new --install bunit.template::#{VERSION}#
192+
```
193+
194+
**2. Create a new test project**
195+
196+
Use the following command to create a bUnit with xUnit test project:
197+
198+
```bash
199+
dotnet new bunit -o <NAME OF TEST PROJECT>
200+
```
201+
202+
where `-o <NAME OF PROJECT>` is used to name the test project.
203+
204+
**3. Add the test project to your solution**
205+
206+
Then you need to add your test project to your solution (`.sln`) and add a reference between your test project and component project:
207+
208+
```bash
209+
dotnet sln <NAME OF PROJECT>.sln add <NAME OF TEST PROJECT>
210+
dotnet add <NAME OF COMPONENT PROJECT>.csproj reference <NAME OF TEST PROJECT>.csproj
211+
```
212+
213+
## Further reading
214+
215+
- [Miscellaneous bUnit testing tips](/docs/misc-test-tips.html)

docs/docs/creating-a-new-bunit-mstest-project.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/docs/creating-a-new-bunit-nunit-project.md

Lines changed: 0 additions & 65 deletions
This file was deleted.

docs/docs/creating-a-new-bunit-xunit-project.md

Lines changed: 0 additions & 58 deletions
This file was deleted.

docs/docs/index.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ The sections below takes you through each of the steps you need to get started:
66

77
## Creating a test project
88

9-
First you need a test project to store your component tests inside. Depending on what general purpose unit testing framework you prefer, you have a few options:
10-
11-
- [Creating a new bUnit and xUnit test project](/docs/creating-a-new-bunit-xunit-project.html)
12-
- [Creating a new bUnit and NUnit test project](/docs/creating-a-new-bunit-nunit-project.html)
13-
- [Creating a new bUnit and MSTest test project](/docs/creating-a-new-bunit-mstest-project.html)
9+
First you need a test project to store your component tests inside. This is covered on the <xref:create-test-project> page.
1410

1511
## The basics of testing Blazor components
1612

docs/docs/toc.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
# [Getting Started](index.md)
2-
## [Creating a new bUnit and xUnit test project](creating-a-new-bunit-xunit-project.md)
3-
## [Creating a new bUnit and NUnit test project](creating-a-new-bunit-nunit-project.md)
4-
## [Creating a new bUnit and MSTest test project](creating-a-new-bunit-mstest-project.md)
5-
## [Miscellaneous bUnit testing tips](/docs/misc-test-tips.md)
6-
## [Basics of Blazor component testing](basics-of-blazor-component-testing.md)
2+
# [Create a test project](xref:create-test-project)
3+
# [Basics of Blazor component testing](basics-of-blazor-component-testing.md)
74
# [C# based testing](csharp-based-testing.md)
85
# [Razor based testing](razor-based-testing.md)
96
# [Snapshot testing](snapshot-testing.md)
107
# [Semantic HTML markup comparison](semantic-html-markup-comparison.md)
118
# [Mocking JsRuntime](mocking-jsruntime.md)
9+
# [Miscellaneous bUnit testing tips](/docs/misc-test-tips.md)
1210
# [Tutorials and Presentations](tutorials-and-presentations.md)
1311
# [Contribute](contribute.md)

docs/templates/bunit/styles/main.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,17 @@
2424

2525
.sideaffix {
2626
margin-top: 0;
27+
}
28+
29+
.tabGroup {
30+
margin-bottom: 1rem;
31+
}
32+
33+
.tabGroup section pre:only-child {
34+
border: none;
35+
margin-top: -16px !important;
36+
}
37+
38+
pre code {
39+
white-space: pre;
2740
}

0 commit comments

Comments
 (0)