在一个单元测试多个断言不好?即使链接? [英] Are multiple asserts bad in a unit test? Even if chaining?

查看:203
本文介绍了在一个单元测试多个断言不好?即使链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有什么错,在这个单元测试检查这么多东西?

Is there anything wrong with checking so many things in this unit test?:

ActualModel = ActualResult.AssertViewRendered()        // check 1
                          .ForView("Index")            // check 2
                          .WithViewData<List<Page>>(); // check 3

CollectionAssert.AreEqual(Expected, ActualModel);      // check 4

本次测试的主要目的是验证右视图返回(检查2),它包含了正确的数据(检查4)。

The primary goals of this test are to verify the right view is returned (check 2) and it contains the right data (check 4).

我将获得由分成多个测试这样的东西?我所有关于正确地做事,但我不会去的东西分裂,如果它不具有实用价值。

Would I gain anything by splitting this into multiple tests? I'm all about doing things right, but I'm not going to split things up if it doesn't have practical value.

我是pretty新的单元测试,所以要温柔。

I'm pretty new to unit testing, so be gentle.

推荐答案

正如其他人所指出的那样,这是最好的一个断言坚持在每个测试,以避免丢失信息 - 如果第一个断言失败,你不知道后来者是否也会失败。你要修复与少信息的问题 - 这可能(可能会)更难

As others have pointed out, it's best to stick with one assert in each test in order to avoid losing information - if the first assert fails, you don't know whether the later ones would fail also. You have to fix the problem with less information - which might (probably will) be harder.

一个很好的参考是罗伊Osherove的单元测试的艺术 - 如果你想获得与单元测试正确启动,你可以做比从这里开始差了不少。

A very good reference is Roy Osherove's Art of Unit Testing - if you want to get started right with Unit Testing, you could do a lot worse than starting here.

这篇关于在一个单元测试多个断言不好?即使链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆