测试所有东西是一个好方法吗?

huangapple go评论83阅读模式
英文:

Is testing everything a good approach?

问题

我有一个名为Generator的类,其中有一个名为buildDocument的方法,在这个方法的内部有以下代码:

service.getDocument() + EXTENSION;

其中,private final static String EXTENSION = ".txt";

方法getDocument()已经在ServiceTest类中进行了测试。我是否应该为buildDocument方法添加测试,以测试这个文件扩展名的添加?这似乎有点像在测试:

private boolean method(){
   return true;
}

这样的方法是否应该被测试?

英文:

I have class Generator with a method buildDocument and inside this method there is:
service.getDocument() + EXTENSION; where private final static String EXTENSION = ".txt";

method getDocument() is already tested in ServiceTest class. Should I add test for buildDocument method to test this file extension adding? It seems a bit like testing:

private boolean method(){
   return true;
}

should such methods be tested?

答案1

得分: 3

请注意,你的方法是私有的。任何私有的内容都应视为实现细节。因此,你最好不要为仅仅该部分编写单元测试。

意思是:如果你已经在某个地方有一个测试方法,确保你的文档位于正确的位置,并且具有期望的名称(包括扩展名),那么就可以“足够好”。

但请注意,这也有一些“个人风格”的成分。

英文:

Note that your method is private. Anything that is private should be considered an implementation detail. Thus you rather avoid writing a unit test for just that part.

Meaning: if you have test method somewhere already that your document sits in the right place, with the expected name (which would include the extension), then you are "good enough".

But note this also a bit of "personal style".

huangapple
  • 本文由 发表于 2020年8月26日 16:29:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63593611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定