每个单元测试应该测试多少?

编程入门 行业动态 更新时间:2024-10-26 02:30:52
本文介绍了每个单元测试应该测试多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我的每个单元测试应该检查多少?例如我有这个测试

How much should each of my unit tests examine? For instance I have this test

[TestMethod]
public void IndexReturnsAView()
{
    IActivityRepository repository = GetPopulatedRepository();
    ActivityController activityController = GetActivityController(repository);
    ActionResult result = activityController.Index();
    Assert.IsInstanceOfType(result, typeof(ViewResult));
}

还有

[TestMethod]
public void IndexReturnsAViewWithAListOfActivitiesInModelData()
{
    IActivityRepository repository = GetPopulatedRepository();
    ActivityController activityController = GetActivityController(repository);
    ViewResult result = activityController.Index() as ViewResult;
    Assert.IsInstanceOfType(result.ViewData.Model, typeof(List<Activity>));
}

显然,如果第一个测试失败,那么第二个测试也会失败,所以应该将这两个测试合并为一个带有两个断言的测试吗?我的感觉是测试越细化,每个测试检查的次数越少,找到失败原因的速度就越快.然而,拥有大量非常小的测试会产生开销,这可能会花费时间运行所有测试.

Obviously if the first test fails then so will the second test so should these two be combined into one test with two asserts? My feeling is that the more granular the tests and the less each test checks the faster it will be to find the causes of failures. However there is overhead to having a huge number of very small tests which might cost time in running all the tests.

推荐答案

我建议尽可能将它们分解.

I'd recommend breaking them down as much as possible.

这有很多原因,恕我直言,最重要的是:

There are lots of reasons for this, IMHO the most important ones are:

您的一项测试失败时,您希望能够尽可能快速、安全地准确隔离出问题所在.让每种测试方法只测试一件事情是实现这一目标的最佳方式.

When one of your tests fails, you want to be able to isolate exactly what went wrong as quickly and as safely as possible. Having each test-method only test one single thing is the best way to achieve this.

每个测试都需要从头开始.如果您创建存储库一次,然后在 2 个或更多测试中使用它,那么您对这些测试的顺序有隐含的依赖性.假设 Test1 向存储库添加了一个项目,但忘记删除它.Test2 的行为现在将有所不同,并可能导致您的测试失败.唯一的例外是不可变数据.

Each test needs to start with a clean slate. If you create the repository once and then use it in 2 or more tests, then you have an implicit dependency on the order of those tests. Say Test1 adds an item to the repository but forgets to delete it. Test2's behavior will now be different, and possibly cause your test to fail. The only exception to this is immutable data.

关于您的速度问题,我不担心.对于像这样的纯代码处理,.NET 非常 速度非常快,您永远无法分辨其中的区别.一旦您摆脱代码处理并进入诸如数据库之类的事情,那么您就会感受到性能问题,但是一旦您这样做,您就会遇到上述所有干净的石板"问题,所以您可能只是必须忍受它(或尽可能多地使您的数据不可变).

Regarding your speed concerns, I wouldn't worry about it. For pure code-crunching like this, .NET is very fast, and you'll never be able to tell the difference. As soon as you get out of code-crunching and into things like databases, then you'll feel the performance issues, but as soon as you do that you run into all the "clean slate" issues as described above, so you may just have to live with it (or make as much of your data immutable as possible).

祝您测试顺利.

这篇关于每个单元测试应该测试多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 07:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1406740.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试   测试

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!