本文介绍了我应该使用 TDD 来设计我的客户端和我的库吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你创建了一个库来做一些计算,你使用 TDD 来构建它没有问题,对吧?现在您必须实现实际使用该库的代码.例如,客户端可以是 Java Servlet 或 CLI 程序.

Assuming you create a library to do some calculations, you use TDD to build it without problems, right? Now you must implement the code that actually uses this library. The client could be a Java Servlet or a CLI Program, for example.

此客户端代码是否必须使用 TDD 概念构建?您为这些客户编写测试?TDD 仅与库的设计有关,还是我需要担心客户端代码?

Must this client code be built using the TDD concept? You write tests for these clients? Is TDD solely about design of the library, or do I need to worry about the client code?

推荐答案

在我提供一些关于 TDD 的信息后,我会在下面更直接地回答你的问题.

I will answer your question more directly below after I give some info on TDD.

记住 TDD 实际上是一个设计过程,它恰好涉及单元测试并遵循红绿重构循环.这是一个设计过程,因为在每个红绿重构迭代中,您首先为不存在的代码编写测试.您正在设计.

It helps to remember that TDD is actually a design process that happens to involve unit testing and following the Red-Green-Refactor cycle. It's a design process because within each Red-Green-Refactor iteration, you write the test first for code that doesn't exist. You're designing as you're going.

TDD 的第一个优点是您的代码设计可以保证是可测试的.可测试的代码往往具有松耦合和高内聚的特点.松耦合和高内聚很重要,因为它们使代码在需求变化时易于更改.TDD 的第二个优点是,在您完成系统实现之后,您碰巧有一个巨大的回归套件来捕获任何错误和假设的变化.因此,TDD 因其创建的设计而使您的代码易于更改,并且由于它创建的测试工具,它使您的代码可以安全地更改.

The first beauty of TDD is that the design of your code is guaranteed to be testable. Testable code tends to have loose coupling and high cohesion. Loose coupling and high cohesion are important because they make the code easy to change when requirements change. The second beauty of TDD is that after you're done implementing your system, you happen to have a huge regression suite to catch any bugs and changes in assumptions. Thus, TDD makes your code easy to change because of the design it creates and it makes your code safe to change because of the test harness it creates.

现在,实际回答您的问题.因为 TDD 是一个设计过程而不是一个测试过程,所以在尽可能多的代码中使用 TDD 是有帮助的,因为无论你在哪里使用它,你的设计都会从 TDD 过程中受益.事实上,我更喜欢从客户端开始实现功能,因为它可以帮助我首先关注客户场景(请参阅此链接关于 Presenter First 了解更多信息).通常情况下,如果我使用 Model-View-Controller/Model-View-Presenter/Model-View-ViewModel 来实现某些东西,我将开始使用带有控制器/Presenter/ViewModel 的 TDD,不会进行测试视图,因为它将是一个没有逻辑的瘦包装器(并且使用自动化测试验证视图的实现和维护成本很高),并且会在有意义的情况下将内容移动到模型中.

Now, to actually answer your question. Because TDD is a design process and not a testing process, it helps to use TDD in as much of your code as is reasonable because wherever you use it, your design will benefit from the TDD process. In fact, I prefer to start implementing features starting with the client because it helps me focus on customer scenarios first (see this link about Presenter First for more info). Typically, the way this works out if I'm implemeting something using Model-View-Controller/Model-View-Presenter/Model-View-ViewModel, I'll start using TDD with the Controller/Presenter/ViewModel, won't test the view because it will be a thin wrapper with no logic (and it's expensive to implement and maintain to verifying views with automated tests), and will move things into the model as it makes sense.

这篇关于我应该使用 TDD 来设计我的客户端和我的库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 08:42