DotNet Unit Tests Naming Conventions and Static Methods

System Under Test (Sut)

Developping using TDD, I am using a naming convention for my Unit Tests, where the code I am testing is always refered as Sut for System Under Test. That way when I look in my code it s way easier for me to see what is actually being tested.

Static methods

This naming convention is not working when using static methods. … Unless using aliases. As I am using one test class per class I am developping, I am using a simple trick to keep this naming convention. Namespace aliases :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sut = MyWrapper.LogApiWrapper;

namespace ExtratorTests
{
    [TestClass]
    public class LogApiWrapperTests
    {
        [TestMethod]
        public void TestAuth()
        {
            Assert.IsTrue(string.IsNullOrEmpty(Sut.Token));
            Assert.IsTrue(Sut.Authenticate());
            Assert.IsFalse(string.IsNullOrEmpty(Sut.Token));
        }
    }
}

Testing Static Private Methods

Easy way to test a private static method :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
    [TestMethod()]
    public void ExtractTokenTest()
    {
        string expectedToken = "eyJhbGciOiJSUzI1NiIsImprdSI6Imh0dHBzOi8vbXctZGV2LmFt";
        string response = "{\"access_token\":\""+ expectedToken +"\",\"jti\":\"704b6ca709124e38841e801b95cdf0e4\"}";
        PrivateType privateTypeObject = new PrivateType(typeof(Sut));
        object obj = privateTypeObject.InvokeStatic("ExtractToken", response);

        Assert.AreEqual(expectedToken, (string)obj);
    }