您如何对单元测试进行单元测试? [英] How do you unit test a unit test?

查看:124
本文介绍了您如何对单元测试进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在MVCStoreFront应用程序上观看Rob Connerys的网络广播,我注意到他甚至在对最平凡的东西进行单元测试,例如:

I was watching Rob Connerys webcasts on the MVCStoreFront App, and I noticed he was unit testing even the most mundane things, things like:

public Decimal DiscountPrice
{
   get
   {
       return this.Price - this.Discount;
   }
}

将进行以下测试:

[TestMethod]
public void Test_DiscountPrice
{
    Product p = new Product();
    p.Price = 100;
    p.Discount = 20;
    Assert.IsEqual(p.DiscountPrice,80);
}

虽然我全都用于单元测试,但有时我想知道这种形式的测试优先开发确实是有益的,例如,在实际过程中,您的代码(业务请求,需求文档,体系结构文档)上方有3-4层,其中实际定义的业务规则(折扣价为价格-折扣)可以

While, I am all for unit testing, I sometimes wonder if this form of test first development is really beneficial, for example, in a real process, you have 3-4 layers above your code (Business Request, Requirements Document, Architecture Document), where the actual defined business rule (Discount Price is Price - Discount) could be misdefined.

如果是这种情况,您的单元测试对您毫无意义。

If that's the situation, your unit test means nothing to you.

此外,您的单元测试是另一个失败点:

Additionally, your unit test is another point of failure:

[TestMethod]
public void Test_DiscountPrice
{
    Product p = new Product();
    p.Price = 100;
    p.Discount = 20;
    Assert.IsEqual(p.DiscountPrice,90);
}

现在测试存在缺陷。显然,在一个简单的测试中,这没什么大不了的,但是说我们正在测试一个复杂的业务规则。我们在这里获得了什么?

Now the test is flawed. Obviously in a simple test, it's no big deal, but say we were testing a complicated business rule. What do we gain here?

在维护开发人员对其进行维护时,可以将应用程序的生命提前两年。现在企业改变了规则,测试再次中断,一些菜鸟开发人员随后错误地修复了测试……我们现在又遇到了一个失败点。

Fast forward two years into the application's life, when maintenance developers are maintaining it. Now the business changes its rule, and the test breaks again, some rookie developer then fixes the test incorrectly...we now have another point of failure.

我所看到的是更多可能的故障点,没有真正的收益,如果折扣价格错误,测试团队仍会发现问题,单元测试如何节省任何工作?

All I see is more possible points of failure, with no real beneficial return, if the discount price is wrong, the test team will still find the issue, how did unit testing save any work?

我在这里想念什么?请教我爱TDD,因为到目前为止我很难接受它。我也想要,因为我想保持进步,但这对我来说毫无意义。

What am I missing here? Please teach me to love TDD, as I'm having a hard time accepting it as useful so far. I want too, because I want to stay progressive, but it just doesn't make sense to me.

编辑:几个人不断提到测试有助于实施规范。根据我的经验,规范也常常是错误的,但是也许我注定要在一个规范由不应该编写规范的人编写的组织中工作。

A couple people keep mentioned that testing helps enforce the spec. It has been my experience that the spec has been wrong as well, more often than not, but maybe I'm doomed to work in an organization where the specs are written by people who shouldn't be writing specs.

推荐答案

首先,测试就像安全性一样-您永远不能百分百确定自己已经掌握了它,但是每一层都增加了信心和框架为了更轻松地解决仍然存在的问题。

First, testing is like security -- you can never be 100% sure you've got it, but each layer adds more confidence and a framework for more easily fixing the problems that remain.

第二,您可以将测试分为子例程,然后可以对它们进行测试。当您有20个类似的测试时,进行(已测试的)子程序意味着您的主要测试是对该子程序进行20次简单的调用,而这很可能是正确的。

Second, you can break tests into subroutines which themselves can then be tested. When you have 20 similar tests, making a (tested) subroutine means your main test is 20 simple invocations of the subroutine which is much more likely to be correct.

第三,有人会认为 TDD 解决了此问题。也就是说,如果您只编写了20个测试并且通过了测试,则您不会完全确定他们实际上在测试任何东西。但是,如果您最初编写的每个测试失败,然后又进行了修复,那么您将更有信心,它确实可以测试您的代码。恕我直言,这来回花费的时间多于其应有的时间,但这是一个尝试解决您的问题的过程。

Third, some would argue that TDD addresses this concern. That is, if you just write 20 tests and they pass, you're not completely confident that they are actually testing anything. But if each test you wrote initially failed, and then you fixed it, then you're much more confident that it's really testing your code. IMHO this back-and-forth takes more time than it's worth, but it is a process that tries to address your concern.

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

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