Asserting the Results of Tests in xUnit.net

The below is an excerpt from the latest chapter “An Introduction to Unit Testing With xUnit.net” from my book Keeping Software Soft.

 

xUnit.net provides a number of ways for checking (asserting) results are as expected.


The following explanatory tests shown the different types of assertions that xUnit.net supports:

using System;
using System.Collections.Generic;
using Xunit;

namespace KeepingSoftwareSoftSamples.XUnitTestingDemo
{
    public class XUnitAssertExamples
    {
        [Fact]
        public void SimpleAssertsThatOneValueEqualsAnother()
        {
            Assert.Equal(1, 2); // fail
            Assert.Equal("hello", "hello"); // pass

            Assert.NotEqual(1, 2); // pass
            Assert.NotEqual("hello", "hello"); // fail
        }

        [Fact]
        public void BooleanAsserts()
        {
            Assert.True(true); // pass
            Assert.True(false); // fail

            Assert.False(false); // pass
            Assert.False(true); // fail

            // Don't do this
            Assert.True(1 == 1); // pass
        }

        [Fact]
        public void Ranges()
        {
            const int value = 22;

            Assert.InRange(value, 21, 100); // pass
            Assert.InRange(value, 22, 100); // pass

            Assert.NotInRange(value, 999, 99999); // pass

            Assert.InRange(value, 23, 100); // fail
        }

        [Fact]
        public void Nulls()
        {
            Assert.Null(null); // pass

            Assert.NotNull("hello"); // pass

            Assert.NotNull(null); // fail
        }

        [Fact]
        public void ReferenceEquality()
        {
            var objectA = new Object();
            var objectB = new Object();

            Assert.Same(objectA, objectB); // fail

            Assert.NotSame(objectA, objectB); // pass
        }

        [Fact]
        public void AnIEnumberableContainsASpecificItem()
        {
            var days = new List<string>
                                 {
                                     "Monday",
                                     "Tuesday"
                                 };

            Assert.Contains("Monday", days); // pass
            Assert.Contains("Friday", days); // fail

            Assert.DoesNotContain("Friday", days); // pass
        }

        [Fact]
        public void IEnumerableEmptiness()
        {
            var aCollection = new List<string>();

            Assert.Empty(aCollection); // pass

            aCollection.Add("now no longer empty");

            Assert.NotEmpty(aCollection); // pass

            Assert.Empty(aCollection); // fail            
        }

        [Fact]
        public void IsASpecificType()
        {
            Assert.IsType<string>("hello"); // pass

            Assert.IsNotType<int>("hello"); // pass

            Assert.IsType<int>("hello"); // fail
        }

        [Fact]
        public void IsAssignableFrom()
        {
            const string stringVariable = "42";

            Assert.IsAssignableFrom<string>(stringVariable); // pass

            Assert.IsAssignableFrom<int>(stringVariable); // fail
        }
    }
}

To learn more about xUnit.net check out my xUnit.net Pluralsight course.

You can start watching with a Pluralsight free trial.

SHARE:

Add comment

Loading