简体   繁体   English

我是否应该为A类编写测试,如果它属于B类

[英]Should I write tests for class A if it's covered from class B

I'd like to get some opinion about testing methodology. 我想对测试方法有一些看法。

Lets assume we have class A and B. Class B uses functionality of class A. The B class is fully tested and thus some test coverage is also applied indirectly for class A. 让我们假设我们有A类和B类.B类使用A类的功能.B类经过全面测试,因此一些测试覆盖也间接应用于A类。

Should I write full tests directly for class A? 我应该直接为A级写完整的测试吗? Or should I test only not tested A class functionality? 或者我应该只测试未测试的A类功能?

I am asking because maybe in the future there will be possibility that the B class will be removed or modified in the way that it might not use the same functionality from A class so it might leave some methods untested. 我在问,因为将来可能会删除或修改B类,因为它可能不会使用A类中的相同功能,因此可能会留下一些未经测试的方法。 What would you do? 你会怎么做?

Yes, you should fully test A . 是的,你应该全面测试A

  1. B might change at some point down the road, so just because it uses A now doesn't mean that it always will. B可能会在某个时刻发生变化,所以仅仅因为它现在使用A并不意味着它总会如此。

  2. B may not use all of the functionality of A which means that you're not testing all of your code. B可能不会使用A所有功能,这意味着您没有测试所有代码。

CLASSES != UNITS CLASSES!= UNITS

If you practice a good TDD, you will understand easily what is behind. 如果你练习一个好的TDD,你会很容易理解背后的东西。


IMO, you should test B 's behavior without based on the fact that A would already be tested. IMO,你应该测试B的行为,而不是基于A已经被测试的事实。

Actually, there are three cases: 实际上,有三种情况:

A and B BELONGING TO THE SAME LAYER: AB属于同一层:

  • If A is created through refactor cycle (extract class) of B (happens often while practicing a good TDD), then A should TOTALLY be left untested! 如果A是通过B重构循环(提取类)创建的(通常在练习良好的TDD时发生),那么A应该完全没有经过测试! No need to test it at all! 根本不需要测试它!
    Indeed, structure of code (in this case, separation of classes/SRP) should be independent of Unit concept; 实际上,代码结构(在这种情况下,类/ SRP的分离)应该独立于单元概念; B and A in this case belonging to the same unit. 在这种情况下, BA属于同一单元。

  • If A existed BEFORE B , B should not be based on this fact, and B 's whole behavior should be tested. 如果A存在于B 之前B不应该基于这个事实, B的整个行为应该被测试。

A and B NOT BELONGING TO THE SAME LAYER (distinct boundaries for instance): AB不属于同一层(例如明显的边界 ):

  • If B is a GUI class, and A a business class, then A should be doubled/mocked when testing B , and also A should have a full test dedicated for it. 如果B是GUI类,而A是业务类,那么在测试BA应该加倍/模拟,并且A应该有专门的测试。
    Indeed, domain architecture should not be mingled with behavior/feature notion. 实际上,域架构不应与behavior/feature概念混合在一起。

To understand why, read this recent article of Uncle Bob dealing with this concept: 要理解原因,请阅读Bob最近关于这个概念的文章:

http://blog.8thlight.com/uncle-bob/2014/01/27/TheChickenOrTheRoad.html?utm_source=hootsuite&utm_campaign=hootsuite http://blog.8thlight.com/uncle-bob/2014/01/27/TheChickenOrTheRoad.html?utm_source=hootsuite&utm_campaign=hootsuite

Excerpt of it: 摘录:

It is a common misconception that the design of the tests must mirror the design of the production code. 一种常见的误解是,测试的设计必须反映生产代码的设计。 TDD does not require, as the Author suggests, "that every unit in your system is paired with a well-designed [...] unit test." 正如作者所说,TDD并不要求“系统中的每个单元都与精心设计的单元测试配对。” Indeed, that's one of the reasons that many of us have stopped calling them "unit" tests. 实际上,这是我们许多人停止称他们为“单位”测试的原因之一。

Note: TDD doesn't care about "future", in the contrary, it helps you to write as much code as you need, no more. 注意:TDD并不关心“未来”,相反,它可以帮助您编写尽可能多的代码,而不是更多。 Therefore you should not worry about this: 所以你不应该担心这个:

in the future there will be possibility that the B class will be removed or modified 将来可能会删除或修改B类

If you wrote good tests (I prefer the word "specs"), such removal would be detected immediately. 如果你写了好的测试(我更喜欢“specs”这个词),那么就会立即检测到这种删除。

肯定写A级的完整测试。你在这里回答了你自己的问题:

(...)maybe in the future there will be possibility that the B class will be removed or modified in the way that it might not use the same functionality from A class so it might leave some methods untested.

The general thought behind unittesting is that each unit is comprised of a Unit of Work. 单元测试背后的一般思想是每个单元由一个工作单元组成。 This can be as small as a method or as big as several methods working together. 这可以像方法一样小,也可以像几种方法一样大。

You have already covered the scenario where B is dependent on A but from your story we can assume A will also be used separately. 您已经介绍了B依赖于A的场景,但是从您的故事中我们可以假设A也将单独使用。 Therefore A should also be tested since it is a separate Unit of Work. 因此,A也应该进行测试,因为它是一个单独的工作单元。

B should only use A through an Interface IA B应该只通过接口IA使用A.

Test B through it's public interface and A through it's public interface. 测试B通过它的公共接口和A通过它的公共接口。

Both should be tested. 两者都应该进行测试。

I think your answer is right there: 我想你的答案就在那里:
in the future there will be possibility that the B class will be removed or modified
I think that's a strong case for a testsuite for A. 我认为对于A的测试套件来说这是一个很好的例子。

You should first test A, then B. If you get a test failure on B, you won't have enough diagnostic to know whether it originated in B's code or in A's. 您应首先测试A, 然后测试B.如果您在B上测试失败,您将无法获得足够的诊断信息来了解它是源自B代码还是A代码。

Unit tests are not just about "pass/fail", they are very much about diagnosing the problem . 单元测试不只是“通过/失败”,它们非常关乎诊断问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM