当基类被淘汰时,单元测试如何改变? [英] How do unit tests change when a base class is driven out?

查看:30
本文介绍了当基类被淘汰时,单元测试如何改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对这个问题的部分跟进.

我不确定问这个问题的最佳方式,所以我会尝试一个短篇小说来设置场景:

I'm not sure the best way to ask this, so I'll try a short story to set the scene:

曾几何时,有一个类A",它有一个单元测试类ATests",负责通过公共接口测试其行为.他们一起幸福地生活了一段时间,然后发生了变化,出现了B"班,结果发现它与A"班有很多共同点,因此引入了一个基类.

Once upon a time, there was a class ‘A’, which had a unit test class ‘ATests’ responsible for testing its behaviour through the public interface. They lived happily together for a while and then a change happened, and class ‘B’ came along, which as it turned out had a lot in common with class ‘A’, so a base class was introduced.

A 类的测试已经涵盖了基类的公共行为.那么,问题是接下来会发生什么?

The public behaviour of the base class is already covered by the tests for class A. So, the question is what happens next?

• B 类是否需要对公共(基类行为)进行测试?行为似乎是 B 的一部分,因此应该对其进行测试,但是这些测试是否应该与 A 类的测试共享?对于基类?如果是这样,最好的分享方式是什么?

• Does class B need to have tests for the common (base class behaviour)? It seems like the behaviour is a part of B, so it should be tested, but should these tests be shared with those for class A? For the base class? If so, what’s the best way to share?

• 新基类是否需要单元测试,或者基类是否可以通过其子类的测试进行测试?基类是否是抽象的重要吗?

• Does the new base class need unit tests, or is it ok for base classes to be tested through the tests of their children? Does it matter if the base class is abstract?

• 是否足以确保 A 类 &B 从基类派生并信任"基类的单元测试来测试公共行为(因此测试不需要在子类中复制)?A & 的测试B 只需要测试他们是新的/改变的行为吗?

• Is it enough to ensure that classes A & B derive from the base class and ‘trust’ the unit tests for the base class to test the common behaviour (so the tests don’t need to be replicated in the child classes)? The tests for A & B only need to test they’re new/changed behaviour?

• 我是否完全错误地采用了每个真实课程大约一个单元测试课程的方法?

• Am I following totally the wrong approach having approximately one unit test class per real class?

我在不同的时间采取了不同的观点,不同的方法会对重构代码的能力、编写测试所花费的时间等产生相当大的影响.人们认为哪些方法最有效?

I’ve taken different views at different times and the different approaches can have quite an impact on the ability to refactor the code, time taken to write tests etc. What approaches have people found works best?

推荐答案

您可能会查看代码覆盖工具;他们可以告诉你你是否真的在测试所有的代码.就个人而言,如果我有一个覆盖基类行为的测试并且我没有覆盖它,我不会再次测试它.目标是让代码更改(可能)只破坏一个测试.

You might look at code coverage tools; they can show you if you're actually testing all of the code. Personally if I have a test covering the base class behavior and I"m not overriding that, I won't test it again. The goal is to have a code change (potentially) break only one test.

不要觉得每个真正的课程都需要坚持一个单元测试课程.每个独特的设置装置一个单元测试类是一种方式,然后是整个 BDD 学校...

Don't feel the need to stick to one unit test class per real class. One unit test class per unique setup fixture is one way, and then there's the whole BDD school...

这篇关于当基类被淘汰时,单元测试如何改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆