为什么要使用测试驱动开发? [英] Why should I use Test Driven Development?

查看:139
本文介绍了为什么要使用测试驱动开发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • Why should I practice Test Driven Development and how should I start?

对于不了解测试驱动开发的开发人员,通过采用TDD可以解决什么问题?

For a developer that doesn't know about Test-Driven Development, what problem(s) will be solved by adopting TDD?

让我们假设开发人员已经(ab)使用了单元测试框架。

Let's assume that the developer already (ab)uses a unit testing framework.

推荐答案

以下是TDD可以帮助开发人员/团队的三个原因:

Here are three reasons that TDD might help a developer/team:


  • 更好地了解您要编写的内容

  • 实施更好的编写测试策略

  • 加快开发速度

首先编写测试的原因之一是在编写实际代码之前对其有更好的理解。对我来说,这是测试驱动开发的主要好处。当您首先编写测试用例时,您会更审慎地考虑极端情况。这样,在编写代码时就可以轻松解决它们,并确保它们准确无误。

One reason to write the tests first is to have a better understanding of the actual code before you write it. To me, this is the main benefit of test driven development. When you write the test cases first, you think more critically about the corner cases. It's then easier to address them when you write the code and ensure that they're accurate.

另一个原因是实际上要强制编写测试。通常,当人们在没有TDD的情况下进行单元测试时,他们会建立一个测试框架,编写一些新代码,然后退出。他们认为代码已经可以正常工作了,那么为什么要编写测试呢?很简单,不会破损,对不对?但是,现在您已经失去了进行单元测试的优势(完全不同的讨论)。先写它们,然后它们就已经存在了。

Another reason is to actually enforce writing the tests. Often when people do unit-testing without the TDD, they have a testing framework set up, write some new code, and then quit. They think that the code already works just fine, so why write tests? It's simple enough that it won't break, right? But now you've lost the advantages of doing unit-tests in the first place (completely different discussion). Write them first, and they're already there.

首先编写这些测试可能意味着您不需要在调试环境中启动程序(缓慢—特别是对于较大的项目),以测试一些小问题是否可行。当然,在提交更改之前不这样做没有任何借口。

Writing these tests first could mean that you don't need to launch the program in a debugging environment (slow — especially for larger projects) to test if a few small things work. Of course there's no excuse for not doing so before committing changes.

说服自己或其他人首先编写测试可能很困难。您可能会很幸运,让他们同时写两个书,这可能同样有益。

Convincing yourself or other people to write the tests first may be difficult. You may have better luck getting them to write both at the same time which may be just as beneficial.

这篇关于为什么要使用测试驱动开发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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