|
1 | 1 | # Creating a new bUnit with NUnit test project |
2 | 2 |
|
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) |
0 commit comments