assert

异常处理

 ̄綄美尐妖づ 提交于 2019-11-29 10:20:51
异常处理 一丶什么异常 1.语法错误 2.逻辑错误 二丶Exception s1 = 'hello' try: int(s1) except Exception as e: print(e) 二丶try...except总结 把错误处理和真正的工作分开来 代码更易组织,更清晰,复杂的工作任务更容易实现; 毫无疑问,更安全了,不至于由于一些小的疏忽而使程序意外崩溃了; 三丶抛出异常raise try: raise TypeError('抛出异常,类型错误') except Exception as e: print(e) 四丶断言 assert 1 == 1 try: assert 1 == 2 except Exception as e: print(e) 来源: https://www.cnblogs.com/shiqizz/p/11514973.html

python:pytest中的setup和teardown

情到浓时终转凉″ 提交于 2019-11-29 10:14:37
原文: https://www.cnblogs.com/peiminer/p/9376352.html   之前我写的unittest的setup和teardown,还有setupClass和teardownClass(需要配合@classmethod装饰器一起使用),接下来就介绍pytest的类似于这类的固件。 (1.setup_function、teardown_function 2.setup_class、teardown_class 3.setup_method、teardown_method 4.setup_module、teardown_module) setup/teardown和unittest里面的setup/teardown是一样的功能,这里setup_method和teardown_method的功能和setup/teardown功能是一样的,优先级是先执行setup_method,在执行setup。一般二者用其中一个即可 , 就不详细介绍了。setup_class和teardown_class等价于unittest里面的setupClass和teardownClass 一、函数级的(setup_function、teardown_function)只对函数用例生效,而且不在类中使用 #!/usr/bin/env/python # -*-coding:utf

pytest的一些实用插件实践

戏子无情 提交于 2019-11-29 09:51:11
1.多重校验 pytest-assume 简单的校验assert,虽然可以写多个assert def test_add1(self): assert add(2,3)==5 assert add(1,3)==3 assert add(2,5)==7 由于第二个断言失败,那么下面的断言就不会执行。 所以如果需要多个断言,都执行就需要第三方插件 pytest-assume 安装命令: pip install pytest-assume 示例: def test_add2(self): pytest.assume(add(1,2)==3) pytest.assume(add(1,4)==3) pytest.assume(add(2,2)==4) 这边即使第二个断言失败了,第三个断言还是会继续执行。 2.设定执行顺序 pytest-ordering 对于一些上下文依赖的,有时候可能需要设定一些特定执行顺序,pytest的ordering插件,就很好的解决了这个问题 安装命令 pip install pytest-ordering 示例脚本如下: def test_order1(): print ("first test") assert True def test_order2(): print ("second test") assert True 没有加上ordering,执行顺序是

C++ assert: the precedence of the expression in an assert macro

北城以北 提交于 2019-11-29 09:11:37
In C++: assert( std::is_same<int , int>::value ); // does not compile assert( (std::is_same<int , int>::value) ); // compiles Can anyone explain why? The comma is being treated as a argument separator for the macro, but parenthesis in your second case protect the arguments. We can see this by going to the draft C++ standard section 16.3 Macro replacement which says ( emphasis mine ): The sequence of preprocessing tokens bounded by the outside-most matching parentheses forms the list of arguments for the function-like macro. The individual arguments within the list are separated by comma

Assert.AreEqual does not use my .Equals overrides on an IEnumerable implementation

倾然丶 夕夏残阳落幕 提交于 2019-11-29 09:11:10
I have a PagedModel class which implements IEnumerable to just return the ModelData, ignoring the paging data. I have also overridden Equals and GetHashCode to allow comparing two PagedModel objects by their ModelData, PageNumber, and TotalPages, and PageSize. Here's the problem Dim p1 As New PagedModel() With { .PageNumber = 1, .PageSize = 10, .TotalPages = 10, .ModelData = GetModelData() } Dim p2 As New PagedModel() With { .PageNumber = 1, .PageSize = 10, .TotalPages = 10, .ModelData = GetModelData() } p1.Equals(p2) =====> True Assert.AreEqual(p1, p2) ======> False! It looks like NUnit is

QUnit的使用

不羁岁月 提交于 2019-11-29 08:38:53
最近我准备构建一个自己的轻量级的库,但是我遇到了一些问题,最大的问题就是比如写着写着,我怕后面写的东西会覆盖掉前面的一些功能,所以我只能不停的测试,于是我想到了用一些测试库,试着安装传说中的mocha啥的,on my god,我的破电脑完全不给力装不上去,最后找了这一款超级轻量级的语法简单的测试库,而且还是牛逼的John Resig发明的. ``` QUnit.test( "isFunction", function( assert ) assert.expect( 2 );//指明回调里的断言的次数 function calc( x, operation ) { return operation( x ); } var result = calc( 2, function( x ) { assert.ok( true, "calc() calls operation function" ); return x * x; }); assert.equal( result, 4, "2 square equals 4" ); }); ``` 统计事件执行次数 ``` QUnit.test( "a test", function( assert ) { assert.expect( 1 ); var $body = $( "body" ); $body.on( "click",

Disabling python's assert() without -0 flag

天大地大妈咪最大 提交于 2019-11-29 08:23:19
问题 I'm running a python script from inside a different software (it provides a python interface to manipulate its data structures). I'm optimizing my code for speed and would like to see what impact on performance my asserts have. I'm unable to use python -O . What other options do I have, to programatically disable all asserts in python code? The variable __debug__ (which is cleared by -O flag) cannot be assigned to :( 回答1: The docs say, The value for the built-in variable [ __debug__ ] is

ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);

浪尽此生 提交于 2019-11-29 06:17:14
  MFC中ASSERT作为断言语句,括号内内容为TRUE,继续执行;为FALSE终止执行。之后取得当前窗口的系统菜单,在这个菜单中添加字符串资源IDS_ABOUTBOX和菜单资源IDM_ABOUTBOX。是MFC中一段通用代码。    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);   ASSERT(IDM_ABOUTBOX < 0xF000); 来源: https://www.cnblogs.com/wjq13752525588/p/11458637.html

When should I use Apache Commons' Validate.isTrue, and when should I just use the 'assert' keyword?

邮差的信 提交于 2019-11-29 06:14:50
When should I use Apache Commons' Validate.isTrue, and when should I just use the 'assert' keyword? Assertions can be turned off (in fact, they normally are), so they are not useful for validating user input, for example. avandeursen Validate.isTrue and 'assert' serve completely different purposes. assert Java's assert statements are typically used for documenting (by means of assertions) under what circumstances methods can be invoked, and what their callers can expect to be true afterward. The assertions can optionally be checked at run time, resulting in an AssertionError exception if they