我应该测试接口和实现它的所有对象吗

发布于 2024-10-05 16:20:43 字数 97 浏览 9 评论 0原文

你好 假设我有一个接口 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

偏闹i 2024-10-12 16:20:44

由于您的接口不应该有任何具体的实现,因此您不需要测试它,因为根据定义没有任何可测试的内容。测试应该针对接口的具体实现。

如果您发现自己处于需要部分实现接口的情况,您可以按照我的做法。例如,假设我有一个项目的界面。我将其称为 IItem 并拥有所有接口。然后我声明一个 Item,它是通用代码接口的部分实现,然后声明 ItemAItemB 等,用于 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 an Item which is the partial implementation of the interface for common code and then ItemA, ItemB, etc. for the specialisations of Item.

很快妥协 2024-10-12 16:20:44

我读了你所有的帖子,我认为这个解决方案效果最好。

Interface A
{
   String A1();
   String A2();
}

public class B:A
{
   String A1(){return "A1"}
   String A2(){return "A2"}
}

public class testB
{
   public void B_Can_Return_A1()
   {
      A b=new B();
      Assert.True(b.A1=="A1")
   }
}

但是,如果您要从具体实现仍然依赖的接口中删除一个方法,那么您肯定不应该删除该接口的那部分吗?

这是事实,但仍应在测试(即测试)中强制执行。接口(应该)在开发中发挥重要作用,而变化可能会带来巨大的问题。如果一个对象实现了一个接口,我认为这就是应该如何测试它或类似的东西。

请对此发表评论。

I read all your posts I I think this solution works best.

Interface A
{
   String A1();
   String A2();
}

public class B:A
{
   String A1(){return "A1"}
   String A2(){return "A2"}
}

public class testB
{
   public void B_Can_Return_A1()
   {
      A b=new B();
      Assert.True(b.A1=="A1")
   }
}

But if you are removing a method from an interface that the concrete implementations still rely on surely you shouldn't be removing that part of the interface?

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.

゛时过境迁 2024-10-12 16:20:43

一般来说,测试应该涉及所有(可执行)代码行。如果您正在实现一个接口,那么它会变得更加容易,因为您可以编写形成接口“契约”的测试代码,并且现在测试适用于该接口的所有实现者。

这确保了所有实施者的一致性。如果您遇到实现者行为不同的情况(例如 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.

柳若烟 2024-10-12 16:20:43

根据我的经验,您只需测试具体的类及其与接口的交互。

也就是说,如果您有实现 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.

多像笑话 2024-10-12 16:20:43

是的,您的测试目标应该是获得 100% 的代码覆盖率

yes, you should aim to get 100% code coverage with your testing

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文