英文:
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".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论