You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[](https://github.com/egil/bunit/releases)
**bUnit** is a testing library for Blazor Components. You can:
9
+
**bUnit** is a testing library for Blazor Components. Its goal is to make it easy to write _comprehensive, stable unit tests_. You can:
10
10
11
11
- Setup and define components under tests in C# or Razor syntax
12
-
- Verify outcome using semantic HTML diffing/comparison logic
12
+
- Verify outcome using semantic HTML comparer
13
13
- Interact with and inspect components
14
14
- Trigger event handlers
15
15
- Provide cascading values
16
16
- Inject services
17
17
- Mock `IJsRuntime`
18
18
- Perform snapshot testing
19
19
20
-
The library builds on top of existing unit testing frameworks such as xUnit, which runs the Blazor components tests, just as any normal unit test.
21
-
22
-
The library's goal is to make it easy to write _comprehensive, stable unit tests_ for Blazor Components/Razor Components.
20
+
bUnit builds on top of existing unit testing frameworks such as xUnit, NUnit, and MSTest, which runs the Blazor components tests, just as any normal unit test.
23
21
24
22
**Go to [bUnit.egilhansen.com](https://bunit.egilhansen.com) to learn more.**
25
23
@@ -41,8 +39,8 @@ To get started, head to the documentation on [bUnit.egilhansen.com/docs](https:/
41
39
42
40
These are the current goals that should be reached before v1.0.0 is ready:
43
41
44
-
-**Stabilize the APIs**, such that they work equally well with both xUnit, Nunit, and MSTest as the underlying test framework. The general goals is to make it easy and obvious for developers to create the tests they needed, and fall into the pit of success.
45
-
-**Get the Razor-based testing to stable**, e.g. make the discovery and running of tests defined in .razor files stable and efficient. This includes adding support for Nunit and MSTest as test runners.
42
+
-**Stabilize the APIs**, such that they work equally well with both xUnit, NUnit, and MSTest as the underlying test framework. The general goals is to make it easy and obvious for developers to create the tests they needed, and fall into the pit of success.
43
+
-**Get the Razor-based testing to stable**, e.g. make the discovery and running of tests defined in .razor files stable and efficient. This includes adding support for NUnit and MSTest as test runners.
46
44
-**Improve the documentation**. Currently there are a list of "How to" guides planned in the [Update Docs](https://github.com/egil/bunit/issues?q=is%3Aopen+is%3Aissue+milestone%3A%22updated+docs%22) milestone.
47
45
-**Join the .NET Foundation.**. This project is too large for one person to be the owner and be the sole maintainer of, so the plan is to apply for membership as soon as possible, most likely close to or after v1.0.0 ships, and get the needed support and guidance to ensure the project long term.
Copy file name to clipboardExpand all lines: docs/docs/getting-started/create-test-project.md
+16-14Lines changed: 16 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,24 +5,24 @@ title: Creating a new bUnit Test Project
5
5
6
6
# Creating a new bUnit Test Project
7
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.
8
+
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, to write and run tests.
9
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.
10
+
To use bUnit with xUnit, the easiest approached is using the bUnit project template described in the [Create a test project with bUnit template](#create-a-test-project-with-bunit-template) section further down the page. To create a test project manually in a general-purpose testing frameworks agnostic way, read the following section.
11
11
12
12
## Create a Test Project Manually
13
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:
14
+
To create a project for testing you Blazor components that uses either of three general purpose test frameworks, go through these steps:
15
15
16
16
1. Create a new xUnit/NUnit/MSTest testing project
17
17
2. Add bUnit to the test project
18
18
3. Configure project settings
19
-
4. Add the test project to your solution
19
+
4. Add the test project to your existing solution
20
20
21
21
These steps look like this from the `dotnet` CLI:
22
22
23
23
**1. Create a new test project**
24
24
25
-
Use the following command (_click on the tab that for the test framework you would like to use_):
25
+
Use the following command (_click on the tab that for the test framework of choice_):
26
26
27
27
# [xUnit](#tab/xunit)
28
28
@@ -48,7 +48,7 @@ where `-o <NAME OF PROJECT>` is used to name the test project.
48
48
49
49
**2. Add bUnit to the test project**
50
50
51
-
To add bUnit to your test project, first change to the newly created test projects folder, and then use the following command:
51
+
To add bUnit to the test project, change to the newly created test projects folder and then use the following command:
Then you need to change a few project settings, in particular we need to change the project's SDK to `Microsoft.NET.Sdk.Razor`, remember to set `RazorLangVersion` to `3.0`, and set the `<TargetFramework>` to `netcoreapp3.1`, since bUnit builds on `.netstandard 2.1`.
79
+
The test projects setting needs to be set accordingly:
80
+
81
+
- the project's SDK to `Microsoft.NET.Sdk.Razor`
82
+
- set `RazorLangVersion` to `3.0`
83
+
- set the `<TargetFramework>` to `netcoreapp3.1` (bUnit builds on `.netstandard 2.1`)
80
84
81
85
To do so, change the first part of the test projects `.csproj` file to look like this.:
82
86
@@ -95,7 +99,7 @@ To do so, change the first part of the test projects `.csproj` file to look like
95
99
96
100
**4. Add the test project to your solution**
97
101
98
-
Then you need to add your test project to your solution (`.sln`) and add a reference between your test project and component project:
102
+
If using Visual Studio, add the test project to your solution (`.sln`), and add a reference between the test project and project containing the components that should be tested:
99
103
100
104
```dotnetcli
101
105
dotnet sln <NAME OF PROJECT>.sln add <NAME OF TEST PROJECT>
@@ -104,7 +108,6 @@ dotnet add <NAME OF COMPONENT PROJECT>.csproj reference <NAME OF TEST PROJECT>.c
104
108
105
109
The result should be a test project with a `.csproj` that looks like this (other packages than bUnit might have different version numbers):
106
110
107
-
108
111
# [xUnit](#tab/xunit)
109
112
110
113
```xml
@@ -185,15 +188,14 @@ The result should be a test project with a `.csproj` that looks like this (other
185
188
186
189
## Create a Test Project with bUnit Template
187
190
188
-
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.
191
+
To skip a few steps in the guide above, 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.
189
192
190
193
The steps are as follows:
191
194
192
195
1. Install the template (only needed the first time)
193
196
2. Create a new test project
194
197
3. Add the test project to your solution
195
198
196
-
197
199
These steps look like this from the `dotnet` CLI:
198
200
199
201
**1. Install the template**
@@ -216,7 +218,7 @@ where `-o <NAME OF PROJECT>` is used to name the test project.
216
218
217
219
**3. Add the test project to your solution**
218
220
219
-
Then you need to add your test project to your solution (`.sln`) and add a reference between your test project and component project:
221
+
If using Visual Studio, add the test project to your solution (`.sln`), and add a reference between the test project and project containing the components that should be tested:
220
222
221
223
```dotnetcli
222
224
dotnet sln <NAME OF PROJECT>.sln add <NAME OF TEST PROJECT>
@@ -225,6 +227,6 @@ dotnet add <NAME OF COMPONENT PROJECT>.csproj reference <NAME OF TEST PROJECT>.c
225
227
226
228
## Further Reading
227
229
228
-
Now you are ready to write some tests. To learn how, continue reading the <xref:writing-csharp-tests> and <xref:writing-razor-tests> pages.
230
+
To start creating tests, continue reading the <xref:writing-csharp-tests> and <xref:writing-razor-tests> pages.
229
231
230
-
For addition tips and tricks that will make writing tests easier, see the <xref:misc-test-tips> page.
232
+
For addition tips and tricks that will make writing tests easier, see the <xref:misc-test-tips> page.
1. The fixture has both a setup and test method specified. The setup methods is used to register an empty list of tasks, that the `<SimpleTodo>` component requires.
105
105
2. The `<SimpleTodo>` component is wrapped in a `<CascadingValue>` component that passes down the "Theme" cascading value.
Copy file name to clipboardExpand all lines: docs/docs/getting-started/index.md
+8-15Lines changed: 8 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,27 +5,20 @@ title: Getting Started with bUnit
5
5
6
6
# Getting Started with bUnit
7
7
8
-
To get started writing tests for your Blazor components, you first need to set up a test project. Then you can start writing your tests, using either C# or Razor syntax.
8
+
To start writing tests for Blazor components, first set up a test project, then you can start writing tests, either using C# or Razor syntax.
9
9
10
-
These three pages will teach you *the basics*:
10
+
The *the basics* topics are:
11
11
12
12
1.**<xref:create-test-project>** covers setting up a bUnit test project.
13
-
2.**<xref:writing-csharp-tests>** covers the basics of writing tests in C#.
14
-
With C# based tests, you write all your testing logic in C# files, i.e. like regular unit tests.
15
-
3.**<xref:writing-razor-tests>** covers the basics of writing tests in Razor and C# syntax.
16
-
With Razor based tests, you write tests in `.razor` files, which allows you to declare, in Razor syntax, the component under test and other markup fragments you need. You still write your assertions via C# in the .razor file, inside `@code {...}` blocks.
13
+
2.**<xref:writing-csharp-tests>** covers the basics of writing tests in C#, i.e. like regular unit tests.
14
+
3.**<xref:writing-razor-tests>** covers the basics of writing tests in `.razor` files in Razor and C# syntax.
17
15
18
-
After reading those, these topics will take you to the *next level*:
16
+
The *next level* topics:
19
17
20
-
1.**[Providing different types of input](xref:providing-input)** to a component under test, e.g. passing parameters or injecting services.
18
+
1.**[Providing different types of input](xref:providing-input)** to a component under test in C# based tests, e.g. passing parameters or injecting services.
21
19
2.**[Verifying output in various ways](xref:verification)** from a component under test, e.g. inspecting the rendered markup.
22
20
3.**[Mocking dependencies](xref:mocking)** a component under test has, e.g. the `IJsRuntime` or `HttpClient`.
23
21
24
-
## Examples
25
-
26
-
To see examples of how to work assert and manipulate a rendered component, go to the following pages:
27
-
28
-
-[C# test examples in the sample project](https://github.com/egil/bunit/tree/master/sample/tests/Tests)
29
-
-[Razor based test examples in the sample project](https://github.com/egil/bunit/tree/master/sample/tests/RazorTestComponents)
30
-
-[Snapshot test examples in the sample project](https://github.com/egil/bunit/tree/master/sample/tests/SnapshotTests)
22
+
## Getting Help
31
23
24
+
Cannot figure out how to write a test for a testing scenario? Found a testing scenario that is hard or inelegant with bUnit? Found a bug in bUnit? Head over to the [GitHub issues list](https://github.com/egil/bunit/issues) and ask a question, suggest a new feature, or join [bUnits Gitter channel](https://gitter.im/egil/bunit) and let us know. There are no stupid questions, and all questions are welcome!
Copy file name to clipboardExpand all lines: docs/docs/getting-started/writing-csharp-tests.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,13 +7,13 @@ title: Writing Tests in C# for Blazor Components
7
7
8
8
Testing Blazor components is a different from testing regular C# classes: Blazor components are *rendered*, they have the *Blazor component life cycle*, during which we can *provide input* to them and where they *produce output*.
9
9
10
-
**bUnit** enables you to render the component you want to test, pass in parameters to it, inject services into it, and access the rendered component instance and the markup it has produced.
10
+
Use **bUnit** to render the component you want to test, pass in parameters to it, inject services into it, and access the rendered component instance and the markup it has produced.
11
11
12
12
Rendering a component happens through bUnit's <xref:Bunit.TestContext>, and the result of the rendering, a <xref:Bunit.IRenderedComponent`1>, provides access to the component instance, and the markup produced by the component.
13
13
14
14
## Creating a Basic Test
15
15
16
-
Let us see a simple example, where we test the following `<HelloWorld>` component:
16
+
This is a simple example, that tests the following `<HelloWorld>` component:
@@ -38,7 +38,7 @@ Let us see a simple example, where we test the following `<HelloWorld>` componen
38
38
39
39
***
40
40
41
-
In this test, we do the following:
41
+
The following happens in the test above:
42
42
43
43
1. New up the disposable <xref:Bunit.TestContext>, and assign it using the `using var` syntax, to avoid unnecessary indention.
44
44
2. Render the `<HelloWorld>` component using <xref:Bunit.TestContext>, which we do through the <xref:Bunit.TestContext.RenderComponent``1(Bunit.Rendering.ComponentParameter[])> method. We will cover passing parameters to components elsewhere.
Copy file name to clipboardExpand all lines: docs/docs/providing-input/index.md
+5-1Lines changed: 5 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,4 +5,8 @@ title: Providing Input to a Component Under Test
5
5
6
6
# Providing Input to a Component Under Test
7
7
8
-
providing a toc with descriptions of each topic
8
+
This section covers the various ways to provide input to a component under test, its split into three sub sections:
9
+
10
+
-**<xref:passing-parameters-to-components>:** This covers passing regular parameters, child content, cascading values, event callbacks, etc. This topic is mostly relevant when writing tests in C# only.
11
+
-**<xref:inject-services-into-components>:** This covers injecting services into components under test. This topic is relevant for both Razor-based tests and C# only tests.
12
+
-**<xref:configure-3rd-party-libs>:** This covers setting up 3rd party libraries in a bUnit testing scenario, such that components under test that use them can be tested easily.
Copy file name to clipboardExpand all lines: docs/index.md
+5-7Lines changed: 5 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,20 +11,18 @@ title: bUnit - a testing library for Blazor components
11
11
12
12
# bUnit - a testing library for Blazor components
13
13
14
-
**bUnit** is a testing library for Blazor Components. You can:
14
+
**bUnit** is a testing library for Blazor Components. Its goal is to make it easy to write _comprehensive, stable unit tests_. You can:
15
15
16
16
- Setup and define components under tests in C# or Razor syntax
17
-
- Verify outcome using semantic HTML diffing/comparison logic
17
+
- Verify outcome using semantic HTML comparer
18
18
- Interact with and inspect components
19
19
- Trigger event handlers
20
20
- Provide cascading values
21
21
- Inject services
22
22
- Mock `IJsRuntime`
23
23
- Perform snapshot testing
24
24
25
-
The library builds on top of existing unit testing frameworks such as xUnit, which runs the Blazor components tests, just as any normal unit test.
26
-
27
-
The library's goal is to make it easy to write _comprehensive, stable unit tests_ for Blazor Components/Razor Components.
25
+
bUnit builds on top of existing unit testing frameworks such as xUnit, NUnit, and MSTest, which runs the Blazor components tests, just as any normal unit test.
28
26
29
27
**Go to the [Documentation](xref:getting-started) pages to learn more.**
30
28
@@ -58,8 +56,8 @@ bUnit is available on NuGet in various incarnations. If you are using xUnit as y
58
56
59
57
These are the current goals that should be reached before v1.0.0 is ready:
60
58
61
-
-**Stabilize the APIs**, such that they work equally well with both xUnit, Nunit, and MSTest as the underlying test framework. The general goals is to make it easy and obvious for developers to create the tests they needed, and fall into the pit of success.
62
-
-**Get the Razor-based testing to stable**, e.g. make the discovery and running of tests defined in .razor files stable and efficient. This includes adding support for Nunit and MSTest as test runners.
59
+
-**Stabilize the APIs**, such that they work equally well with both xUnit, NUnit, and MSTest as the underlying test framework. The general goals is to make it easy and obvious for developers to create the tests they needed, and fall into the pit of success.
60
+
-**Get the Razor-based testing to stable**, e.g. make the discovery and running of tests defined in .razor files stable and efficient. This includes adding support for NUnit and MSTest as test runners.
63
61
-**Improve the documentation**. Currently there are a list of "How to" guides planned in the [Update Docs](https://github.com/egil/bunit/issues?q=is%3Aopen+is%3Aissue+milestone%3A%22updated+docs%22) milestone.
64
62
-**Join the .NET Foundation.**. This project is too large for one person to be the owner and be the sole maintainer of, so the plan is to apply for membership as soon as possible, most likely close to or after v1.0.0 ships, and get the needed support and guidance to ensure the project long term.
0 commit comments