C# Testing set up

I have spent a ton of time doing testing for the last several weeks, so I get to teach my friends in a C# study group some basic testing in C# tomorrow. It’s been a minute since I’ve done this (February, maybe?). So I wanted to get the process down somewhere in case I go awhile without working in C# again.

I am assuming you have C# and .NET already on your machine, but if you don’t, the docs are pretty easy to follow.

Setting it up

First, create a directory and cd into it

mkdir MythicalCreatures
cd MythicalCreatures

I’m going to use the general idea of some of the mythical creatures exercises we did at Turing, because why not? I have no idea who wrote these originally, and that repo is what I found from a quick google.

Also, C# uses PascalCase (I’m writing this so I remember)

Next, create the .NET class library

dotnet new classlib -n MythicalCreatures

Then, create a directory for the tests

mkdir MythicalCreatures.Tests
cd MythicalCreatures.Tests

Create a new xUnit test project

dotnet new xunit -n MythicalCreatures.Tests
cd MythicalCreatures.Tests

And then add a reference to the main project

dotnet add reference ../MythicalCreatures/MythicalCreatures.csproj

For what it’s worth, here is where I am located

/Users/myUserName/csharpstudy/MythicalCreatures/MythicalCreatures.Tests/MythicalCreatures.Tests

I am going to run all of this in VSCode, so I will go back to the root of my project and run

code .

That is what it looks like when I open VSCode. To set it up, go to the main project directory, and I am going to create a models directory to put the file inside of that

cd MythicalCreatures
mkdir Models
touch Models/Unicorn.cs

And same thing for tests, and I removed the UnitTest1 test file.

cd ..
cd MythicalCreatures.Tests
mkdir Models
touch Models/UnicornTest.cs
rm UnitTest1.cs 

Anyway, now to set up the actual tests. I think the whole point of mythical creatures was to get more comfortable with test-driven development, so I’m going to turn the Ruby tests into C# tests, and see if I can think of a few more to add in to explore what XUnit can do.
Few things to note before we get to the code. In xUnit, you add [Fact] above the test to mark it as a test method. [Theory] is one of the other ones, for parameterized tests, and I’ll try to think of a way to implement that too. If it’s not there, xUnit won’t recognize the method as a test. Basically, use [Fact] if there are no parameters and it is a standalone test.

Writing the tests

This is the basic structure, with the namespace, public class, and all that stuff. It helps me to see it this way and fill things in

using Xunit;
using MythicalCreatures.Models; // This namespace needs to match wherever Unicorn.cs is located

namespace MythicalCreatures.Tests.Models
{
    public class UnicornTests
    {

        }
    }
}

And the first test, to make sure that the unicorn has a name. For the test method, it needs to be assigned an attribute, in this case it will be [Fact]

I want to test if it can correctly assign a name when a new object is created. The ruby tests use Robert, and my toddlers are asleep so I can’t ask them to name the unicorn, so we will stick with Robert. The method is HasAName().
Next, the test logic is inserted. Create a new unicorn object, give it a name, and then Assert.Equal to make sure it works. Not too complicated

using Xunit;
using MythicalCreatures.Models; // Ensure this namespace matches where Unicorn.cs is located

namespace MythicalCreatures.Tests.Models
{
    public class UnicornTests
    {
        [Fact]
        public void HasAName()
        {
            var unicorn = new Unicorn("Robert");
            Assert.Equal("Robert", unicorn.Name);
        }
    }
}

Next, take the other tests and add them in. This will require some code to be put in the Unicorn.cs file.

We’re testing

  1. It should default to white
  2. You can change the color if you want to
  3. Can it say things (that are sparkly)

These add in Assert.True to make sure that it is the correct color, or Assert.False if you want to test that.

using Xunit;
using MythicalCreatures.Models; // This namespace needs to match wherever Unicorn.cs is located

namespace MythicalCreatures.Tests.Models
{
    public class UnicornTests
    {
        [Fact]
        public void HasAName()
        {
            var unicorn = new Unicorn("Robert");
            Assert.Equal("Robert", unicorn.Name);
        }

        [Fact]
        public void IsWhiteByDefault()
        {
            var unicorn = new Unicorn("Margaret");
            Assert.Equal("white", unicorn.Color);
            Assert.True(unicorn.IsWhite);
        }

        [Fact]
        public void DoesNotHaveToBeWhite()
        {
            var unicorn = new Unicorn("Barbara", "purple");
            Assert.Equal("purple", unicorn.Color);
            Assert.False(unicorn.IsWhite);
        }

        [Fact]
        public void SaysSparklyStuff()
        {
            var unicorn = new Unicorn("Johnny");
            Assert.Equal("**;* Wonderful! **;*", unicorn.Say("Wonderful!"));
            Assert.Equal("**;* I don't like you very much. **;*", unicorn.Say("I don't like you very much."));
        }
    }
}

You’ll obviously need the other code to run this, so here it is. Mine is in MythicalCreatures/Models/Unicorn.cs

namespace MythicalCreatures.Models
{
    public class Unicorn
    {
        public string Name { get; }
        public string Color { get; }

        public Unicorn(string name, string color = "white")
        {
            Name = name;
            Color = color;
        }
        public bool IsWhite => Color.Equals("white", System.StringComparison.OrdinalIgnoreCase);
        public string Say(string message)
        {
            return $"**;* {message} **;*";
        }
    }
}

To run the test, go to the root directory (make sure there is a .sln file in there, mine is MythicalCreatures.generated.sln)

You need to restore the dependencies, build the solution to compile the project, and run the tests.

dotnet restore
dotnet build
dotnet test

And hopefully, it returns this

This was some very basic setup for xUnit, hope it helps!


Comments

One response to “C# Testing set up”

  1. […] 详情参考 […]

Leave a Reply

Discover more from Brendan Bondurant

Subscribe now to keep reading and get access to the full archive.

Continue reading