Chapter 9. Testing All the Things
In this chapter, we’re going to explore the various testing options available to you as a Blazor developer. It’s important to know what you can test and how to test it. We’ll start with the most basic testing use cases that apply to all .NET and JavaScript developers alike. I’ll provide an introduction to testing and show you how to use the xUnit, bUnit, and Playwright testing frameworks. We will then move on to more advanced testing scenarios. We’ll finish with code examples that exemplify how to automate testing with GitHub Action workflows and how to write tests, such as unit, component, and end-to-end tests.
Why Test?
You may be asking, “What’s the point of testing if your code works anyway?” That’s a fair question. For years, I felt the same way—I disliked testing because it seemed unnecessary. After years of writing code, however, I’ve changed my mind. Testing is a great way to ensure that your code works as expected and can be refactored as needed. Testing also helps make things work right if core business rules change. Just as I once said that good code is a love letter for the next developer, testing is a show of affection as well. Let’s get started with the smallest kind of test—the unit test.
Unit Testing
A unit test is the most basic testing strategy that exercises a small, isolated piece or unit of code. A unit test should accept only known inputs and return expected outputs—it’s best to avoid randomization in testing. By automating ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access