assert

Proper way to assert type of variable in Python

╄→гoц情女王★ 提交于 2019-12-02 17:50:42
In using a function, I wish to ensure that the type of the variables are as expected. How to do it right? Here is an example fake function trying to do just this before going on with its role: def my_print(begin, text, end): """Print 'text' in UPPER between 'begin' and 'end' in lower """ for i in (begin, text, end): assert isinstance(i, str), "Input variables should be strings" out = begin.lower() + text.upper() + end.lower() print out def test(): """Put your test cases here! """ assert my_print("asdf", "fssfpoie", "fsodf") assert not my_print("fasdf", 33, "adfas") print "All tests passed"

Making Python's `assert` throw an exception that I choose

自作多情 提交于 2019-12-02 17:26:06
Can I make assert throw an exception that I choose instead of AssertionError ? UPDATE: I'll explain my motivation: Up to now, I've had assertion-style tests that raised my own exceptions; For example, when you created a Node object with certain arguments, it would check if the arguments were good for creating a node, and if not it would raise NodeError . But I know that Python has a -o mode in which asserts are skipped, which I would like to have available because it would make my program faster. But I would still like to have my own exceptions. That's why I want to use assert with my own

What is the meaning of an assumption in scala compared to an assertion?

蹲街弑〆低调 提交于 2019-12-02 16:59:40
Scala seems to define 3 kinds of assertions: assert , require and assume . As far as I can understand, the difference (compared to a generic assertion) of require is that it is specifically meant for checking inputs (arguments, incoming messages etc). And what's the meaning of assume then? If you look at the code in Predef.scala you'll see that all three do very similar job: def assert(assertion: Boolean) { if (!assertion) throw new java.lang.AssertionError("assertion failed") } def assume(assumption: Boolean) { if (!assumption) throw new java.lang.AssertionError("assumption failed") } def

NSAssert vs. assert: Which do you use, and when?

半腔热情 提交于 2019-12-02 16:57:48
I've read two really interesting pieces of advice, recently: In the comments to this StackOverflow answer , @Mike Weller says to leave your asserts on in production code... what's the performance hit, really? Is there any reason NOT to leave them in? In Vincent Gable's blog , he states that you should prefer assert over NSAssert ... is there any reason NOT to use assert ? (it's less letters :)) To answer your two questions: There should be very little performance hit for leaving in an assertion, unless the actual action in the assertion is time consuming (e.g. assert([obj

Assert a good practice or not?

喜你入骨 提交于 2019-12-02 16:42:44
Is it a good practice to use Assert for function parameters to enforce their validity. I was going through the source code of Spring Framework and I noticed that they use Assert.notNull a lot. Here's an example public static ParsedSql parseSqlStatement(String sql) { Assert.notNull(sql, "SQL must not be null");} Here's Another one: public NamedParameterJdbcTemplate(DataSource dataSource) { Assert.notNull(dataSource, "The [dataSource] argument cannot be null."); this .classicJdbcTemplate = new JdbcTemplate(dataSource); } public NamedParameterJdbcTemplate(JdbcOperations classicJdbcTemplate) {

pytest8-skip与xfail

我与影子孤独终老i 提交于 2019-12-02 16:15:39
skip(无条件跳过测试用例)与skipif(有条件跳过测试用例) # test_skip_function.py 函数级别 import pytest import sys @pytest.mark.skip(reason='no way of currently testing this') def test_the_unknown(): assert 1 == 1 @pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher") # 有条件跳过测试用例 def test_function(): assert 1 == 1 输出结果:D:\myproject\pytest_demo>pytest -qs --tb=no test_skip_function.py ss 2 skipped in 0.02 seconds # test_skip_class.py 类级别 import pytest import sys @pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.6 or higher") class TestSkipClass: def test_class(self):

Java/ JUnit - AssertTrue vs AssertFalse

半世苍凉 提交于 2019-12-02 15:24:09
I'm pretty new to Java and am following the Eclipse Total Beginner's Tutorials . They are all very helpful, but in Lesson 12, he uses assertTrue for one test case and assertFalse for another. Here's the code: // Check the book out to p1 (Thomas) // Check to see that the book was successfully checked out to p1 (Thomas) assertTrue("Book did not check out correctly", ml.checkOut(b1, p1)); // If checkOut fails, display message assertEquals("Thomas", b1.getPerson().getName()); assertFalse("Book was already checked out", ml.checkOut(b1,p2)); // If checkOut fails, display message assertEquals("Book

python assert with and without parenthesis

廉价感情. 提交于 2019-12-02 14:47:29
Here are four simple invocations of assert: >>> assert 1==2 Traceback (most recent call last): File "<stdin>", line 1, in ? AssertionError >>> assert 1==2, "hi" Traceback (most recent call last): File "<stdin>", line 1, in ? AssertionError: hi >>> assert(1==2) Traceback (most recent call last): File "<stdin>", line 1, in ? AssertionError >>> assert(1==2, "hi") Note that the last one does not raise an error. What is the difference between calling assert with or without parenthesis that causes this behavior? My practice is to use parenthesis, but the above suggests that I should not. The last

MyBatis从入门到精通(四):MyBatis XML方式的基本用法之增删改

梦想与她 提交于 2019-12-02 14:33:40
最近在读刘增辉老师所著的《MyBatis从入门到精通》一书,很有收获,于是将自己学习的过程以博客形式输出,如有错误,欢迎指正,如帮助到你,不胜荣幸! 1. insert用法 1.1 简单的insert方法 假如现在我们想新增一个用户,该如何操作呢? 首先,在接口SysUserMapper中添加如下方法。 /** * 新增用户 * * @param sysUser * @return */ int insert ( SysUser sysUser ) ; 然后打开对应的SysUserMapper.xml文件,添加如下语句。 < insert id = " insert " > INSERT INTO sys_user(id, user_name, user_password, user_email, user_info, head_img, create_time) VALUES (#{id},#{userName},#{userPassword},#{userEmail},#{userInfo},#{headImg,jdbcType=BLOB},#{createTime,jdbcType=TIMESTAMP}) </ insert > 特别说明: 1)为了防止类型错误,对于一些特殊的数据类型,建议指定具体的jdbcType值。例如headImg指定BLOB类型

Using assert_eq or printing large fixed sized arrays doesn't work

感情迁移 提交于 2019-12-02 13:59:41
问题 I have written some tests where I need to assert that two arrays are equal. Some arrays are [u8; 48] while others are [u8; 188] : #[test] fn mul() { let mut t1: [u8; 48] = [0; 48]; let t2: [u8; 48] = [0; 48]; // some computation goes here. assert_eq!(t1, t2, "\nExpected\n{:?}\nfound\n{:?}", t2, t1); } I get multiple errors here: error[E0369]: binary operation `==` cannot be applied to type `[u8; 48]` --> src/main.rs:8:5 | 8 | assert_eq!(t1, t2, "\nExpected\n{:?}\nfound\n{:?}", t2, t1); | ^^^^