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 NFluent to the .NET Open Source Developer Projects #1310

Merged
merged 1 commit into from
Jun 13, 2023

Conversation

blabaere
Copy link
Contributor

NFluent is a fluent assertion library with crystal-clear error messages.

NFluent is a fluent assertion library with crystal-clear error messages.
@Apollo9999
Copy link
Contributor

NFluent is a fluent assertion library for .NET that provides an expressive and readable way to write assertions in your tests or code. It aims to provide clear error messages to aid in understanding the cause of test failures. Here's how you can define NFluent:

Install NFluent: You can install NFluent via NuGet package manager in Visual Studio or using the .NET CLI. Search for "NFluent" and install the appropriate package for your project.

Import NFluent namespace: In your C# code file, add the following using directive at the top to import the NFluent namespace:

Writing assertions: NFluent provides a fluent interface for writing assertions. You can use various methods and chaining to express your assertions in a more readable and concise way.
NFluent offers a wide range of assertion methods to check for equality, inequality, nullity, collections, strings, exceptions, and more. You can chain multiple assertions together to form more complex assertions.

Error messages: One of the key features of NFluent is its clear error messages. When an assertion fails, NFluent provides detailed information about the expected and actual values, making it easier to diagnose the issue. Here's an example of an NFluent error message:

using NFluent;
using Xunit;

public class SampleTests
{
[Fact]
public void TestNumberEquality()
{
int number = 42;

    Check.That(number).IsEqualTo(42);
}

[Fact]
public void TestStringContainment()
{
    string message = "Hello, World!";

    Check.That(message).Contains("Hello");
    Check.That(message).DoesNotContain("Goodbye");
}

[Fact]
public void TestCollection()
{
    int[] numbers = { 1, 2, 3, 4, 5 };

    Check.That(numbers).Contains(3);
    Check.That(numbers).Not.Contains(6);
    Check.That(numbers).IsOnlyMadeOf(1, 2, 3, 4, 5);
}

}
we have a test class named SampleTests with three test methods decorated with the [Fact] attribute from xUnit.net. Each test method demonstrates different types of assertions using NFluent.

The first test method, TestNumberEquality, asserts that the variable number is equal to 42.

The second test method, TestStringContainment, asserts that the message string contains the substring "Hello" and does not contain the substring "Goodbye".

The third test method, TestCollection, performs assertions on an array of integers. It checks if the array contains the value 3, does not contain the value 6, and is only made up of the specified values.

To run these tests, you'll need to have xUnit.net and NFluent installed in your project. You can use the test runner of your choice (such as Visual Studio Test Explorer) to execute the tests and see the test results, including any assertion failures.

Make sure to install the NFluent package via NuGet in your project to resolve the NFluent namespace and its assertion method

1 similar comment
@Apollo9999
Copy link
Contributor

NFluent is a fluent assertion library for .NET that provides an expressive and readable way to write assertions in your tests or code. It aims to provide clear error messages to aid in understanding the cause of test failures. Here's how you can define NFluent:

Install NFluent: You can install NFluent via NuGet package manager in Visual Studio or using the .NET CLI. Search for "NFluent" and install the appropriate package for your project.

Import NFluent namespace: In your C# code file, add the following using directive at the top to import the NFluent namespace:

Writing assertions: NFluent provides a fluent interface for writing assertions. You can use various methods and chaining to express your assertions in a more readable and concise way.
NFluent offers a wide range of assertion methods to check for equality, inequality, nullity, collections, strings, exceptions, and more. You can chain multiple assertions together to form more complex assertions.

Error messages: One of the key features of NFluent is its clear error messages. When an assertion fails, NFluent provides detailed information about the expected and actual values, making it easier to diagnose the issue. Here's an example of an NFluent error message:

using NFluent;
using Xunit;

public class SampleTests
{
[Fact]
public void TestNumberEquality()
{
int number = 42;

    Check.That(number).IsEqualTo(42);
}

[Fact]
public void TestStringContainment()
{
    string message = "Hello, World!";

    Check.That(message).Contains("Hello");
    Check.That(message).DoesNotContain("Goodbye");
}

[Fact]
public void TestCollection()
{
    int[] numbers = { 1, 2, 3, 4, 5 };

    Check.That(numbers).Contains(3);
    Check.That(numbers).Not.Contains(6);
    Check.That(numbers).IsOnlyMadeOf(1, 2, 3, 4, 5);
}

}
we have a test class named SampleTests with three test methods decorated with the [Fact] attribute from xUnit.net. Each test method demonstrates different types of assertions using NFluent.

The first test method, TestNumberEquality, asserts that the variable number is equal to 42.

The second test method, TestStringContainment, asserts that the message string contains the substring "Hello" and does not contain the substring "Goodbye".

The third test method, TestCollection, performs assertions on an array of integers. It checks if the array contains the value 3, does not contain the value 6, and is only made up of the specified values.

To run these tests, you'll need to have xUnit.net and NFluent installed in your project. You can use the test runner of your choice (such as Visual Studio Test Explorer) to execute the tests and see the test results, including any assertion failures.

Make sure to install the NFluent package via NuGet in your project to resolve the NFluent namespace and its assertion method

@richlander richlander closed this Jun 13, 2023
@richlander richlander reopened this Jun 13, 2023
@mairaw mairaw merged commit dc2dccc into microsoft:main Jun 13, 2023
Mattes0h pushed a commit to Mattes0h/dotnet that referenced this pull request Jul 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants