assert

Exception Vs Assertion

本小妞迷上赌 提交于 2019-11-26 06:16:40
问题 What is the difference between Java exception handling and using assert conditions? It\'s known that Assert is of two types. But when should we use assert keyword? 回答1: Use assertions for internal logic checks within your code, and normal exceptions for error conditions outside your immediate code's control. Don't forget that assertions can be turned on and off - if you care about things like argument validation, that should be explicit using exceptions. (You could, however, choose to perform

How to check if an object is a list or tuple (but not string)?

给你一囗甜甜゛ 提交于 2019-11-26 06:10:22
问题 This is what I normally do in order to ascertain that the input is a list / tuple - but not a str . Because many times I stumbled upon bugs where a function passes a str object by mistake, and the target function does for x in lst assuming that lst is actually a list or tuple . assert isinstance(lst, (list, tuple)) My question is: is there a better way of achieving this? 回答1: In python 2 only (not python 3): assert not isinstance(lst, basestring) Is actually what you want, otherwise you'll

Can I use assert on Android devices?

亡梦爱人 提交于 2019-11-26 03:49:03
问题 I want to use the Assert keyword in my android apps to destroy my app in some cases on the emulator, or my device during testing. Is this possible? It seems that the emulator just ignores my asserts. 回答1: The API provides the JUnit Assert. You can do import static junit.framework.Assert.*; now you can use all the functions like assertTrue, assertEquals, assertNull that are provided in the junit framework. Be careful not to import the Junit4 framework through eclipse, that would be the org

Best practice for Python assert

▼魔方 西西 提交于 2019-11-26 03:18:59
问题 Is there a performance or code maintenance issue with using assert as part of the standard code instead of using it just for debugging purposes? Is assert x >= 0, \'x is less than zero\' better or worse than if x < 0: raise Exception, \'x is less than zero\' Also, is there any way to set a business rule like if x < 0 raise error that is always checked without the try/except/finally so, if at anytime throughout the code x is less than 0 an error is raised, like if you set assert x < 0 at the

What does the “assert” keyword do? [duplicate]

淺唱寂寞╮ 提交于 2019-11-26 03:03:45
问题 This question already has an answer here: What does the Java assert keyword do, and when should it be used? 18 answers What does assert do? For example in the function: private static int charAt(String s, int d) { assert d >= 0 && d <= s.length(); if (d == s.length()) return -1; return s.charAt(d); } 回答1: If you launch your program with -enableassertions (or -ea for short) then this statement assert cond; is equivalent to if (!cond) throw new AssertionError(); If you launch your program

When should assertions stay in production code?

元气小坏坏 提交于 2019-11-26 02:40:46
问题 There\'s a discussion going on over at comp.lang.c++.moderated about whether or not assertions, which in C++ only exist in debug builds by default, should be kept in production code or not. Obviously, each project is unique, so my question here is not so much whether assertions should be kept, but in which cases this is recommendable/not a good idea. By assertion, I mean: A run-time check that tests a condition which, when false, reveals a bug in the software. A mechanism by which the program

What is the use of “assert” in Python?

落花浮王杯 提交于 2019-11-26 01:55:02
问题 I have been reading some source code and in several places I have seen the usage of assert . What does it mean exactly? What is its usage? 回答1: The assert statement exists in almost every programming language. It helps detect problems early in your program, where the cause is clear, rather than later as a side-effect of some other operation. When you do... assert condition ... you're telling the program to test that condition, and immediately trigger an error if the condition is false. In

使用pytest————持续更新中

大兔子大兔子 提交于 2019-11-26 01:43:25
pytest 1,最简单的示例 import pytest def test_case_01(): print("执行test01") assert 1 # 断言成功 def test_case_02(): print("执行test02") assert 0 # 断言失败 if __name__ == '__main__': pytest.main(['test_01.py']) 运行结果如下: pytest 1,最简单的示例 import pytest def test_case_01(): print("执行test01") assert 1 # 断言成功 def test_case_02(): print("执行test02") assert 0 # 断言失败 if __name__ == '__main__': pytest.main(['test_01.py']) 运行结果如下: 如图所示: 在执行完成之后先会显示test01.py,后面跟着.F 其中.代表执行成功,F代表执行失败,并且在下方会展示错误的提示 2,pytest使用步骤: 1,导入pytest 2,编写测试用例 一,无需在测试类下编写测试用例,可以直接编写测试函数 二,测试函数名必须包含test_ 开头,或者_test结尾; 3,在pytest框架下执行测试用例 在py文件内执行测试用例: pytest

How to do a JUnit assert on a message in a logger

佐手、 提交于 2019-11-26 01:06:14
问题 I have some code-under-test that calls on a Java logger to report its status. In the JUnit test code, I would like to verify that the correct log entry was made in this logger. Something along the following lines: methodUnderTest(bool x){ if(x) logger.info(\"x happened\") } @Test tester(){ // perhaps setup a logger first. methodUnderTest(true); assertXXXXXX(loggedLevel(),Level.INFO); } I suppose that this could be done with a specially adapted logger (or handler, or formatter), but I would

Static assert in C

天大地大妈咪最大 提交于 2019-11-26 00:28:19
问题 What\'s the best way to achieve compile time static asserts in C (not C++), with particular emphasis on GCC? 回答1: C11 standard adds the _Static_assert keyword. This is implemented since gcc-4.6: _Static_assert (0, "assert1"); /* { dg-error "static assertion failed: \"assert1\"" } */ The first slot needs to be an integral constant expression. The second slot is a constant string literal which can be long ( _Static_assert(0, L"assertion of doom!") ). I should note that this is also implemented