bUnit: a Testing Library for Blazor Components
bUnit is a testing library for Blazor Components. Its goal is to make it easy to write comprehensive, stable unit tests. With bUnit, you can:
- Setup and define components under tests using C# or Razor syntax
- Verify outcomes using semantic HTML comparer
- Interact with and inspect components as well as trigger event handlers
- Pass parameters, cascading values and inject services into components under test
- Mock
IJSRuntime
and Blazor authentication and authorization - Perform snapshot testing
bUnit builds on top of existing unit testing frameworks such as xUnit, NUnit, and MSTest, which run the Blazor components tests in just the same way as any normal unit test. bUnit runs a test in milliseconds, compared to browser-based UI tests which usually take seconds to run.
Go to the Documentation pages to learn more.
Test Example
Let’s write a test for the <Counter>
component listed below. This comes with the standard Blazor project template which verifies that the counter corrects increments when the button is clicked:
<h1>Counter</h1>
<p>
Current count: @currentCount
</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
int currentCount = 0;
void IncrementCount()
{
currentCount++;
}
}
To do this, you can carry out the following using bUnit and xUnit:
[Fact]
public void CounterShouldIncrementWhenClicked()
{
// Arrange: render the Counter.razor component
using var ctx = new TestContext();
var cut = ctx.RenderComponent<Counter>();
// Act: find and click the <button> element to increment
// the counter in the <p> element
cut.Find("button").Click();
// Assert: first find the <p> element, then verify its content
cut.Find("p").MarkupMatches("<p>Current count: 1</p>");
}
This test uses bUnit’s test context to render the ‘Counter’ component with the ‘RenderComponent’ method. It then finds the button the component rendered and clicks it with the ‘Find’ and ‘Click’ methods. Finally, it finds the paragraph (<p>
) element and verifies that it matches the expected markup passed to the MarkupMatches method.
Go to the Documentation pages to learn more.
NuGet Downloads
bUnit is available on NuGet in various incarnations. If you are using xUnit as your general purpose testing framework, you can use bunit
, which includes everything in one package. If you want to use NUnit or MStest, then pick bunit.core
and bunit.web
:
Name | Description | NuGet Download Link |
---|---|---|
bUnit.web | Adds support for testing Blazor components for the web. This includes bUnit.core. | |
bUnit.xUnit | Adds additional support for using bUnit with xUnit, including support for Razor-based tests. | |
bUnit.core | Core library that enables rendering a Blazor component in a test context. | |
bUnit.template | Template, which currently creates xUnit-based bUnit test projects only |
Sponsors
A huge thank you to the sponsors of my work with bUnit. The higher tier sponsors are:
Progress-Telerik |
Hassan Rezk Habib (@hassanhabib) |
Milestones to v1.0.0
Going forward, we have a variety of milestones to reach. These are the current goals that should be reached before v1.0.0 is ready:
- Stabilize the APIs, such that they work equally well with both xUnit, NUnit, and MSTest as the underlying test framework. The general goal is to make it easy for developers to create their required tests successfully.
- 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.
- Improve the documentation. It is a good idea to get an experienced technical editor to review the documentation, making sure it is clear and understandable. In addition to this, more ‘How to’ guides are planned in the Update Docs milestone.
- Join the .NET Foundation.. This project is too large for one person to act as owner and sole maintainer, 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 support and guidance needed to ensure the project's long term future.
In the post-v1.0.0 to v1.0.x time frame, focus will be on improving performance. In particular, it would be nice to reduce the current spin-up time of about one second.
Contributors
Shout outs and a big thank you to all the contributors to the library, including those who raise issues, those who provide input to issues, and those who send pull requests.
Want to help out? You can help in a number of ways:
- Provide feedback and input through issues, via Twitter or by using the bUnit Gitter chat room.
- Help build the library. Just pick an issue and submit pull-requests.
- Help write documentation.
- Create blog posts, presentations or video tutorials. If you do, I'll be happy to showcase them in the related section on this site.