Developer Docs

What is xUnit?

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);

Was this article helpful?

Thanks for your feedback!

If you would like to tell us more, please click on the link below to send us a message with more details.







On this page

New to Codebots?

We know our software can be complicated, so we are always happy to have a chat if you have any questions.