4 mistakes that make your unit testing unreliable

Unit testing code has some special considerations to achieve its porpuse. You should avoid below mistakes to make your tests more reliable.

1- Multiple tests for the same block of code

Example:

 [Test]
 public void Add_5And3_Return8()
 {
     int a = 5, b = 3;
     var result = 8;
     Assert.AreEqual(_calcuator.Add(a, b), result);
 }

 [Test]
 public void Add_6And5_Return11()
 {
     int a = 6, b = 5;
     var result = 11;
     Assert.AreEqual(_calcuator.Add(a, b), result);
 }

The two tests in above example are testing the same logic. If you remove one of them, this will not affect the quality of the tests.

The problem with having multiple tests for same logic appear when failure happen and both tests fail for same reason. Develpers will start questioning the porpuse of both tests and get confused about which one to fix.

2- When you add logic to test code

Example:

        [Test]
        public void Add_Test()
        {
            int a = 6, b = 5;
            var result = a + b;
            Assert.AreEqual(_calcuator.Add(a, b), result);
        }

In above example the is some logic in the test itself. Although it is very simple in this example, a little more complexity will make it much worse.

The issue is that if test fails, it may need the developer to debug the test code to find out the reason.

It’s even worse if the issue is in the test code itself. It’s always better to make test code with very simple logic and static vaues as in following example.

        [Test]
        public void Add_5And3_Return8()
        {
            int a = 5, b = 3;
            var result = 8;
            Assert.AreEqual(_calcuator.Add(a, b), result);
        }

3- Testing multiple concerns in same test

Example:

        [Test]
        public void Calculator_Test()
        {
            int a = 6, b = 5, addResult = 11;
            Assert.AreEqual(_calcuator.Add(a, b), addResult);

            int c = 9, d = 5, subResult = 4;
            Assert.AreEqual(_calcuator.Subtract(c, d), subResult);

            int e = 20, f = 5, divResult = 4;
            Assert.AreEqual(_calcuator.Divide(e, f), divResult);
        }

In above example many functions are being tested in one test. The issue is that this test will fail if any of the tested fuctions failed. It even adds more complexity to the test and require the developer to debug the test to find the failing function.

4- Not reviewing test code

It’s a very common mistake that made by teams to ignore the unit testing code in code review process. Testing code should be considered as important as business code and even more important.

You should cover all previous aspects in code review in addition with the vaidity of the tests themselves.

Feedback

Pease comment below to add any other mistakes that affect unit testing reliability and subscribe to news letters to get notified about latest blog posts.

Leave a Reply

Your email address will not be published. Required fields are marked *