在进行单元测试时,断言是否多余?

编程入门 行业动态 更新时间:2024-10-26 10:33:42
本文介绍了在进行单元测试时,断言是否多余?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

更多推荐

在进行单元测试时,断言是否多余?

本文发布于:2023-11-03 20:00:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555955.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:断言   多余   单元测试

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!