开发者

TDD Shared Requirement

I'm developing new requirements into an older piece of code and using the opportunity to start to work in a few unit tests. Following TDD and the most simple thing that would possible work. I've run into a situation were I can't decide what is the best practice. Two of the requirements are actually very similar and as a result there was ample opportunity for refactoring as the code grew. Below is the situation:

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 u开发者_开发问答nit 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?


I'd suggest having a set of tests for localizeError, and another set for displayUserMessage. There doesn't seem to be any reason for the two to be tied together; localized errors might go into the log, and you might want to display non-error messages, right?

In general, try to keep unit tests as small as possible. The idea is that they test a small unit of code so you can isolate and fix problems as quickly as possible.

EDIT: Remember that TDD is not a silver bullet. In this case, the key point is that unit tests are appropriate for localizeError and displayUserMessage, but maybe the requirement-driven test you describe is not so appropriate for methodOne and methodTwo. Depending on your organisation, maybe the higher-level "check that it displays correctly" test is better in UAT. The general guidelines about "don't repeat yourself" and all those other good software development rules apply to writing tests, too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜