单元测试哲学 [英] unit testing philosophy

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

问题描述

我有一个食谱"方法,我正在尝试使用 TDD 编写它.它基本上会调用不同的方法,偶尔会根据这些方法的结果做出决定:

I have a "recipe" method which i am trying to write using TDD. It basically calls out to different methods and occasionally makes decisions based on results of these methods:

  public void HandleNewData(Data data)
  {
     var existingDataStore = dataProvider.Find(data.ID);
     if (data == null)
        return;

     UpdateDataStore(existingDataStore, data, CurrentDateTime);

     NotifyReceivedData(data);

     if (!dataValidator.Validate(data))
        return;

     //... more operations similar to above
  }

我的下意识反应是开始编写测试用例,我验证 HandleNewData 调用上面看到的方法并传递预期参数,并在方法调用失败的情况下返回.但这对我来说有点像在编写这样的测试时需要投入大量时间,而实际上几乎没有任何好处.

My knee jerk reaction would be to start writing test cases where I verify that HandleNewData calls the methods seen above passing the expected arguments and that it returns in those cases where the method call fails. But this feels to me kind of like this is a huge investment in time to code such a test up with little to no actual benefit.

那么编写这样一个测试的真正好处是什么?还是真的不值得打扰?

So what is the real benefit of writing such a test? Or is it really not worth the bother?

这似乎只是对代码本身的过度规范,并且每当该代码必须调用另一种方法或决定不再调用当前方法之一时都会导致维护问题.

It seems like it is just an over-specification of code it self, and will lead to maintenance problems whenever that code has to call another method or decide to not call one of the current methods anymore.

推荐答案

TDD 并不意味着为已经存在的代码编写单元测试(尽管有时在改进遗留代码时可能有必要).

TDD does not mean writing unit tests for code that already exists (although sometimes it may be necessary when improving legacy code).

您可能听说过红色、绿色、重构"这个词.这是我们在进行 TDD 时采用的方法.以下是测试驱动开发的三个定律,它们更进一步......

You've probably heard the term "Red, Green, Refactor". This is the approach we take when doing TDD. Here are the three laws of Test-Driven Development, which take that a little further...

  1. 您不能编写生产代码直到你写了一个失败的单元测试.
  2. 您编写的单元测试不能超过足以失败的程度,并且不编译是失败的.
  3. 您编写的生产代码不能超过足以通过当前未通过测试.

采用这种方法的好处是,您最终会获得非常接近 100% 的单元测试覆盖率,并且您知道您的代码完全按照规定工作.

The benefits of taking this approach is that you end up with very close to 100% unit-test coverage and you know that your code works exactly as specified.

它会减少维护问题,因为只要有人对您的代码进行更改并运行测试,他们就会知道他们是否破坏了任何东西.

It will reduce maintenance problems because as soon as somebody makes a change to your code and runs the tests, they will know if they have broken anything.

在这种情况下,在为 HandleNewData() 添加任何方法之前,我会逐渐为从 HandleNewData() 调用的方法添加单元测试.

In this case, I would incrementally add unit tests for the methods being called from HandleNewData() before adding any for HandleNewData().

向遗留代码添加单元测试很困难,但可行且非常值得付出努力.如果您还没有,我真的建议您阅读 Working Effectively with LegacyMichael Feathers 的代码.我发现将单元测试添加到已有 25 年历史的代码库中非常有用.

Adding unit tests to legacy code is tough, but doable and very much worth the effort. If you haven't yet, I really recommend reading Working Effectively with Legacy Code by Michael Feathers. I've found it invaluable when adding unit tests to a 25-year-old code-base.

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

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