如何设计code可测性 [英] How to design code for testability

查看:108
本文介绍了如何设计code可测性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在考虑使用TDD和实施适当的测试(只是刚刚开始了解它是如何更好地使你的生活)在我的项目,我在未来创建。因此,在过去的几天我一直漂浮在SO想了解如何可测性设计的应用程序,但我似乎仍然有一些想法挣扎。

I have been looking at using TDD and implementing proper testing (only just started to learn how much better it makes your life) in any of my projects that I create in the future. So for the last couple of days I have been floating around on SO trying to learn about how to design your application for testability, but I still seem to be struggling with some of the ideas.

我已经读了很多,你应该程序对接口,而不是。主要的问题我有是,有多少接口,你应该创建?如果您有一个给你想要的一切测试?还是我读这错了吗?

I have read a lot that you should program against interfaces rather than classes. The main problem I'm having is, how many interfaces should you create? Should you have one for everything you want to test? or am I reading this wrong?

另一件事是使用大量依赖注入,所以你可以嘲笑你的注入,而不是用真实的事物的部分。它是否正确?还是我的方式在这里下车吗?

Another thing is use lots of dependency injection, so you can mock the parts that you are injecting rather than use the real things. Is this correct? or am I way off here too?

推荐答案

我觉得你有正确的想法,但我认为你正在成为一个更大的交易这比它。如果你开始做TDD,你的第一反应可能是是这样的吗?。再后来,你应该有希望说'啊哈'!

I think you have the right idea, but I think you are making this into a bigger deal than it is. If you start doing TDD, your first reaction will probably be 'is this it?'. And then later, you should hopefully say 'aha'!

最主要的是,你得到NUnit的,学习教程,并确保你写一个测试你所做的一切你写的实施之前。你可能会跳过编写测试的属性访问器,但任何事情需要任何计算,你应该​​先写一个测试。

The main thing is that you get nUnit, learn the tutorial, and then make sure you write a test for everything you do before you write the implementation. You might skip over writing tests for property accessors, but anything that requires any calculations, you should write a test first.

所以,pretend你正在测试一个计算器的添加(INT 1,INT 2),你想到的第一件事就是,'我怎样才能打破这种'。我的想法,事情可能出错是:负数,零和溢出。诀窍是想象每一个错误谁去创建Add()方法可能会使,然后编写一个测试反对它的人。所以,我可能会写:

So, pretend you're testing a calculator's Add(int 1, int 2), the first thing you think is, 'how can I break this'. My ideas where things could go wrong are: negative numbers, zeros, and overflows. The trick is to imagine every mistake the person who is going to create the Add() method might make, and then write a test against it. So, I might write:

Assert.AreEqual(5, calc.Add(2, 3), "Adding positives not as expected");
Assert.AreEqual(-5, calc.Add(-2, -3), "Adding negatives not as expected");
Assert.AreEqual(-2, calc.Add(-3, 2), "Adding one positive and one negative not as expected");

// your framework might provide a cleaner way of doing this:
try {
  int result = calc.Add(Int32.Max, 5);
  Assert.Fail("Expected overflow error. Received: " + result);
} catch(Exception e) {
  // This should be a more specific error that I'm not looking up
}

所以,你可以看到,我一直试图做的是找出如何的Add()方法可能无法正常工作,然后进行测试反对。我也找了有趣的极端情况,并明确定义了我期待的行为。然后现在我可以自由地熄灭,code中的Add()方法。

So, as you can see, what I've tried to do is figure out how the Add() method might not work, and then test against it. I've also looked for interesting corner cases and explicitly defined the behaviour that I'm expecting. And then now I'm free to go off and code the Add() method.

现在,而不是所有的伟大,你知道你的Add()方法将是坚如磐石的,当你开始创建复杂的数学函数与您SQRT()方法,随着您添加结合你的罪()方法()方法。

Now, while that's not all that great, you know that your Add() method will be rock-solid for when you start creating complex math functions that combine your Sin() method with your Sqrt() method along with your Add() method.

这,是好还是坏,是测试驱动开发。不要太挂在接口或依赖注入现在。这可以稍后,如果你需要它。

That, for better or worse, is Test Driven Development. Don't get too hung up on the interfaces or dependency injection for now. That can come later, if you need it.

这篇关于如何设计code可测性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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