前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Introducing MSTestEnhancer to make unit test result easy to read

Introducing MSTestEnhancer to make unit test result easy to read

作者头像
walterlv
发布2018-09-18 13:03:00
3310
发布2018-09-18 13:03:00
举报
文章被收录于专栏:walterlv - 吕毅的博客

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:

Test results
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:

代码语言:javascript
复制
[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:

代码语言:javascript
复制
    [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
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.

代码语言:javascript
复制
"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.

代码语言:javascript
复制
"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)

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-03-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Introducing MSTestEnhancer to make unit test result easy to read
    • Classical Style of Writing Unit Tests
      • Introduce MSTestEnhancer
        • Recommended Style of Writing Unit Tests
          • Advanced Usages
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档