DEV Community

Cover image for Unit Testing with FluentAssertions: Making .NET Tests Readable for Humans
Kenu Olotu
Kenu Olotu

Posted on

Unit Testing with FluentAssertions: Making .NET Tests Readable for Humans

When I first started my internship, unit testing felt like a chore. In my head, I just wanted to build features and see them live on the new brand site I was developing. I’d write basic Assert.AreEqual statements just to tick a box, but they felt robotic and, honestly, a bit clunky to work with.

Then I discovered FluentAssertions.

The "Ah-ha" moment for me was realizing that my tests didn't have to look like math equations. They could look like English sentences. Instead of struggling through a wall of messy code to figure out why a test failed, I could read a line like response.StatusCode.Should().Be(HttpStatusCode.OK) and immediately know the intent.

Why Organization is More Than Just "Looking Nice"

As a Computer Science student, you hear a lot about "Clean Code," but you don't really feel its importance until you're six months into an internship with a deadline looming. I realized that keeping my tests organized wasn't just about being "easier on the eye", it was about saving time.

When your test suite is organized, you don't fear it. You actually start to rely on it. During my work on the subsidiary brand's site, I caught several bugs in my C# logic that I would have completely missed if I were just manually testing the UI. There’s a specific kind of relief when you see a red light in your test runner and realize, "Wait, I didn't handle the empty string case," before that bug ever hits a real user's phone.

Writing Tests That Read Like a Story

​I believe that code should be a story that explains what the software is supposed to do. Using FluentAssertions allowed me to write tests that even a non-technical manager could almost understand.

For example, when I was testing the login logic, my assertions looked like this:

response.Data.Should().NotBeNull();
​
response.Data.Token.Should().NotBeNullOrEmpty();
Enter fullscreen mode Exit fullscreen mode

This isn't just "organized" code; it’s self-documenting code. If I leave the company and another intern takes over, they don't have to guess what I was trying to check. The test tells them exactly what the expectation was.

The Real Value of Quality

​If you’re a developer working with .NET, don't settle for the bare minimum in your tests. Taking that extra step to use tools like FluentAssertions makes your workflow smoother and your codebase much more professional. It’s the difference between just "coding" and actually practicing software engineering.

Top comments (1)

Collapse
 
seankilleen profile image
Sean Killeen • Edited

Worth noting that any use of FluentAssertions in a commercial project requires a paid license. As of today that cost is ~$15/dev/month minimum. xceed.com/products/unit-testing/fl...