Skip to content

Commit 25da511

Browse files
pBouillonegil
andauthored
docs: rework the create a test project page (#877)
* Retrieve the modifications from #875 * Update docs/site/docs/getting-started/create-test-project.md Co-authored-by: Egil Hansen <[email protected]> * Update docs/site/docs/getting-started/create-test-project.md Co-authored-by: Egil Hansen <[email protected]> * Refrase the parts related to .NET 7 and the project creation * Remove duplicated space * Refrase the mentions of the section moved upwards Co-authored-by: Egil Hansen <[email protected]>
1 parent a9c05ad commit 25da511

File tree

1 file changed

+78
-70
lines changed

1 file changed

+78
-70
lines changed

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

Lines changed: 78 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,78 @@ title: Creating a new bUnit test project
77

88
To write tests, you need a place to put them - a test project. bUnit is not a unit test runner, so a general-purpose test framework like xUnit, NUnit, or MSTest is needed in addition to bUnit in order to write and run tests.
99

10-
To use bUnit, the easiest approach is to use the bUnit project template described in the [Create a test project with bUnit template](#creating-a-test-project-with-bunit-template) section further down the page. To create a test project manually and in a general-purpose testing frameworks agnostic way, read the following section.
10+
To use bUnit, the easiest approach is to use the bUnit project template described in the following section. To create a test project manually and in a general-purpose testing frameworks agnostic way, read the section [Create a new test project](#create-a-new-test-project) section further down the page.
11+
12+
## Creating a test project with bUnit template
13+
14+
To quickly get started with bUnit, install and use the [bUnit test project template](https://www.nuget.org/packages/bunit.template/).
15+
16+
The steps for creating a test project with the bUnit template are as follows:
17+
18+
1. Install the template (only needed the first time)
19+
2. Create a new bUnit test project
20+
3. Add the test project to your solution
21+
22+
These steps look like this from the `dotnet` CLI:
23+
24+
**1. Install the template**
25+
26+
Install the template from NuGet using this command:
27+
28+
```dotnetcli
29+
dotnet new --install bunit.template
30+
```
31+
32+
Or, since .NET 7 onwards:
33+
34+
```dotnetcli
35+
dotnet new install bunit.template
36+
```
37+
38+
**2. Create a new test project**
39+
40+
If you successfully installed the template listed in the previous section, you
41+
can create a new project directly from the "Create new project" wizard in Visual Studio (or Rider), where the bUnit project type will also show up.
42+
43+
Otherwise, use one of the following command to create a bUnit test project with
44+
the framework of your choice:
45+
46+
# [xUnit](#tab/xunit)
47+
48+
```dotnetcli
49+
dotnet new bunit --framework xunit -o <NAME OF TEST PROJECT>
50+
```
51+
52+
# [NUnit](#tab/nunit)
53+
54+
```dotnetcli
55+
dotnet new bunit --framework nunit -o <NAME OF TEST PROJECT>
56+
```
57+
58+
# [MSTest](#tab/mstest)
59+
60+
```dotnetcli
61+
dotnet new bunit --framework mstest -o <NAME OF TEST PROJECT>
62+
```
63+
64+
***
65+
66+
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:
67+
68+
- `xunit` - [xUnit](https://xunit.net/),
69+
- `nunit` - [NUnit](https://nunit.org/),
70+
- `mstest` - [MSTest](https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-with-mstest)
71+
72+
**3. Add the test project to your solution**
73+
74+
If using Visual Studio, add the test project to your solution (`.sln`), and add a reference between the test project and the project containing the components that should be tested:
75+
76+
```dotnetcli
77+
dotnet sln <NAME OF PROJECT>.sln add <NAME OF TEST PROJECT>
78+
dotnet add <NAME OF TEST PROJECT>.csproj reference <NAME OF COMPONENT PROJECT>.csproj
79+
```
80+
81+
This will allow the test project to see and test the components in the component project.
1182

1283
## Creating a test project manually
1384

@@ -116,7 +187,7 @@ The result should be a test project with a `.csproj` that looks like this (non b
116187
<ItemGroup>
117188
<ProjectReference Include="<PATH TO COMPONENT LIB>.csproj" />
118189
</ItemGroup>
119-
190+
120191
</Project>
121192
```
122193

@@ -132,13 +203,13 @@ The result should be a test project with a `.csproj` that looks like this (non b
132203
</PropertyGroup>
133204

134205
<ItemGroup>
135-
<PackageReference Include="bunit" Version="#{RELEASE-VERSION}#" />
206+
<PackageReference Include="bunit" Version="#{RELEASE-VERSION}#" />
136207
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
137208
<PackageReference Include="NUnit" Version="3.13.2" />
138209
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
139210
<PackageReference Include="coverlet.collector" Version="3.1.0" />
140211
</ItemGroup>
141-
212+
142213
<ItemGroup>
143214
<ProjectReference Include="<PATH TO COMPONENT LIB>.csproj" />
144215
</ItemGroup>
@@ -158,83 +229,20 @@ The result should be a test project with a `.csproj` that looks like this (non b
158229
</PropertyGroup>
159230

160231
<ItemGroup>
161-
<PackageReference Include="bunit" Version="#{RELEASE-VERSION}#" />
232+
<PackageReference Include="bunit" Version="#{RELEASE-VERSION}#" />
162233
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
163234
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
164235
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
165236
<PackageReference Include="coverlet.collector" Version="3.1.0" />
166-
</ItemGroup>
167-
237+
</ItemGroup>
238+
168239
<ItemGroup>
169240
<ProjectReference Include="<PATH TO COMPONENT LIB>.csproj" />
170241
</ItemGroup>
171242

172243
</Project>
173244
```
174245

175-
***
176-
177-
## Creating a test project with bUnit template
178-
179-
To skip a few steps in the guide above, use the [bUnit test project template](https://www.nuget.org/packages/bunit.template/).
180-
181-
The steps for creating a test project with the bUnit template are as follows:
182-
183-
1. Install the template (only needed the first time)
184-
2. Create a new test project
185-
3. Add the test project to your solution
186-
187-
These steps look like this from the `dotnet` CLI:
188-
189-
**1. Install the template**
190-
191-
Install the template from NuGet using this command:
192-
193-
```dotnetcli
194-
dotnet new install bunit.template::#{NBGV_NuGetPackageVersion}#
195-
```
196-
197-
**2. Create a new test project**
198-
199-
Use the following command to create a bUnit with xUnit test project:
200-
201-
# [xUnit](#tab/xunit)
202-
203-
```dotnetcli
204-
dotnet new bunit --framework xunit -o <NAME OF TEST PROJECT>
205-
```
206-
207-
# [NUnit](#tab/nunit)
208-
209-
```dotnetcli
210-
dotnet new bunit --framework nunit -o <NAME OF TEST PROJECT>
211-
```
212-
213-
# [MSTest](#tab/mstest)
214-
215-
```dotnetcli
216-
dotnet new bunit --framework mstest -o <NAME OF TEST PROJECT>
217-
```
218-
219-
***
220-
221-
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:
222-
223-
- `xunit` - [xUnit](https://xunit.net/),
224-
- `nunit` - [NUnit](https://nunit.org/),
225-
- `mstest` - [MSTest](https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-with-mstest)
226-
227-
**3. Add the test project to your solution**
228-
229-
If using Visual Studio, add the test project to your solution (`.sln`), and add a reference between the test project and the project containing the components that should be tested:
230-
231-
```dotnetcli
232-
dotnet sln <NAME OF PROJECT>.sln add <NAME OF TEST PROJECT>
233-
dotnet add <NAME OF TEST PROJECT>.csproj reference <NAME OF COMPONENT PROJECT>.csproj
234-
```
235-
236-
This will allow the test project to see and test the components in the component project.
237-
238246
## Further reading
239247

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

0 commit comments

Comments
 (0)