Introducing MSTestEnhancer to make unit test result easy to read

Introducing MSTestEnhancer to make unit test result easy to read

发布于 2018-03-05 06:21 更新于 2018-08-12 08:03

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.


中文 English

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

▲ 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:

[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.

Recommended Style of Writing Unit Tests

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

    [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.

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.

"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.

"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://walterlv.com/post/introduce-mstest-enhancer.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Spring相关

第2章—装配Bean—自动化装配Bean

CD和CDPlayer,如果你不将CD放入(注入)到播放器中,那么CD播放器其实是没多大用处的,所以说,CD播放器是依赖于CD才能完成它的使命.

9320
来自专栏Flutter&Dart

DartVM服务器开发(第二十二天)--RestfulApi封装

阅读该文章之前,我会默认的认为你已经看过我之前的文章,并知道如何使用数据库,开启服务器等!

24030
来自专栏Java与Android技术栈

给 Java 和 Android 构建一个简单的响应式Local Cache

首先,Local Cache 不是类似于 Redis、Couchbase、Memcached 这样的分布式 Cache。Local Cache 适用于在单机环境...

9320
来自专栏一个会写诗的程序员的博客

Spring Boot 使用 Kotlin Script Template 模板引擎kts 开发web

在 Spring Framework 5.0 M4 中引入了一个专门的Kotlin支持。

24420
来自专栏ImportSource

Spring Boot下的TDD(测试驱动开发)

首先来看下TDD三原则吧: You are not allowed to write any production code unless it is to m...

2K110
来自专栏bboysoul

给你的linux电脑跑个分(unixbench)

第一次碰到unixbench是在看LinusTechTips视频的时候碰到的,那时候没有对这个软件产生兴趣,直到今天在看博客的时候又碰到了这个软件,所以想研究下...

39120
来自专栏技术墨客

Spring核心——纯Java运行与@Bean

在3.0之前的Spring核心框架中,我们启动一个Spring容器必须使用一个XML文件。而到了3.X之后的版本Spring为创建容器新增了一个入口类——Ann...

8930
来自专栏玩转JavaEE

Spring常用配置(二)

按:最近公众号文章主要是整理一些老文章,主要是个人CSDN上的博客,也会穿插一些新的技术点。 ---- OK,上篇博客我们介绍了Spring中一些常见的配置,上...

36530
来自专栏王硕

原 建立共享存储实验记录

383120
来自专栏生信宝典

Python学习教程(五)

作业(二) 将 “作业(一)” 中的程序块用函数的方式重写,并调用执行 def func(para1,para2,…): func(para1,para2,…)...

21790

扫码关注云+社区

领取腾讯云代金券