TDD 和测试优先开发(或测试优先编程)之间有区别吗?

发布于 2024-07-09 20:12:19 字数 80 浏览 9 评论 0原文

这两个想法对我来说听起来非常相似,但可能存在细微的差异,或者是完全相同的事情,但以不同的方式解释。 TDD 和测试优先开发/编程之间有什么关系?

Both ideas sound very similar to me, but there might be subtle differences or the exact same thing, explained in different ways. What is the relationship between TDD and Test First Development/Programming?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

沉鱼一梦 2024-07-16 20:12:19

驱动因素是什么是有区别的。

您是否对类(或系统 - 当然,这可以在不同的规模上发生)应该是什么样子有一个模糊的想法,然后想出测试来赋予它实际的形状? 这就是TDD。

您是否确切地知道该类的公共 API 应该是什么,并且只在实现之前编写测试? 这就是测试优先的开发。

我的风格往往是两者的混合。 有时,在编写任何测试之前,API 应该是什么是显而易见的 - 在其他情况下,可测试性确实驱动了设计。

换句话说,TDD 从“我想问什么问题?”开始。 而非 TDD(无论是否先测试)以“我想给出什么答案?”开始。

There's a difference in terms of what the driving factor is.

Do you have a vague idea of what the class (or system - this can happen at different scales, of course) should look like, then think up tests which give it the actual shape? That's TDD.

Do you know exactly what the public API of the class should be, and just write the tests before the implementation? That's test-first development.

My style tends to be a mixture of the two. Sometimes it's obvious what the API should be before writing any tests - in other cases testability really drives the design.

To put it another way, TDD starts with "What questions do I want to ask?" whereas non-TDD (whether test first or not) starts with "What answer do I want to give?"

淡看悲欢离合 2024-07-16 20:12:19

它们基本上是描述同一事物的不同名称 - 实际上是五个名称,因为最后一个 D 可以代表设计和开发。

测试优先是最初使用的术语,特别是在极限编程的背景下,用于测试代码重构循环。 测试驱动开发这个名称后来被提出并很快被采用,以强调 TFD 始终是一种设计策略而不是测试策略。

显然,今天有些人对这两个术语有不同的含义,但这不是它们存在背后的意图,我不会依赖它成为常识(因为它不是)。 事实上,我宁愿看到 TFD 一词被弃用。

They are basically different names describing the same thing - well, in fact five names, as the last D can stand for Design as well as for Development.

Test First was the term used originally, especially in the context of Extreme Programming, for the test-code-refactor cycle. The name Test Driven Development has been proposed - and quickly adopted - later, to stress the fact that TFD is - and always has been - more of a design strategy than a testing strategy.

Obviously today some people have different connotations for those two terms, but that wasn't the intent behind their existance, and I wouldn't rely on it being common knowledge (because it's not). In fact, I'd rather see the term TFD as being deprecated.

妄想挽回 2024-07-16 20:12:19

有很多类似的术语,例如测试优先编程、测试优先开发、测试驱动开发甚至测试驱动设计。 有必要澄清以下几点:

1. 测试优先编程(TFP)

术语测试优先编程是一种编程最佳实践。 Kent Beck 在他的《极限编程解释》一书中重新引入了它(如果不是创造的话):“在编程之前编写单元测试并保持所有测试始终运行”。 因此,当谈论测试优先编程时,我们谈论的是由将要编写代码来满足这些测试的开发人员编写自动化单元测试。 单元测试堆积并构建一个可以定期运行的自动回归测试套件。

2. 测试驱动开发(TDD)

测试驱动开发(TDD)是 Kent Beck 在他的《Test Driven Development by Examples》一书中引入的一种方法的名称。 这是一个软件开发过程,不仅仅是在代码之前编写测试。 整本书试图通过模式、工作流程、文化等来解释它。 它的一个重要方面是强调重构。

有些人使用术语“测试优先开发”、“测试驱动设计”或“测试驱动编程”……有一点是肯定的:行之有效的方法是测试驱动开发,而编程技术是“测试优先编程”。 其余的要么一般指在代码之前编写测试的想法,要么错误地指测试驱动开发或测试优先编程。

The are a lot of similar terms like test-first programming, test-first development, test-driven development or even test-driven design. It is important to clarify a few points:

1. Test First Programming (TFP)

The term test-first programming is a programming best practice. It was reintroduced (if not coined) by Kent Beck in his book “Extreme Programming Explained”: “Write unit tests before programming and keep all of the tests running at all times”. So, when talking about test-first programming we are talking about writing automated unit tests by the very developer that is going to write the code to satisfy those tests. The unit tests pile up and build an automated regression test suite that could be run periodically.

2. Test Driven Development (TDD)

Test-driven development (TDD) is the name of a methodology introduced by Kent Beck in his book "Test Driven Development by Example". It is a software development process, it is not only about writing tests before code. The whole book is trying to explain it by patterns, workflows, culture and so forth. One important aspect of it is the emphasis on refactoring.

Some people use the terms test-first development, test-driven design, or test-driven programming and ... One thing is for sure: the well established methodology is test-driven development and the programming technique is test-first programming. The rest are either generally referring to the idea of writing tests before code or mistakenly referring to test-driven development or test-first programming.

灼痛 2024-07-16 20:12:19

TDD = TFD + 重构。

当您执行 TFD 时,您会应用一些重构来使代码更加通用和健壮。

TDD = TFD + Refactoring.

When you do TFD , you apply some refactoring to make code more generic and robust.

奈何桥上唱咆哮 2024-07-16 20:12:19

历史上正确的是:测试优先编程和测试驱动开发

在 XP(极限编程)的背景下意味着同一件事,但名称有所改进,这是使测试优先编程和测试驱动开发流行起来的软件开发过程,测试-在认识到首先编写测试对软件体系结构和软件系统设计具有巨大的积极影响之后,“首次编程”更名为“测试驱动开发”,然后更名为“测试驱动设计”。

这种对架构和设计的影响或多或少是令人惊讶的同义词的结果:

  • 测试、解耦
  • 、可
  • 重用、可
  • 独立部署
  • 、可独立开发、独立合理、

软件实体只有在解耦时才能轻松地重用、测试、独立部署、独立开发或轻松单独推理。 在实际实现之前编写测试是确保持续解耦的几乎万无一失的方法。

除了其他积极影响之外,这种对软件设计和架构的影响变得如此重要,以至于创建者发现值得将其从“测试优先编程”重命名为“测试驱动开发”。

测试驱动开发这个名称还有助于在接受和正确理解方面更好地营销该方法,因为测试驱动开发这个名称比测试优先编程更好地强调方法的整体方面。

历史上不正确但有用

虽然历史上不正确,但我发现以下区别非常有用:

测试优先编程…………

是在被测代码之前编写对被测代码的测试的任何方法。

测试驱动开发…………

是测试优先编程的一个特定子集,遵循 Robert C. Martin 描述的测试驱动开发 3 条定律:

  1. 在首先编写失败的单元测试之前,您无法编写任何生产代码。
  2. 编写的单元测试不能超过足以失败的程度,不编译就是失败。
  3. 您编写的生产代码不能多于足以通过当前失败的单元测试的代码。
    — Robert C. Martin,测试驱动开发的三大法则

遵循这三个规则将使您进入所谓的红-绿-重构循环。
1. 你编写了一个失败的测试。
2. 你让它通过了。
3. 现在已经通过了,您可以在编写下一个失败的测试之前进行无情的重构。

请注意,安全重构需要测试。 重构意味着改变源代码的结构而不改变重要的行为。 但我们怎么知道我们没有无意中改变了重大行为呢? 什么定义了重大行为? 这是测试有用的众多事情之一。

顺便说一句,如果您的测试妨碍了重构,则说明您的测试太低级、耦合太紧密,并且可能使用了太多模拟。

极限编程

  • 持续集成中其他有趣的重命名 -> 持续交付-> 持续部署; 严格来说,它们的含义不同,但是,按照 XP 的精神,它从一开始就意味着持续部署,当人们追随潮流时,人们意识到集成被过于字面意思理解了,人们在完成之前就停下来了。
  • 持续重构-> 持续设计改进; 重构本身并不是达到目的的手段,而是遵循更高的目的。
  • 每周 40 小时 -> 可持续的步伐(都市传奇:在法国软件开发人员的抗议后进行了更名。)

Historically Correct: Test-First Programming and Test-Driven Development mean Same Thing with an Improved Name

In the context of XP (Extreme Programming), which is the software development process that made Test-First Programming and Test-Driven Development popular, Test-First Programming was renamed to Test-Driven Development and then Test-Driven Design following the realization that writing tests first has a tremendously positive effect on the software architecture and design of a software system.

This influence on architecture and design is a consequence of more or less surprising synonyms:

  • Testable
  • Decoupled
  • Reusable
  • Independently Deployable
  • Independently Developable
  • Independently Reasonable

Software entities can only be easily reused, tested, deployed independently, developed independently, or easily reasoned separately if they are decoupled. Writing tests before the actual implementation is an almost bullet-proof method to ensure continuous decoupling.

This influence on software design and architecture became so important besides the other positive effects that the creators found it worthwhile renaming it from Test-First Programming to Test-Driven Development.

The name Test-Driven Development also helps marketing the method better in terms of acceptance as well as proper understanding because the name Test-Driven Development emphasises better on the holistic aspects of the method than Test-First Programming.

Not Historically Correct but Useful

While historically not correct, I find the following distinction very useful:

Test-First Programming…

…is any method in which tests for the code under test are written before the code under test.

Test-Driven Development…

…is a specific subset of Test-First Programming that follows the 3 Laws of Test-Driven Development as described by Robert C. Martin:

  1. You can't write any production code until you have first written a failing unit test.
  2. You can't write more of a unit test than is sufficient to fail, and not compiling is failing.
  3. You can't write more production code than is sufficient to pass the currently failing unit test.
    — Robert C. Martin, The Three Laws of Test-Driven Development

Following these three rules puts you in what is called the Red-Green-Refactor cycle.
1. You write a failing test.
2. You make it pass.
3. Now that it passes, you can refactor mercilessly before writing the next failing test.

Note that refactoring safely requires tests. Refactoring means changing the structure of source code without changing the significant behavior. But how do we know that we haven't accidentally altered significant behavior? What defines significant behavior? That's one of the many things for which tests are useful.

BTW if your tests get in the way of refactoring, your tests are too low-level, too tightly coupled, and maybe you've used too much mocking.

Other interesting renames in Extreme Programming

  • Continuous Integration -> Continuous Delivery -> Continuous Deployment; Strictly speaking they mean different things, however, in the spirit of XP, it meant Continuous Deployment from the start, and when people jumped on the bandwagon, it was realized that integration was taken too literally and people stopped before they are done.
  • Continuous Refactoring -> Continuous Design Improvement; Refactoring is not a means to an end in itself, but follows a higher purpose.
  • 40 hours week -> Sustainable Pace (Urban legend: This rename happened after protests by French software developers.)
烟沫凡尘 2024-07-16 20:12:19

测试驱动开发:举例中,作者 Kent Beck 明确指出“测试优先”(TF) 是规则。 所以TF是统治TDD的原则。 后面的就是过程了。

In Test Driven Development: By Example, the author, Kent Beck, clearly states that "test first" (TF) is the rule. So TF is the principle that rules TDD. The later one is the process.

烟若柳尘 2024-07-16 20:12:19

TDD(测试驱动开发)是测试优先开发/编程,虽然我看到和听说过 TDD 过去意味着创建持久的、可重复的单元测试(甚至在代码之后),但实际上它意味着测试是在他们正在测试的代码之前编写的。

TDD (Test Driven Development) is Test First Development/Programming although I have seen and heard TDD used to mean creating persistent, repeatable unit tests (even after the code), but really it implies the tests are written before the code they are testing.

伤感在游骋 2024-07-16 20:12:19

它们是完全相同的东西。 两者都提到先编写测试,然后编写将通过测试的代码

They are exactly the same thing. Both reference writing tests first, then writing the code that will pass the test

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文