在进行单元测试时,断言是否多余? [英] Are assertions redundant when you have unit tests?

查看:99
本文介绍了在进行单元测试时,断言是否多余?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还不习惯编写单元测试,我想在一个完整的小工具框架上进行此操作(使其更安全地使用).这样一来,我肯定会比现在学到更多有关单元测试的知识.

I'm not used yet to write unit tests and I want to do this on a full framework of little tools (making it more secure to use). That way I'll certainly learn more about unit tests than what I learnt until now.

但是,我真的习惯于在所有看到可以确定的上下文(在最终版本中将其删除)的地方都系统地添加断言.通常作为函数实现的前提条件,每次我检索必须正确的信息时(例如著名示例的C/C ++指针有效性).

However I'm really used to add assertions systematically everywhere I see there is a context to be sure about (that are removed in the final release). Mostly as preconditions in functions implementations and each time I retrieve informations that have to be correct (like C/C++ pointers validity for a famous example).

现在我要问的是:在进行单元测试时,断言是否多余?因为当您测试一些代码的行为时,它看起来很多余;但与此同时,执行上下文也不相同.

Now I'm asking : are assertions redundant when you have unit tests? Because it looks redundant as you're testing the behaviour of a bit of code; but in the same time it's not the same execution context.

我两个都做吗?

推荐答案

检查前提条件的断言可以帮助检测和定位集成错误.也就是说,尽管单元测试表明在正确使用(调用)某个方法时正确运行,但是检查前提条件的断言可以检测到该方法的不正确使用(调用).使用断言会导致错误的代码快速失败,从而有助于调试.

Assertions that check preconditions can help detect and locate integration bugs. That is, whereas unit tests demonstrate that a method operates correctly when it is used (called) correctly, assertions that check preconditions can detect incorrect uses (calls) to the method. Using assertions causes faulty code to fail fast, which assists debugging.

这篇关于在进行单元测试时,断言是否多余?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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