首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TDD共享需求

TDD共享需求
EN

Stack Overflow用户
提问于 2010-12-30 00:00:19
回答 1查看 62关注 0票数 0

我正在将新需求开发成一段较旧的代码,并利用这个机会开始在一些单元测试中工作。遵循TDD和可能工作的最简单的事情。我遇到了一种情况,我无法决定什么是最佳实践。其中两个需求实际上非常相似,因此,随着代码的增长,有足够的机会进行重构。下面是具体的情况:

代码语言:javascript
复制
MethodOne()
{
  int errorCode = sectionOfDiverseCode();

//SharedCode
  string userMessage = localizeError(errorCode);
  displayUserMessage(userMessage);
}

MethodTwo()
{
  int errorCode = anotherSectionOfDiverseCode();
//SharedCode
  string userMessage = localizeError(errorCode);
  displayUserMessage(userMessage);
}


Currently I'm writing the same unit tests to fit the requirements that the string is localized correctly and also that it is displayed in the user interface. Would it better to isolate the localization and display methods and have one set of unit tests for them and then single test to confirm the two methods call these methods? Or is it better to write the localize and display tests for each method?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-12-30 00:05:04

我建议为localizeError设置一组测试,并为displayUserMessage设置另一组测试。似乎没有任何理由将两者联系在一起;本地化的错误可能会记录到日志中,并且您可能希望显示非错误消息,对吧?

一般而言,请尽量保持单元测试的规模。他们的想法是测试一小部分代码,这样你就可以尽可能快地隔离和修复问题。

编辑:请记住,TDD不是灵丹妙药。在这种情况下,关键点是单元测试适用于localizeErrordisplayUserMessage,但您所描述的需求驱动测试可能不太适合methodOnemethodTwo。根据您的组织,也许更高级别的“检查是否正确显示”测试在UAT中更好。关于“不要重复自己”的一般指导原则和所有其他良好的软件开发规则也适用于编写测试。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4555634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档