assert

NUnit conflict with Debug.Assert

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 19:51:25
问题 I'm using NUnit to write unit tests for a libary a colleague of mine has written. His library contains a lot of Debug.Asserts which triggers on invalid input. When I'm writing the unit tests and give invalid input to his library, his Debug.Assert throws up a message box complaining about the bad input. I feel that it's a good thing that his library throws up an assert on invalid input, but at the same time I want the unit tests to cover bad input. But when I do this, the message box shows up

单元测试 - 探索java web 单元测试的正确姿势

南笙酒味 提交于 2019-12-04 19:11:20
单元测试 - 探索java web 单元测试的正确姿势 一丶起因   笔者一直听闻TDD,自动化测试等高大上的技术名词, 向往其中的便利之处, 但一直求而不得, 只因项目中有各种依赖的存在,其中最大的依赖便是数据库. java web 项目大部分都是写sql语句, 不依赖数据库, 便测试不了sql语句的正确性, 但依赖数据库又有种种不变之处. 除此之外, 还有种种类与类之间的依赖关系,很不方便. 遗憾的是, 网上各种文章参差不齐, 笔者所参与的项目很少甚至没有单元测试, 修改代码, 如履薄冰. 在苦思不得其解之际, 向优秀开源项目mybatis求取经验, 终获得一些答案. 二丶实践思路   mybatis使用单元测试的方式是使用内存数据库做单元测试,单元测试前,先根据配置以及数据库脚本,初始化内存数据库,然后再使用内存数据库测试.所以,笔者也是采用这种思路.   除此之外, 还有各种类与类之间依赖关系, 笔者依据mybatis以及spring选择使用mockito框架mock解决   所以选用的工具有 hsql内存数据库, mockito mock工具, junit单元测试工具, spring-boot-test子项目 三丶实施测试   1. 在pom.xml添加hsql 以及mockito <dependency> <groupId>org.hsqldb</groupId>

pytest -- 测试的参数化

随声附和 提交于 2019-12-04 18:21:38
目录 1. @pytest.mark.parametrize 标记 1.1. empty_parameter_set_mark 选项 1.2. 多个标记组合 1.3. 标记测试模块 2. pytest_generate_tests 钩子方法 往期索引: https://www.cnblogs.com/luizyao/p/11771740.html 在实际工作中,测试用例可能需要支持多种场景,我们可以把和场景强相关的部分抽象成参数,通过对参数的赋值来驱动用例的执行; 参数化的行为表现在不同的层级上: fixture 的参数化:参考 4、fixtures:明确的、模块化的和可扩展的 -- fixture 的参数化 ; 测试用例的参数化:使用 @pytest.mark.parametrize 可以在测试用例、测试类甚至测试模块中标记多个参数或 fixture 的组合; 另外,我们也可以通过 pytest_generate_tests 这个钩子方法自定义参数化的方案; 1. @pytest.mark.parametrize 标记 @pytest.mark.parametrize 的根本作用是在 收集 测试用例的过程中,通过对 指定参数 的赋值来新增被标记对象的 调用(执行) ; 首先,我们来看一下它在源码中的定义: # _pytest/python.py def parametrize

What is the role of asserts in C++ programs that have unit tests?

只愿长相守 提交于 2019-12-04 16:52:53
问题 I've been adding unit tests to some legacy C++ code, and I've run into many scenarios where an assert inside a function will get tripped during a unit test run. A common idiom that I've run across is functions that take pointer arguments and immediately assert if the argument is NULL. I could easily get around this by disabling asserts when I'm unit testing. But I'm starting to wonder if unit tests are supposed to alleviate the need for runtime asserts. Is this a correct assessment? Are unit

How to install a DebugBreak handler?

让人想犯罪 __ 提交于 2019-12-04 16:04:43
We are setting up Appveyor for our Visual Studio solution, which produces a C++ library. A few of our tests [dumb] fuzz C++ objects to ensure they don't do something unexpected. Under debug builds it causes an assert to fire (and in release builds it just throws). We use a custom assert to avoid Posix behavior of crashing a program being debugged. It is shown below. It appears Appveyor or the Operating System kills the program if an assert fires and a debugger is not attached: We want to install a DebugBreak handler if a debugger is not present. This should confirm its the OS doing the killing

How to use a C assert to make the code more secure?

半世苍凉 提交于 2019-12-04 10:41:00
Reading misc. tutorials related to SDL development I've found two different examples, doing the same thing, but in a different manner. I was wondering which of the two are you considering to be correct, judging from the perspective of code "security" and maintainability . In the first example the programmer isn't using assert at all, but the code looks OK (at least for my eye): int main(){ SDL_Surface *screen; /** Initialize SDL */ if(SDL_Init(SDL_INIT_VIDEO)!=0){ fprintf(stderr,"Unable to initialize SDL: %s",SDL_GetError()); } atexit(SDL_Quit); /** Sets video mode */ screen=SDL_SetVideoMode

Assert() - what is it good for ?

我是研究僧i 提交于 2019-12-04 09:41:52
I don't understand the purpose of assert() . My lecturer says that the purpose of assert is to find bugs . For example : double divide(int a , int b ) { assert (0 != b); return a/b; } Does the above assert justified ? I think that the answer is yes , because if my program doesn't supposed to work with 0 (the number zero) , but somehow a zero does find its way into the b variable , then something is wrong with the code . Am I correct ? Can you show me some examples for a justified assert() ? Regards assert is used to validate things that should always be true if the program is correct. Whether

Any reason to use a run-time assert instead of compile-time assert?

﹥>﹥吖頭↗ 提交于 2019-12-04 09:11:40
While reviewing Visual C++ codebase I found a following strange thing. A run-time assert (which is check the condition and throw an exception if the condition is violated ) was used in a case when the condition could be evaluated at compile time: assert( sizeof( SomeType ) == sizeof( SomeOtherType ) ); clearly the compiler will evaluate the condition and replace the code that will effectively be either assert( true ); which does nothing or assert( false ); which throws an exception every time control passes through that line. IMO a compile-time assert should have be used instead for the

Using assertion in the Linux kernel

◇◆丶佛笑我妖孽 提交于 2019-12-04 08:51:43
问题 I have a question about assert() in Linux: can I use it in the kernel? If no, what techniques do you usually use if, for example I don't want to enter NULL pointer? 回答1: The corresponding kernel macros are BUG_ON and WARN_ON . The former is for when you want to make the kernel panic and bring the system down (i.e., unrecoverable error). The latter is for when you want to log something to the kernel log (viewable via dmesg ). As @Michael says, in the kernel, you need to validate anything that

Assertions in Fortran

寵の児 提交于 2019-12-04 08:41:44
Does Fortran have a standard function/keyword equivalent for C assert ? I could not find assert mentioned in Fortran2003 standard I have. I have found few ways how to use pre-processor, but in this answer it is suggested to write own assertions. Is it possible to create such user function/subroutine without using pre-processor? I expect that these assertions are disabled for release builds. To my knowledge, there is no such statement or function/subroutine in Standard Fortran. But - as you have said - you can use your own subroutines/function and/or OOP in Fortran to realize this goal. See