C# 7 Tuple Better Test Assertion
Tags: unittest
Published: 2019-04-20 01:38:34

---
title: "C# 7 Tuple Better Test Assertion"
published: true
description: "Apply the new Tuple feature in C# 7 to write a readable unit test code, shorten the assertion part"
tags: UnitTest, C#
canonical_url: https://www.thaianhduc.com/2019/04/c-7-tuple-better-test-assertion/
---

*Original posted on [my blog](https://www.thaianhduc.com/2019/04/the-process-of-making-elegant-unit-tests/) April 18, 2019*


Recently I read [C# In Depth 4.0](https://www.manning.com/books/c-sharp-in-depth-fourth-edition) where I met the new Tuple design in [C# 7](https://devblogs.microsoft.com/dotnet/new-features-in-c-7-0/). It is really a cool feature. Besides the syntax sugar, it offers capacities that developers can leverage.

At the time of reading it, I was tasked with writing unit tests in my job. It triggered my memory about [Semantic Comparison with Likeness](https://github.com/AutoFixture/SemanticComparison). The main idea of semantic comparison is to compare 2 objects with certain properties. It allows developers to define what equality means. The tuple supports equality by default. So maybe I should be able to use the tuple to accomplish the same thing as Likeness.

In this post, I will write a simple unit test without Likeness or tuple, then refactors it with Likeness, finally uses Tuple. Let's explore some code.

```CSharp
public class Product
{
    public Guid Id { get; set;}
    public string Name { get; set;}
    public double Price { get; set;}
    public string Description { get; set;}
}

[TestFixture]
public class ProductTests
{
    [Test]
    public void Test_Are_Products_Same()
    {
        var expectedProduct = new Product
        {
            Name = "C#",
            Price = 10,
            Description = "For the purpose of demoing test"
        };

        var reality = new Product
        {
            Name = "C#",
            Price = 10,
            Description = "For the purpose of demoing test"
        };

        // Assert that 2 products are the same. Id is ignored
    }
}
```

The task is simple. How are we going to assert the 2 products?

## Old Fashion

Very simple. We simply assert property by property.

```CSharp
[TestFixture]
public class ProductTests
{
    [Test]
    public void Test_Are_Products_Same()
    {
        var expectedProduct = new Product
        {
            Name = "C#",
            Price = 10,
            Description = "For the purpose of demoing test"
        };

        var reality = new Product
        {
            Name = "C#",
            Price = 10,
            Description = "For the purpose of demoing test"
        };

        Assert.AreEqual(expectedProduct.Name, reality.Name);
        Assert.AreEqual(expectedProduct.Price, reality.Price);
        Assert.AreEqual(expectedProduct.Description, reality.Description);
    }
}
```

Some might think that we should override the Equals method in the Product class. I do not think it is a good idea. The definition of equality between production and unit test are tremendously different. Be careful before overriding equality.

The product class has 3 properties (except the Id property). So the code still looks readable. Think about the situation where there are 10 properties.

## Likeness - Semantic Comparison

There is a [blog post](https://www.rahulpnath.com/blog/semantic-comparison-improve-test-assertions/) explaining it in the detail. In this demo, we can rewrite our simple test.

```CSharp
[TestFixture]
public class ProductTests
{
    [Test]
    public void Test_Are_Products_Same()
    {
        var expectedProduct = new Product
        {
            Name = "C#",
            Price = 10,
            Description = "For the purpose of demoing test"
        }.AsSource()
        .OfLikeness<Product>();

        var reality = new Product
        {
            Name = "C#",
            Price = 10,
            Description = "For the purpose of demoing test"
        };

        Assert.AreEqual(expectedProduct, reality);
    }
}
```

Likeness is a powerful tool in your testing toolbox. Check it out if you are interested in.

## Tuple - Customized

The idea is that we can produce a tuple containing asserted properties and compare them. This allows us to flatten the structure if wished.

```CSharp
[TestFixture]
public class ProductTests
{
    [Test]
    public void Test_Are_Products_Same()
    {
        var expectedProduct = (Name = "C#", Price = 10, Description = "For the purpose of demoing test");

        var reality = new Product
        {
            Name = "C#",
            Price = 10,
            Description = "For the purpose of demoing test"
        };

        Assert.AreEqual(expectedProduct, (reality.Name, reality.Price, reality.Description));
    }
}
```

It might not look different from the Likeness approach. And I do not say which approach is better. It is just another way of doing things.

## Summary

So which approach is better? None of them. Each has its own advantages and disadvantages. They are options in your toolbox. How they are used depends on you, developers. Definitely, I will take advantages of the new Tuple in both production and unit test code.