assert

What are the advantages or difference in “assert False” and “self.assertFalse”

不羁岁月 提交于 2019-11-27 05:32:52
问题 I am writing tests and I have heard some people saying to use self.assertFalse rather than assert False. Why is this and are there any advantages to be had? 回答1: assert False throws an exception without useful logging information. The test had an error. self.assertFalse() throws a test failure exception with test failure information like a message and a test name. There's a difference between an error -- test could not even run -- and a failure -- test code worked but produced the wrong

Some (anti-)patterns on using assert (Java, and others)

大兔子大兔子 提交于 2019-11-27 02:47:15
问题 Finally, I have a question to ask on Stack Overflow! :-) The main target is for Java but I believe it is mostly language agnostic: if you don't have native assert, you can always simulate it. I work for a company selling a suite of softwares written in Java. The code is old, dating back to Java 1.3 at least, and at some places, it shows... That's a large code base, some 2 millions of lines, so we can't refactor it all at once. Recently, we switched the latest versions from Java 1.4 syntax and

iOS退出APP

99封情书 提交于 2019-11-27 02:24:35
强制退出有四种: exit(); abort(); assert(); 主动制造一个崩溃; exit() 1.附加了关闭打开文件与返回状态码给执行环境,并调用你用atexit注册的返回函数; 2.警告:不要使用exit函数,调用exit会让用户感觉程序崩溃了,不会有按Home键返回时的平滑过渡和动画效果; 3.另外,使用exit可能会丢失数据,因为调用exit并不会调用-applicationWillTerminate:方法和UIApplicationDelegate方法; - (void)exitApplication { [UIView beginAnimations:@"exitApplication" context:nil]; [UIView setAnimationDuration:0.5]; [UIView setAnimationDelegate:self]; [UIView setAnimationTransition:UIViewAnimationCurveEaseOut forView:self.window cache:NO]; [UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)]; self.window.bounds = CGRectMake

NodeJS测试框架Mocha

蓝咒 提交于 2019-11-27 02:21:00
一、测试框架:Mocha 相对比较新的测试框架,可以用来做 TDD(Test-Driven Development)或 BDD(Behavior Driven Development)风格的测试 说明: TDD:解决的是代码级的验证,但是测试代码与需求的符合问题解决得不是很好,非技术人员、客户看不懂代码,无法评审测试是否符合需求。 BDD:理念是使用自然语言来描述功能,而且强调的是使用例子来说明需求功能。 1、 全局变量泄露测试 Mocha 能够发现那种不小心创建出来的全局变量泄露,如果创建了全局变量,它会在测试时抛出错误。如果想禁用全局泄露检查,可以运行 mocha 命令时加上 --ignored-leaks 选项。 2、 用 Mocha 挂钩定义设置和清理逻辑 BDD 接口 beforeEach()、afterEach()、before()和 after()接受回调,可以用来定义设置和清理逻辑。 3、示例 const memdb = require('..'); describe('memdb', () => { describe('.saveSync(doc)', () => { it('should save the document', () => { }); }); }); 说明: describe 块:被称为测试套件,表示一组相关的测试,它是一个函数

How can I assert() without using abort()?

独自空忆成欢 提交于 2019-11-27 01:48:20
问题 If I use assert() and the assertion fails then assert() will call abort() , ending the running program abruptly. I can't afford that in my production code. Is there a way to assert in runtime yet be able to catch failed assertions so I have the chance to handle them gracefully? 回答1: Yes, as a matter of fact there is. You will need to write a custom assert function yourself, as C++'s assert() is exactly C's assert() , with the abort() "feature" bundled in. Fortunately, this is surprisingly

Is it good practice to use assert in Java?

笑着哭i 提交于 2019-11-27 01:47:19
问题 I know that the keyword assert exists in java. However I don't remember seeing code that uses it. Probably I am using exceptions and logging in places where I could have used it. Is it a good practice to use the assert keyword in java? EDIT : I know that assertions in general is a good practice. my question is, to be more accurate, if in java the BKM of assertion is using the assert keyword rather than using exception, logging and other techniques. 回答1: The main reason assertions are not used

assert vs. JUnit Assertions

♀尐吖头ヾ 提交于 2019-11-27 01:05:25
Today I saw a JUnit test case with a java assertion instead of the JUnit assertions—Are there significant advantages or disadvantages to prefer one over the other? Yishai In JUnit4 the exception (actually Error) thrown by a JUnit assert is the same as the error thrown by the java assert keyword (AssertionError), so it is exactly the same as assertTrue and other than the stack trace you couldn't tell the difference. That being said, asserts have to run with a special flag in the JVM, causing many tests to appear to pass just because someone forgot to configure the system with that flag when the

how to completely disable assertion

大憨熊 提交于 2019-11-27 00:44:16
I have my code full of call to assert(condition) . In the debug version I use g++ -g exploiting my assertion. With my surprise I can see assertion working also in my release version, the one compiled without -g option. How can I completely disable at compile time my assertion? Should I explicitly define NDEBUG in any build I produce despite they are debug,release or whatever any other? GWW You must #define NDEBUG (or use the flag -DNDEBUG with g++ ) this will disable assert as long as it's defined before the inclusion of the assert header file. Use #define NDEBUG 7.2 Diagnostics 1 The header

Assert keyword in Java

社会主义新天地 提交于 2019-11-26 23:59:14
问题 Do you use the assert keyword or throw some validation runtime exception? What benefits does it give to you or why do you think it's not worth it to use? 回答1: Assert will throw a runtime error (AssertionError) if its condition is false. Asserts give you a streamlined way of documenting, checking, and enforcing correctness criteria for your code. The benefits are a language-level hook for defining and manipulating these correctness conditions. To the extent that you wish to enable or disable

What is the “assert” function?

╄→гoц情女王★ 提交于 2019-11-26 23:14:43
I've been studying OpenCV tutorials and came across the assert function; what does it do? bdonlan assert will terminate the program (usually with a message quoting the assert statement) if its argument turns out to be false. It's commonly used during debugging to make the program fail more obviously if an unexpected condition occurs. For example: assert(length >= 0); // die if length is negative. You can also add a more informative message to be displayed if it fails like so: assert(length >= 0 && "Whoops, length can't possibly be negative! (didn't we just check 10 lines ago?) Tell jsmith");