你怎么跟你的单元测试测试? [英] What do you test with your unit tests?

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

问题描述

TDD的东西,这似乎是每个人的嘴唇这些天,我已经尝试了一些我自己,但我不认为我得到的想法。我正在就如何编写单元测试的抓地力,但我不明白到底是什么我的单元测试应该测试。

TDD is something that seems to be on everybody's lips these days, and I have tried some on my own but I don't think I'm getting the idea. I am getting a grip on how to write a unit test, but I don't understand exactly what my unit tests should test.

  1. 如果我有一个返回的数据列表的操作方法,我应该怎么检查?只有视图名称是正确的,或者我应该核实数据呢?
  2. 如果我要测试数据为好,不会我可以写相同的code两次?有什么用测试数据,如果我用同样的方法来检索数据,我比较?
  3. 我应该测试方法添加/编辑我的数据吗?我如何验证一个记录已被添加/修改/删除,以正确的方式?

我知道这是相当多的大问题,但我还没有成为阅读文章任何明智的在互联网上,因为他们似乎都与被关注的如何的测试,而不是与什么的。

I know it's quite a lot of large questions, but I haven't become any wiser from reading articles on the internet, as they all seem to be concerned with how to test, and not with what.

作为一个例子 - 我有(或者,我会写)一GuestbookController,与观看方式,添加,编辑和删除帖子。我需要做什么测试?我该怎么做呢?

As an example - I have (or, am going to write) a GuestbookController, with methods for viewing, adding, editing and removing posts. What do I need to test? How do I do it?

推荐答案

这混乱似乎是相当普遍的。 UT是所有关于code覆盖。 TDD涉及的功能的。他们的没有的同样的事情[乔尔对不起!]

Unit Testing (UT) != Test Driven Design (TDD)

This confusion seems to be fairly common. UT is all about code coverage. TDD is concerned with features. They are not the same thing [sorry Joel!]

使用UT,你写什么code,你想,然后回去和测试每个功能(甚至是一些琐碎的)。

With UT, you write whatever code you want to, then go back and test every single function (even some of the trivial ones).

使用TDD,您选择的下一个功能和先写测试该功能的。只写测试该功能,以及测试覆盖率的无关的。你写测试第一,迫使接口的决定作出了前面。然后你写的code通过测试(铭记可以运转的最简单的事情)。然后你重构根据你所学到的code。然后,你(办理入住手续后,presumably并重新运行的所有的单元测试),继续到下一个要素。

With TDD, you select the next feature and write the test for that feature first. Write only the test for that feature, and test coverage is irrelevant. You write the test first to force interface decisions to be made up front. Then you write the code to pass the test (bearing in mind the 'simplest thing that can possibly work'). Then you refactor the code based on what you've learned. Then you go on to the next feature (presumably after check-in and re-running all unit tests).

如果需要,开发使用TDD再回去完全覆盖与UT工具。如果您要创建一个类库或其它的API供开发人员使用,更多的测试覆盖率越好; - )

If desired, develop using TDD then go back and complete coverage with UT tools. If you're creating a class library or other API for developers to use, the more test coverage the better ;-)

如果你只是写一个应用程序做了五个具体的事情,TDD本身就应该足够了。

If you're just writing an app to do five specific things, TDD alone should be sufficient.

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

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