DEV Community

Cover image for Don't duplicate logic in Asserts: The most common mistake on unit testing
Cesar Aguirre
Cesar Aguirre

Posted on • Edited on • Originally published at canro91.github.io

Don't duplicate logic in Asserts: The most common mistake on unit testing

I originally posted this post on my blog a couple of weeks ago. It's part of a series I've been publishing, called Unit Testing 101.

We have covered some common mistakes when writing unit tets. Some of them may seem obvious. But, we all have made this one mistake when we started to write unit tests. This is the most common mistake when writing unit tests and how to fix it.

Don't repeat the logic under test when verifying the expected result of a test. Instead, use known, hard-coded, pre-calculated values.

Let's write some tests for Stringie, a (fictional) library to manipulate strings with a fluent interface. Stringie has a Remove() method to remove substrings from the end of a string.

We can use Stringie Remove() method like this,

var hello = "Hello, world!";

string removed = hello.Remove("world!").From(The.End);
// "Hello,"
Enter fullscreen mode Exit fullscreen mode

Don't Copy and Paste the tested logic

When writing unit tests, don't copy the tested logic and paste it into private methods to use them inside assertions.

If we bring the tested logic to private methods in our tests, we will have code, and bugs, in two places. Duplication is the root of all evil. Even, inside our tests.

Please, don't write assertions like the one in this test.

[TestMethod]
public void Remove_ASubstring_RemovesThatSubstringFromTheEnd()
{
    string str = "Hello, world!";

    string transformed = str.Remove("world!").From(The.End);

    Assert.AreEqual(RemoveFromEnd(str, "world!"), transformed);
    //              πŸ‘†πŸ‘†πŸ‘†
}

private string RemoveFromEnd(string str, string substring)
{
    var index = str.IndexOf(substring);
    return index >= 0 ? str.Remove(index, substring.Length) : str;
}
Enter fullscreen mode Exit fullscreen mode

building of apartments

That's a lot of duplication. Photo by Danist Soh on Unsplash

Don't make internals public

Also, by mistake, we expose internals of the tested logic to use them in assertions. We make private methods public and static. Even to test those private methods directly.

From our Unit Testing 101, we learned to write unit tests through public methods. We should test the observable behavior of our tested code. A returned value, a thrown exception or an external invocation made.

Again, don't write assertions like the one in this test.

[TestMethod]
public void Remove_ASubstring_RemovesThatSubstringFromTheEnd()
{
    string str = "Hello, world!";

    string transformed = str.Remove("world!").From(The.End);

    Assert.AreEqual(Stringie.PrivateMethodMadePublicAndStatic(str), transformed);
    //                      πŸ‘†πŸ‘†πŸ‘†
}
Enter fullscreen mode Exit fullscreen mode

Use known values to Assert

Instead of duplicating the tested logic, by exposing internals or copy-pasting code into assertions, use a known expected value.

If we end up using the same expected values, we can create constants for them. Like const string Hello = "Hello"; or const string HelloAndComma = "Hello,"; for our example.

For our sample test, simply use the expected substring "Hello,". Like this,

[TestMethod]
public void Remove_ASubstring_RemovesThatSubstringFromTheEnd()
{
    string str = "Hello, world!";

    string transformed = str.Remove("world!").From(The.End);

    // Let's use a known value in our assertions
    Assert.AreEqual("Hello,", transformed);
    //              πŸ‘†πŸ‘†πŸ‘†
}
Enter fullscreen mode Exit fullscreen mode

VoilΓ ! That's the most common mistake when writing unit tests. It seems silly! But, often we duplicate Math operations and string concatenations and it passes unnoticed. Remember, don't put too much logic in your tests. Tests should be only assignments and method calls.

If you're new to unit testing, read my post on how to write your first unit tests in C# with MSTest and check the 4 common mistakes when writing your first tests. Also, grab your free copy of my eBook Unit Testing 101.


Hey! I'm Cesar, a software engineer and lifelong learner. If you want to upgrade your unit testing skills, check my course: Mastering C# Unit Testing with Real-world Examples on Udemy. Practice with hands-on exercises and learn best practices by refactoring real-world unit tests.

Happy testing!

Top comments (0)