Testing

 




Introduction

Conducting tests will enhance the product's quality since it ensures that previously functioning features continue to operate correctly. However, writing tests for UI components can often be challenging; the most prevalent approach is to launch the site and utilize tools that simulate button clicks, then analyse the results to confirm whether everything works as expected. The benefit of this approach is that it allows us to test our site across various browsers and devices. The drawback is that this testing process can be quite time-consuming. We have to launch the web application, open a web browser, confirm the outcomes of the test, close the web browser, and then repeat this for each subsequent test.

We can apply this approach in Blazor in the same way as with any ASP.NET application, but Blazor offers additional options for testing. Steve Sanderson initiated a testing framework for Blazor, which was further developed by Microsoft MVP Egil Hansen. The framework created by Egil is known as bUnit and has established itself as a standard in the Blazor community for testing components.

What is bUnit?

bUnit is specifically designed for Blazor. It allows for the definition and setup of tests using either C# or Razor syntax. It is capable of mocking JavaScript interop as well as Blazor’s authentication and authorization features. To enhance the testability of our components, we sometimes need to consider these aspects from the outset or implement slight modifications to our code. bUnit does not depend on a web browser; instead, it renders the output internally and provides it to us so that we can test against expected outputs.

Setting Up a Test Project

To be able to do tests, we need a test project:

  • To install the bUnit templates, open PowerShell and run the following command-

dotnet new --install bunit . template

  • In Visual Studio, right-click MyBlogSolution and choose Add | New Project.

  • Search for bUnit and select bUnit Test Project in the results, and then click Next. Sometimes it takes time to find a template. We can also change the project type dropdown to bUnit to find the template. We might need to reboot Visual Studio to find it.

  • Name the project MyBlog.Shared.Tests, leave the location as is, and click Next.

  • Select .NET 5 in the dropdown.

We now have a test project.

Conclusion

We have successfully learnt about bUnit and created a test project.






























Comments

Popular posts from this blog

Information Protection Scanner: Resolve Issues with Information Protection Scanner Deployment

How AMI Store & Restore Works?

Create A Store Image Task