New Pluralsight Course: The xUnit.net Testing Framework

My latest Pluralsight course on the xUnit.net testing framework has just been released.

Course Description

Learn the latest in unit testing technology for C#, VB.NET (and other .NET languages) created by the original inventor of NUnit.

xUnit.net is a free, extensible, open source framework designed for programmers that aligns more closely with the .NET platform.

 

You can check it out now on Pluralsight.com.

Automated Testing: End to End Course

My newest Pluralsight course has just been published.

We shouldn't live in fear of our code

Long-term customer satisfaction, agility, and developer happiness are crucial. A quality suite of automated tests helps achieve this. This practical course covers how and what to test at the unit, integration, and functional UI levels; and how to bring them all together with TeamCity continuous integration build server.

The course helps to keep your software soft with the right automated tests at the right level.

If you’ve never used Pluralsight before you can sign up for the free trial and get started.

Mocking Framework for Windows Store apps (and Windows Phone)

With Windows Store apps there are challenges getting traditional mocking frameworks such as Rhino and Moq working due to limited reflection support in the platform (presumably for security reasons).

I wrote a mocking solution when Windows Phone 7 first came out, it can also be used for Windows Store apps. I’ve updated the NuGet descriptions etc. to reflect this.

How To Do TDD with Mocking in Windows Store Apps

Create a new (C#/XAML) Windows Store app project in Visual Studio called “MyAwesomeApp”.

Create your test project “MyAwesomeApp.Tests” and reference your main app.

initsolutionshot

In the main app project, install the MoqaLate NuGet package.  When the package is installed you will have a new folder in the main app solution called “MoqaLateCommandLine”:

moqalteinstalled

(Inside this folder is a readmexxx.txt file with some additional info)

More...

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 Pluralsight course: The xUnit.net testing framework. Also check out my xUnit.net cheat sheet.

Quick-Start Guide To Using xUnit To Test Your Windows 8 WinRT Store app

Until xUnit officially supports* Windows store apps you can get xUnit working with your WinRT app by doing the following:

In Visual Studio 2012, go to Tools menu, Extensions and Updates; search for and install “xUnit.net runner for Visual Studio 2012”.

xUnitRunner

In Visual Studio 2012, go to Tools menu, Extensions and Updates; search for and install “xUnit Test Library Template”.

xUnitTemplate

You may have to restart Visual Studio…

More...

Introducing (probably) The World's Only Mocking Framework for Windows Phone 7 (WP7)

Introducing MoqaLate

Whilst I love developing apps for Windows Phone 7, the testing aspect is hard! I'm a TDD-er by default and it's such a pain to have to hand roll my own mock objects. 

So I created MoqaLate.

It's an alpha version but is usable now.

Not sure framework is the right term but it's something that generates mocks from your interfaces.

Add to existing project from NuGet:

PM> Install-Package MoqaLate

Read more about the project.

Download an example solution.

Read (currently very basic!) documentation.

View on NuGet.org

Awesome overview diagram :)

 

Cleaner Code in Unit Tests

One thing that can quickly become messy when writing unit tests is the creation of test objects. If the test object is a simple int, string, etc then it's not as much of a problem as when you a list or object graph you need to create.

Even using object initializers you can end up taking a lot of lines of indented code, and gets in the way of the tests.

One solution is to use a 'builder' class which will construct the object for you.

For example, rather than lots of initializer code you could write:

            _sampleData = new HistoryBuilder()
                .WithTimer(false11new DateTime(200011))
                .WithTimer(false11new DateTime(200011))
                .WithTimer(true11new DateTime(200011))
                .Build()

 You can multiple overloads of WithTimer (for example one which create adds a default Timer).

 Implementation of HistoryBuilder:

    public class HistoryBuilder
    {
        private readonly History _history;
        public HistoryBuilder()
        {
            _history = new History();
        }
        public HistoryBuilder WithTimer()
        {
            _history.Timers.Add(new Timer());
            return this;
        }
        public HistoryBuilder WithTimer(bool completed, int internalInteruptions, int externalInteruptions,
                                        DateTime time)
        {
            _history.Timers.Add(new Timer
                                    {
                                        Completed = completed,
                                        InternalInteruptionsCount = internalInteruptions,
                                        ExternalInteruptionsCount = externalInteruptions,
                                        StartedTime = time
                                    });
            return this;
        }
        public History Build()
        {
            return _history;
        }
    }