Because life's too short for hard-to-read assertions! 🔍
Shouldly.FromAssert is a Roslyn analyzer that helps you migrate from traditional NUnit assertions to the more human-readable Shouldly library. It's like having a friendly code review buddy who's really into making tests more readable (but doesn't drink your coffee).
- Automatically detects NUnit assertions and suggests Shouldly alternatives
- Provides code fixes to transform assertions with a single click
- Supports a wide range of assertion types:
- Basic assertions (
Assert.AreEqual
→ShouldBe
) - String assertions (
StringAssert.Contains
→ShouldContain
) - Collection assertions (
CollectionAssert.Contains
→ShouldContain
) - Type assertions (
Assert.IsInstanceOf
→ShouldBeOfType
) - And many more!
- Basic assertions (
dotnet add package Shouldly.FromAssert
- Install the package
- Write your tests as usual with NUnit
- Look for the analyzer suggestions (they'll appear as warnings)
- Click the lightbulb 💡 or press (Alt+Enter or Ctrl+. depending on the IDE religeon you practice)
- Select "Convert to Shouldly"
- Watch your assertions transform into beautiful, readable Shouldly statements
// Before: 😕
Assert.That(contestant, Is.EqualTo(1337));
CollectionAssert.Contains(contestants, winner);
StringAssert.StartsWith("Hello", greeting);
// After: 😊
contestant.ShouldBe(1337);
contestants.ShouldContain(winner);
greeting.ShouldStartWith("Hello");
Here are some examples of the transformations this analyzer can perform:
NUnit | Shouldly |
---|---|
Assert.That(x, Is.EqualTo(y)) |
x.ShouldBe(y) |
Assert.IsTrue(x > 10) |
(x > 10).ShouldBeTrue() |
Assert.IsNull(x) |
x.ShouldBeNull() |
CollectionAssert.Contains(list, item) |
list.ShouldContain(item) |
StringAssert.StartsWith("Hi", str) |
str.ShouldStartWith("Hi") |
Assert.Throws<ArgumentException>(() => method()) |
Should.Throw<ArgumentException>(() => method()) |
And many more! Check out the tests for a complete list of supported conversions.
Found a bug? Have a suggestion? Want to add support for more assertions? We'd love your help! Please feel free to:
- Open an issue
- Submit a pull request
- Start a discussion
- Share your success stories
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
- The awesome Shouldly team for making assertions more human
- The Roslyn team for making this possible
- Coffee ☕, for making developers possible
Made with 💚 by Agoda