高级单元测试和模拟对象的价值 [英] The value of high level unit tests and mock objects

查看:63
本文介绍了高级单元测试和模拟对象的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始相信,需要大量使用模拟对象的单元测试高级,编写良好的代码几乎没有价值.我想知道这个断言是正确的,还是我遗漏了什么?

I am beginning to believe that unit testing high level, well-written code, which requires extensive use of mock objects, has little to no value. I am wondering if this assertion is correct, or am I missing something?

我的意思是高水平?这些是食物链顶部附近的类和功能.它们的输入和输出往往是用户输入和用户界面.他们的大部分工作包括接受用户输入并对较低级别的实体进行一系列调用.它们通常没有或几乎没有有意义的返回值.

What do I mean by high level? These are the classes and functions near the top of the food chain. Their input and output tends to be user input, and user interface. Most of their work consists of taking user input and making a series of calls to lower-level entities. They often have little or no meaningful return values.

写得好是什么意思?在这种情况下,我指的是与其依赖项分离的代码(使用接口和依赖注入),并且一行一行地保持一致抽象级别.没有棘手的算法,很少有条件.

What do I mean by well-written? In this case, I am referring to code that is decoupled from its dependencies (using interfaces and dependency injection), and line by line is at a consistent level of abstraction. There's no tricky algorithms, and few conditionals.

讨厌编写此类代码的单元测试.单元测试几乎完全由模拟对象设置组成.一行一行地读取单元测试,几乎像是实现的镜像.实际上,我通过查看实现来编写单元测试. 首先我断言这个模拟方法被调用,然后断言这个模拟方法被称为...",等等.我应该测试该方法的行为,而不是它在调用正确的方法顺序 >.另一件事:我发现这些测试对重构极其脆弱.如果测试是如此脆弱,以至于彻底崩溃,并且在重构被测代码时必须重写它,那么单元测试的主要好处之一就是没有丢失吗?

I hate writing unit tests for this kind of code. The unit tests consist almost entirely of mock object setup. Line by line, the unit test read almost like a mirror image of the implementation. In fact, I write the unit tests by looking at the implementation. "First I assert this mock method is called, then I assert this mock method is called...", etc. I should be testing the behavior of the method, not that it's calling the right sequence of methods. Another thing: I have found that these tests are extremely fragile to refactoring. If a test is so brittle it utterly shatters and must be rewritten when the code under test is refactored, then hasn't one of the major benefits of unit testing been lost?

我不希望这篇文章被标记为有争议,或者不是问题.因此,我将直接陈述我的问题:对我所描述的那种代码进行单元测试的正确方法是什么,还是可以理解并非所有内容都需要单元测试?

I don't want this post to be flagged as argumentative, or not a question. So I'll state my question directly: What is the correct way to unit test the kind of code I have described, or is it understood that not everything needs a unit test?

推荐答案

根据我的经验,相对于编写代码而言,代码的级别越低(琐碎的琐事),单元测试的价值就越高.随着您在食物链中的地位越来越高,测试变得越来越复杂和昂贵.

In my experience, the lower level your code is (short of being trivial), the more value unit tests are, relative to the effort required to write them. As you get higher up the food chain, tests become increasingly elaborate and more expensive.

单元测试至关重要,因为它们会告诉您重构期间何时中断某些内容.

Unit tests are critical because they tell you when you break something during refactoring.

高级测试有其自身的价值,但后来不再称为单元测试;它们被称为集成测试和验收测试.需要进行集成测试,因为它们可以告诉您不同软件组件如何协同工作.

Higher level tests have their own value, but then they are no longer called unit tests; they are called integration tests and acceptance tests. Integration tests are needed because they tell you how well the different software components work together.

验收测试是客户签署的内容.验收测试通常由其他人(而不是程序员)编写,以提供不同的观点.程序员倾向于为有效的内容编写测试,而测试人员则通过测试无效的内容来试图打破它.

Acceptance tests are what the customer signs off. Acceptance tests are typically written by other people (not the programmer) in order to provide a different perspective; programmers tend to write tests for what works, testers try to break it by testing what doesn't work.

模拟仅对单元测试有用.对于集成和验收测试,模拟是没有用的,因为它不使用实际的系统组件,例如数据库和通信基础结构.

Mocking is only useful for unit tests. For integration and acceptance tests, mocking is useless because it doesn't exercise the actual system components, such as the database and the communication infrastructure.

这篇关于高级单元测试和模拟对象的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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