👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Using Regex to migrate from Fluent Assertions to XUnit Assertions

Using Regex to migrate from Fluent Assertions to XUnit Assertions

Regex

1 Articles

Improve

Table of Contents

  1. What we gonna do?
  2. Why we gonna do?
  3. How we gonna do?
  4. Summary

What we gonna do?

January 2025 started with a big surprise from FluentAsserions, a popular free assertion library for .NET. The library maintainers decided to make the library paid one and started charging for it from v8.0. This decision made me realise that it have introduced a technical debt in all the projects in have worked so far over a decade by using this library.

I took This opportunity to pay off the debt I introduced and decided to use AI / LLM to give a prompt and convert to XUnit Assertions. I tried with ollama and it worked when I tried with prompt for single assertion statement in terminal but became very lazy and slow when I was running against multiple test files inside the repo.

So I thought I'm using wrong tool to solve the problem. It's then decided to use simple Regex to make this migration. when I tried and viola the entire repository with 1000+ test methods and all FluentAsserions statements got converted to XUnit Assertions within seconds. I was able to achieve 90% plus conversion in repository and left with small manual cleanup which took and hour to fix.

Why we gonna do?

Our client technical directors decided to move away from FluentAssertions and use XUnit assertions as we are a large enterprise with 1000+ developers and not ready to spend 130$ for each. Even though FluentAsserions team is ready to provide security patches for v7, we decided not to go forward and blocked FluentAsserions from our private Nuget Feed.

The trade off to pay and use FluentAsserions vs cost to migrate all repository to XUnit assertion kicked off this idea and now we are successful in that. This was possible because we have followed strict coding standards and kept our assertion statements simple to one line.

How we gonna do?

Here are the steps that I have followed.

  1. Find all test files and group them by folder.

    Code Sample - Find all test Files

  2. Parallely Process each file.

    Code Sample - Parallely run conversion task

  3. Read lines in each file. If the lines contains .Should(). then Process it and convert to XUnit Assertions and replace the line. Finally save the test file.

    Code Sample - Processing Test File

  4. The secret sauce Regex that I used for commonly used FluentAsserions statements.

    Code Sample - Regex to convert common and simple FluentAsserions statements

Here is the sample input and output.

Code Sample - FluentAsserions input statements

Code Sample - Migrated XUnit output statements

Summary

In this article we learnt about how to use regular expression to migrate from FluentAsserions to XUnit Assertions. Regex are very powerful and I realised when I was able make this conversion happen within seconds. We save huge money and time and achieved 90% results using this approach. The idea is open now and you can extend and use it in your repos. I have never imagined that I'll use Regex for any task other than Email Validation.

👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
  • Regex
  • FluentAssertion
  • XUnit
  • Assertion