DEV Community

Ravi Vijay
Ravi Vijay

Posted on • Edited on • Originally published at Medium

Unit testing with xUnit: Introduction

[Source](https://www.reddit.com/r/wolves/comments/yitg4a/wolves_can_loaf_too/)

Wolf Fact: That's how a pack of wolves sound


Xunit.net is a free, open-source, community-focused unit testing tool for the .NET Framework.

Let’s go. Please visit this link for the code.

Create a .NET console app ( for simplicity) and add an xUnit test project to the solution. Now, we have two projects in the solution — one for application logic and another for unit testing.

xUnit Test Project

xUnit Test Project

Solution XUnitTesting hierarchy

Solution XUnitTesting hierarchy

We should always keep the application code and its tests in separate projects.

  • Application- This project has our application logic.

  • Application.Test- As the name indicates, this project has test cases for the application code.

I wrote a simple method to divide two numbers in Logics class.

Logics.Divide

The access modifier for the method must be public. The xUnit checks all methods in all public exported classes in the assembly.

Now we will test this method. We will write unit tests for this method in the UnitTests class.

Don’t forget to add the Application project reference to the Application.Test project.

Fact-

UnitTests.cs TestDivide()

UnitTests.cs TestDivide()

  • [Fact]: Marks a test method

  • Assert: The assert class contains various static methods. These methods are utilized to evaluate the testing conditions. Example:- Assert.Equal(expected value, actual value), Assert.True(boolean condition)and many more. You can check all assert statements here.

We pass parameter values for the method and then compare the actual and expected values. In the TestDivide() method, we pass parameters as 4 and 2 for the numerator and denominator respectively.

We can run tests using Test Explorer.

Test Explorer

Test Explorer

or

Green Dot

Our test case passed for the numerator 4 and denominator 2. It would fail for numerator 1 and denominator 3.

TestDivide2

We need to improve this test’s coverage. We should specify the precision in the assert statement.

Assert.Equal(expected, actual, precision)

Assert.Equal(expected, actual, precision)

Precision: Verify up to digits after the decimal

There is a drawback to writing test cases like this. We have to create different test methods for various inputs.

Theory-

xUnit theory overcomes the above drawback.

TestDivideMultipleInputs

TestDivideMultipleInputs

  • [Theory]: Marks a test method as being a data theory. A theory is a test case with multiple intakes.

  • [InlineData]: To provide data for theory.

For an InlineData row, the test method executes one time. So the method TestDivideMultipleInputs will be executed three times. We can provide input parameters and desired output for the divide method using InlineData.

Exception-

This testing tool does not address exceptions with the general assert statements. It uses Assert.Throws. It validates that the exact exception is thrown and not a derived exception type.

TestDivideByZero()

TestDivideByZero()

For a zero denominator, the code will throw a DivideByZeroException. The TestDivideByZero() test method validates it. You may try different types of exceptions to verify.

Any feedback would be greatly appreciated. Thank you.

Top comments (0)