Table of Contents

GitHub tag (latest SemVer pre-release) Nuget Issues Open

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, Blazor authentication and authorization, and others

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:


    Current count: @currentCount

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
  int currentCount = 0;

  void IncrementCount()

To do this, you can carry out the following using bUnit and xUnit:

@inherits TestContext
@code {
  public void CounterShouldIncrementWhenClicked()
    // Arrange: render the Counter.razor component
    var cut = Render(@<Counter />);

    // Act: find and click the <button> element to increment
    // the counter in the <p> element

    // 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 Render 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. Most users should just pick the bUnit package:

Name Description NuGet Download Link
bUnit Includes the bUnit.core and bUnit.web packages. Nuget
bUnit.core Core library that enables rendering a Blazor component in a test context. Nuget
bUnit.web Adds support for testing Blazor components for the web. This includes bUnit.core. Nuget
bUnit.template Template for bUnit test projects based on xUnit, NUnit or MSTest Nuget
bUnit.generators Source code generators to minimize code setup in various situations. Nuget
bUnit.web.query bUnit implementation of's query APIs. Nuget


A huge thank you to the sponsors of my work with bUnit. The higher tier sponsors are:


Shoutouts 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.

Code of conduct

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community. For more information see the .NET Foundation Code of Conduct.

.NET Foundation

This project is supported by the .NET Foundation.

Progress Telerik

Premium sponsor: Progress Telerik.


Editorial support provided by Packt.

.NET Foundation

Supported by the .NET Foundation.