如何对子类进行单元测试
单元测试子类的最佳方法是什么?假设有一个我已经为其编写了测试的基类,并且有一些子类在公共和/或受保护的方法中重写了父类的一些行为。
我的子类的测试类是否应该扩展(并在适当的情况下重写测试方法)我的基类的测试类,以便应用所有基类测试?否则,我希望有重复的测试代码。
What is the best way to unit test subclasses? Let's assume there's a base class for which I've already written tests and there are some number of subclasses that override some of the parent's behavior in public and/or protected methods.
Should the test class for my subclasses extend (and override test methods where appropriate) the test class for my base class so that all of the base class tests are applied? Otherwise, I would expect to have repeated test code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据里氏替换原则,子类的实例应该表现出相同的属性作为基类,因此通过(全部?)相同的单元测试。
我将为每个子类运行[也许不是所有,所有相关的]基类测试。这可以通过测试助手来实现。
是的,对测试类进行子类化可能是避免单元测试重复的好方法。查看 Testcase 超类 模式。
According to the Liskov substitution principle, instances of the subclasses, should exhibit the same properties as the base class, and, thus, pass (all ?) the same unit tests.
I would run [perhaps not all, all that are relevant] the base class tests for each subclass. This can be achieved with a test helper.
Yes, subclassing the test class could be a good way to avoid duplication in the unit tests. Have a look at the Testcase superclass pattern.
如果没有示例,很难看出这一点,但我会在一组测试中测试基类,然后为子类创建新的测试,并只测试不同的行为。
It is difficult to see without an example, but I would test the base class in one set of tests, and then create new tests for the subclasses and just test the behaviour that differs.