我应该测试接口和实现它的所有对象吗
你好 假设我有一个接口 A 和一个实现 A 的类 B。在我的测试类中,我创建了一个实现 A 的虚拟类,并且我“测试接口方法”,现在我的问题是我应该测试类 B“获取”的方法吗界面。
Hi
Assume I have an Interface A and a class B that implements A. Within my test class I create a dummy class that implements A and I "test the Interface methods" now my question is should I test the methods that class B "gets" from the interface.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于您的接口不应该有任何具体的实现,因此您不需要测试它,因为根据定义没有任何可测试的内容。测试应该针对接口的具体实现。
如果您发现自己处于需要部分实现接口的情况,您可以按照我的做法。例如,假设我有一个项目的界面。我将其称为
IItem
并拥有所有接口。然后我声明一个Item
,它是通用代码接口的部分实现,然后声明ItemA
、ItemB
等,用于ItemA
的专业化代码>项目。Since your interface shouldn't have any concrete implementation then you don't need to test it since there is nothing to test by definition. The testing should be for the concrete implementation of the interface.
If you find yourself in a situation where you need to have a partial implementaton of an interface you can do what I do. For instance, say I have a interface of an item. This I call
IItem
and has all the interface. Then I declare anItem
which is the partial implementation of the interface for common code and thenItemA
,ItemB
, etc. for the specialisations ofItem
.我读了你所有的帖子,我认为这个解决方案效果最好。
这是事实,但仍应在测试(即测试)中强制执行。接口(应该)在开发中发挥重要作用,而变化可能会带来巨大的问题。如果一个对象实现了一个接口,我认为这就是应该如何测试它或类似的东西。
请对此发表评论。
I read all your posts I I think this solution works best.
This is true but this should still be enforced in tests i.e. tested. interfaces (should) play a big role in development and changes may create huge problems down the line. If an object implements an interface I think this is how it should be tested or something similar.
Please comment on this.
一般来说,测试应该涉及所有(可执行)代码行。如果您正在实现一个接口,那么它会变得更加容易,因为您可以编写形成接口“契约”的测试代码,并且现在测试适用于该接口的所有实现者。
这确保了所有实施者的一致性。如果您遇到实现者行为不同的情况(例如 NullReferenceException 与 ArgumentNullException),您可以添加测试来指定哪个是“正确的”,哪个是错误的。这会减少后续的意外情况。
我什至可以说每个接口都应该附加一组测试来描述预期的行为。
当然,有一些特定于实现的事情只能在具体实现者上进行测试(例如“文件是否已写入?”与“记录是否已提交?”)。这些东西应该通过覆盖或 lambda 提供给接口的测试套件。
Generally testing should touch all (executable) lines of code. If you are implementing an interface it makes it that much easier, since you can code tests that form the "contract" of the interface and now the tests apply to all implementors of the interface.
This ensures consistency across all implementors. Should you encounter a situation where implementors behave differently (e.g. NullReferenceException vs. ArgumentNullException) you can add tests specifying which is "right" and which is wrong. This leads to less surprises down the road.
I might even go as far as saying that every interface should have a set of tests attached to describe the expected behaviour.
There are of course implementation specific things that can only be tested on the concrete implementor (e.g. "Was the file written?" vs. "Was the record comitted?"). These things should be provided through overriding or lambdas to the interface's test suite.
根据我的经验,您只需测试具体的类及其与接口的交互。
也就是说,如果您有实现 A 的具体类 B,您只需测试 B 及其与它引用的其他对象的交互。
In my experience, you just test concrete classes and their interaction with interfaces.
That is, if you have concrete class B that implements A, you just test B and its interaction with other objects it references.
是的,您的测试目标应该是获得 100% 的代码覆盖率
yes, you should aim to get 100% code coverage with your testing