Skip to content

Commit 3360637

Browse files
committed
NUnit and xUnit getting started docs
1 parent ddadbaf commit 3360637

5 files changed

Lines changed: 112 additions & 37 deletions

File tree

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,65 @@
11
# Creating a new bUnit with NUnit test project
22

3-
TODO
3+
To create a project for testing you Blazor components that uses NUnit as the general purpose testing framework, you need to go through these steps:
4+
5+
1. Create a new NUnit testing project
6+
2. Add bUnit to it
7+
3. Set the right project type
8+
4. Add the test project to your solution and add a reference to your component library
9+
10+
These steps look like this from the dotnet CLI:
11+
12+
1\. Create a new test project, use the following command:
13+
14+
```bash
15+
dotnet new nunit -o <NAME OF TEST PROJECT>
16+
```
17+
18+
where `-o <NAME OF PROJECT>` is used to name the test project.
19+
20+
2\. Then you add bUnit to the test project you just created. We just add bUnit's web project, as that references `bunit.core`:
21+
22+
```bash
23+
cd <NAME OF PROJECT>
24+
dotnet add package bunit.web --version 1.0.0-beta-7#{VERSION}#
25+
```
26+
27+
3\. Finally you need to update the project type in the test projects `.csproj` file from `<Project Sdk="Microsoft.NET.Sdk">` to `<Project Sdk="Microsoft.NET.Sdk.Razor">`.
28+
29+
4\. Add the test project to your solution and add a reference between your test project and component project:
30+
31+
```bash
32+
dotnet sln <NAME OF PROJECT>.sln add <NAME OF TEST PROJECT>
33+
dotnet add <NAME OF COMPONENT PROJECT>.csproj reference <NAME OF TEST PROJECT>.csproj
34+
```
35+
36+
The end result should be a test project with a `.csproj` that looks like this (other packages than bUnit might have different version numbers):
37+
38+
```xml
39+
<Project Sdk="Microsoft.NET.Sdk.Razor">
40+
41+
<PropertyGroup>
42+
<TargetFramework>netcoreapp3.1</TargetFramework>
43+
<IsPackable>false</IsPackable>
44+
</PropertyGroup>
45+
46+
<ItemGroup>
47+
<PackageReference Include="bunit.web" Version="#{VERSION}#" />
48+
<PackageReference Include="nunit" Version="3.12.0" />
49+
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1">
50+
<PrivateAssets>all</PrivateAssets>
51+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
52+
</PackageReference>
53+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
54+
</ItemGroup>
55+
56+
<ItemGroup>
57+
<ProjectReference Include="<PATH TO COMPONENT LIB>.csproj" />
58+
</ItemGroup>
59+
60+
</Project>
61+
```
62+
63+
## Further reading
64+
65+
- [Miscellaneous bUnit testing tips](/docs/misc-test-tips.html)

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

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
To create a project for testing you Blazor components, first install the [bUnit Project Template](https://www.nuget.org/packages/bunit.template/) from NuGet, using this command:
44

55
```bash
6-
dotnet new --install Razor.Components.Testing.Library.Template::#{VERSION}#
6+
dotnet new --install bunit.template::#{VERSION}#
77
```
88

9-
Then to create a new project, use the following command:
9+
Then to create a new test project, use the following command:
1010

1111
```bash
12-
dotnet new razortest -o <NAME OF TEST PROJECT>
12+
dotnet new bunit -o <NAME OF TEST PROJECT>
1313
```
1414

1515
where `-o <NAME OF PROJECT>` is used to name the test project.
@@ -53,36 +53,6 @@ If you do not want to use the Blazor test project template, you can create an em
5353
</Project>
5454
```
5555

56-
## Projects structure and tips and tricks
56+
## Further reading
5757

58-
The recommended solution/project structure for a test and production code project set-up is:
59-
60-
```
61-
src
62-
| MyComponentLib.csproj (namespace e.g. "Company.MyComponentLib")
63-
| _Imports.razor
64-
| Component1.razor
65-
| SubFolder
66-
| SubComponent1.razor
67-
68-
test
69-
| MyComponentLibTests.csproj (with project reference to MyComponentLib.csproj)
70-
| _Imports.razor
71-
| Component1Test.cs
72-
| SubFolder
73-
| SubComponent1Test.cs
74-
```
75-
76-
### Use same root namespace and folder structure in both test- and production project
77-
78-
A neat trick, which will limit the `import` statements needed in your test project, is to set the root namespace to the same as that of the production code project, _AND_ use the same folder structure as shown above. Following the example above, the `MyComponentLibTests.csproj` file should contain:
79-
80-
```xml
81-
<PropertyGroup>
82-
<RootNamespace>Company.MyComponentLib</RootNamespace>
83-
</PropertyGroup>
84-
```
85-
86-
### Make copy/paste of HTML easier
87-
88-
When writing C# based tests, you sometime want to copy/paste some HTML into C# strings from e.g. a Razor file. This is tedious to do manually as you have to escape the quotes and other special characters. The extension, [SmartPaster2019](https://marketplace.visualstudio.com/items?itemName=martinw.SmartPaster2013), allows us to copy strings where any character that needs to be escaped will be automatically.
58+
- [Miscellaneous bUnit testing tips](/docs/misc-test-tips.html)

docs/docs/misc-test-tips.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
title: Miscellaneous bUnit testing tips
3+
---
4+
5+
# Miscellaneous bUnit testing tips
6+
7+
Here is a few testing tips and tricks that have proven useful to us.
8+
9+
## Projects structure and tips and tricks
10+
11+
The recommended solution/project structure for a test and production code project set-up is:
12+
13+
```
14+
src
15+
| MyComponentLib.csproj (namespace e.g. "Company.MyComponentLib")
16+
| _Imports.razor
17+
| Component1.razor
18+
| SubFolder
19+
| SubComponent1.razor
20+
21+
test
22+
| MyComponentLibTests.csproj (with project reference to MyComponentLib.csproj)
23+
| _Imports.razor
24+
| Component1Test.cs
25+
| SubFolder
26+
| SubComponent1Test.cs
27+
```
28+
29+
## Use same root namespace and folder structure in both test- and production project
30+
31+
A neat trick, which will limit the `import` statements needed in your test project, is to set the root namespace to the same as that of the production code project, _AND_ use the same folder structure as shown above. Following the example above, the `MyComponentLibTests.csproj` file should contain:
32+
33+
```xml
34+
<PropertyGroup>
35+
<RootNamespace>Company.MyComponentLib</RootNamespace>
36+
</PropertyGroup>
37+
```
38+
39+
## Make copy/paste of HTML easier
40+
41+
When writing C# based tests, you sometime want to copy/paste some HTML into C# strings from e.g. a Razor file. This is tedious to do manually as you have to escape the quotes and other special characters. The extension, [SmartPaster2019](https://marketplace.visualstudio.com/items?itemName=martinw.SmartPaster2013), allows us to copy strings where any character that needs to be escaped will be automatically.

docs/docs/toc.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
## [Creating a new bUnit and xUnit test project](creating-a-new-bunit-xunit-project.md)
33
## [Creating a new bUnit and NUnit test project](creating-a-new-bunit-nunit-project.md)
44
## [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)
56
## [Basics of Blazor component testing](basics-of-blazor-component-testing.md)
67
# [C# based testing](csharp-based-testing.md)
78
# [Razor based testing](razor-based-testing.md)

global.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"sdk": {
33
"version": "3.1.201",
4-
"rollForward": "latestPatch"
4+
"rollForward": "latestMinor",
5+
"allowPrerelease": false
56
}
67
}

0 commit comments

Comments
 (0)