Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FluentAssertions nuget package to test projects #8946

Merged
merged 1 commit into from
Jul 10, 2023

Conversation

filzrev
Copy link
Contributor

@filzrev filzrev commented Jul 7, 2023

This PR add FluentAssertions NuGet package to test projects.

Background
Currently xUnit's assertions are used for unit tests.
I want to write unit test assertions with FluentAssertions assertion helpers. when adding new tests.

FluentAssertions is published as Apache License 2.0 and widely used in org:dotnet repositories
So I thought there is no major obstacles to adapt this assertion library.

If these changes aren't accepted. feel free to close this PR.

@yufeih
Copy link
Contributor

yufeih commented Jul 8, 2023

I don't have a lot of personal experience with FluentAssertions. Are there any benefits you see of adopting it except for syntax differences, such as things xunit assertions cannot do easily, or better diagnostic messages?

I'm leaning towards keeping one way of doing things unless there is enough merit where we can say the new approach is the new default.

@filzrev
Copy link
Contributor Author

filzrev commented Jul 8, 2023

Thanks for your reply.

such as things xunit assertions cannot do easily, or better diagnostic messages?

More readable diagnostic message.

Basically FluentAssertions output more readable diagnostic message.
Because It expose variety of APIs to write assertion.

For example.

1. FluentAssertions assertion

var results = new[] { "Dummy" };
results.Should().HaveCount(0);

Message:

Expected results to contain 0 item(s), but found 1: {"Dummy"}.

2. xUnit.NET assertion

var results = new[] { "Dummy" };
Assert.True(results.Length== 0);

Message:

Assert.True() Failure
Expected: True
Actual: False

It can be confirmed FluentAssertions output contains more assertion context information.

And FluentAssertions have Subject Identification feature.
It output what's tested* information when assertion failed.
This feature is useful when writing assertion for object tree.

For Example

Original code

Assert.Equal("SubBar", tocViewModel[2].Items[0].Name);

FluentAssertionsassertion

> tocViewModel[2].Items[0].Uid.Should().Be("SubBar");

Message:

Expected tocViewModel[2].Items[0].Name to be "SubBarDummy" with a length of 9, but "SubBar" has a length of 6, differs near "r" (index 5).


FluentAssertions features that can't do easily with xunit assertions.

Below is an excerpt features that I want to use when writing unit tests.

AssertionScope

This feature is useful when writing following tests.

  1. Writing multiple assertions in single unit test.
  2. Writing test to multiple items with foreach enumeration.

Example of 1.

Example of 2.

using var scope = new AssertionScope();
foeach(var path in Directory.EnumerateFiles("TestData"))
{
    // Validate file content using fluent assertins.
}

Basically this type of test should be written using xUnit's InlineData or TheoryData.
But sometimes it's useful writing tests with foreach loop.

Object Graph Comparison

This feature is useful for POCO/DTO class (with no IEquatable) data comparison.

// Act
var actual = JsonUtility.Deserialize<BuildJsonConfig>("<path>")

// Assert
actual.Should().BeEquivalent(new BuildJsonConfig{
  // Set properties to assert.
});

Extensibility

It can write custom assertion logics for specific types (e.g. Manifest/OutputFileInfo,DirectoryInfo etc.)
It simplify test assertion codes dramatically. and it can share assertion logics across tests.

Copy link
Contributor

@yufeih yufeih left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I like object graph comparison and improved diagnostics.

@yufeih yufeih merged commit bdd5161 into dotnet:main Jul 10, 2023
@filzrev filzrev deleted the add-fluentassertions branch July 17, 2023 00:18
@yufeih yufeih added the engineering Makes the pull request to appear in the "Engineering" section of the next release note label Jul 24, 2023
p-kostov pushed a commit to ErpNetDocs/docfx that referenced this pull request Jun 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
engineering Makes the pull request to appear in the "Engineering" section of the next release note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants