|
| 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) |
0 commit comments