bUnit bUnit

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

    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 outcome using semantic HTML comparer
    • Interact with and inspect components as well as triggering event handlers
    • Pass parameters, cascading values and inject services into components under test
    • Mock IJSRuntime and Blazors authentication and authorization
    • Perform snapshot testing

    bUnit builds on top of existing unit testing frameworks such as xUnit, NUnit, and MSTest, which runs 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, where a test usually takes 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 do 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. Nuget
    bUnit.xUnit Adds additional support for using bUnit with xUnit, including support for Razor-based tests. Nuget
    bUnit.core Core library that enables rendering a Blazor component in a test context. Nuget
    bUnit.template Template, which currently creates an xUnit based bUnit test projects only Nuget

    Sponsors

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

    @Progress-Telerik
    Progress-Telerik
    Hassan Rezk Habib (@hassanhabib)
    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 be the owner and 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.

    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 spin-up time of about one second.

    Contributors

    Shout outs and a big thank you to all the contributors to the library, both those that raise issues, 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, Twitter or 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 will be happy to showcase them in the related section on this site.

    Editorial support provided by Packt.

    • Improve this Doc
    Back to top Documentation updated on 1/20/2021 6:00:54 PM +00:00 in commit b61b4ab737.