Start modelling your app today.

Get started for free

What's this?

What is xUnit?

An introduction to xUnit and its use in the C#Bot tests

xUnit is a free and open source testing tool for .Net applications, it is the primary framework used for writing and running tests in C#Bot. xUnit is frequently used for test assertions and its tests are identified using by using either the ’[Fact]‘ or ’[Theory]‘ annotations.

Fact vs Theory Tests

The primary difference between fact and theory tests in xUnit is whether the test has any parameters. Theory tests take multiple different inputs and hold true for a particular set of data, whereas a Fact is always true, and tests invariant conditions. This is illustrated in the example below.

using Xunit;

namespace xunitTestExample {
    public class xunitTestClass {

        //Fact tests do not take inputs.
        //As a result, the result of the test should depend only on actions performed within the test
        public void factExample() {
            Assert.Equal((1 + 2), 3);

        //Inputs are used in Theory tests to ensure that the functionality works in multiple circumstances
        //Data to be passed into the tests is defined in the 'InlineData' tags.
        //The test runs one time for each of the 'InlineData' tags present in the annotation
        [InlineData(1, 2, 3)]
        [InlineData(10, 2, 12)]
        [InlineData(3, 3, 6)]
        [InlineData(7, 14, 21)]
        public void theoryExample(int firstValueToAdd, int secondValueToAdd, int expectedOutput) {
            Assert.Equal((firstValueToAdd + secondValueToAdd), expectedOutput);

Question: Do you understand the differences between fact and theory tests in xUnit, and the circumstances where you would use them?

Ready to start building?

Sign up to Codebots today to see how much faster you can build apps with us.