取消

Introducing MSTestEnhancer to make unit test result easy to read

Don’t you think that naming is very very hard? Especially naming for unit test method? Read this article for more data of naming: Don’t go into programming if you don’t have a good thesaurus - ITworld.

MSTestEnhancer is a contract-style unit test extension for MSTestv2. You can write method contract descriptions instead of writing confusing test method name when writing unit tests.


You’ll get the test result like the picture shown below:

Test results
▲ The unit test result that listed via ReSharper.

Classical Style of Writing Unit Tests

We used to be recommended to write unit test like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[TestClass]
public class TheTestedClassTest
{
    [TestMethod]
    public void TheTestedMethod_Condition1_Expect1()
    {
        // Test code here...
    }

    [TestMethod]
    public void TheTestedMethod_Condition2_Expect2()
    {
        // Test code here...
    }
}

It is an example using MSTest. If we use NUnit or XUnit, we’ll get similar test code, too. Sometimes the conditions and expects are more complex, and we cannot write them down with only a few words. And the more complex the method name is, the more difficult for coders to read and comprehend them.

Introduce MSTestEnhancer

MSTestEnhancer is a MSTest v2 extension to connect unit test and the method that should be tested. You’ll find out that all unit test contracts are listed under target methods, and you can see all the result of them directly, no need to translate the obscure method name into what you want to test.

Now, let’s start!

  1. Install MSTestEnhancer from the nuget.org.
  2. Write unit test code in the style listed below.

Assuming that you want to test a class named TheTestedClass containing a method named TheTestedMethod. Then you can write unit tests like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    [TestClass]
    public class TheTestedClassTest
    {
        [ContractTestCase]
        public void TheTestedMethod()
        {
            "When Xxx happens, results in Yyy.".Test(() =>
            {
                // Write test case code here...
            });
            
            "When Zzz happens, results in Www.".Test(() =>
            {
                // Write test case code here...
            });
        }
    }

Notice that the name of class and method are almost the name of the tested class and tested method. As a result, we don’t need to think about anything about naming unit test, nor to read the obscure name of the unit test.

Test results

Advanced Usages

Unit Test with Arguments

Some unit tests need multiple values to verify the contracts, so MSTestEnhancer provides WithArguments method to config the arguments.

1
2
3
4
5
6
7
8
9
"prime number.".Test((int num) =>
{
    // Write test case code here...
}).WithArguments(2, 3, 5, 7, 11);

"{0} is not a prime number.".Test((int num) =>
{
    // Write test case code here...
}).WithArguments(1, 4);

You can pass up to 8 parameters into the test case.

1
2
3
4
5
6
7
8
9
10
"Contract 1: {0} and {1} are allowed in the contract description.".Test((int a, int b) =>
{
    // Now, a is 2 and b is 3.
}).WithArguments(2, 3);

"Contract 2".Test((int a, int b) =>
{
    // Now the test case will run twice. The first group, a is 2 and b is 3; and the second group, a is 10 and b is 20.
    // ValueTuple is supported, too.
}).WithArguments((2, 3), (10, 20));

In this example, the contract description will be replaced to the arguments that you have passed into.

Async Unit Test

All Test extension method support async action so that you can test any async method.

Some Fantastic Feature

Nested unit test classes are supported by MSTest v2, so you can write an infinite level unit test tree.

本文会经常更新,请阅读原文: https://blog.walterlv.com/post/introduce-mstest-enhancer.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

知识共享许可协议

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 吕毅 (包含链接: https://blog.walterlv.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系 ([email protected])

登录 GitHub 账号进行评论