Skip to main content

一个易于使用的 Python 断言库

项目描述

坚持
======

一个易于使用的 python 断言库。


快速入门
===========

安装坚持
--------------

```
$ pip install assert
```



```
$ sudo pip install assert
`` `

Create Insist object
--------------------

```python
from assert import Insist assert = Insist(

) # 引发

RuntimeError 而不是 AssertionError
assert_rt = Insist(RuntimeError)

#在所有消息前加上“OOPS:”
assert_prefix = Insist(message_prefix="OOPS")

# 同时显示自定义和标准错误消息
# 可以与 message_prefix 结合
assert_both = Insist(always_show_standard=True)
```进行

断言
------------- --

```python
assert.true(1 == 1) # 有效断言

坚持(1 == 1) # 与坚持.true(expr) 相同

坚持(1 == 2) # 引发 AssertionError

# 引发带有自定义错误的 AssertionError message
assert(1 == 2, "Danger, Will Robinson")

# 引发 AssertionError,自定义和标准错误消息与“:”结合
assert_both(1 == 2, "Danger, Will Robinson")
```

参考
=========

所有测试
---------

```python
from assert import Insist assert = Insist

( ) assert(x) # x ==

True ) # x == y 坚持.not_equal(x, y) # x != y 坚持.less(x, y) # x < y 坚持.less_equal(x, y) # x <= y















坚持.greater(x, y) # x > y

坚持.greater_equal(x, y) # x >= y

坚持.is_same(x, y) # x是y

坚持.is_not(x, y) # x不是y

assert.is_in(x, y) # x in y

persist.not_in(x, y) # x not in y

persist.is_subclass(x, y) # issubclass(x, y

) issubclass(x, y)

坚持.isa(x, y) # isinstance(x, y)

坚持.is_not_a(x, y) #不是isinstance(x, y)

坚持.is_string(x, y) # isinstance(x, str) for Py3
# isinstance(x, (str, unicode)) for Py2

assert.is_not_string(x, y) # not isinstance(x, str) for Py3
# not isinstance(x, (str, unicode)) for Py2

assert.keys(x, required, optional, extra, custom) # 见下面的例子

# 用于其余示例的字典
x = { 'a' : 1, 'b' : 2, 'c' : 3 }

# 要求 x 列出了允许附加键的键。
坚持.keys(x, required=['a', 'b']) # OK坚持 .keys
(x, required=['a', 'd']) # 引发错误

不允许使用密钥。
assert.keys(x, required=['a', 'b'], extra=False) # 引发错误

# 要求 x 有列出的键,但除了可选键之外没有其他键。
坚持.keys(x, required=['a', 'b'], optional=['c']) # OK
坚持.keys(x, required=['a', 'b'], optional=[' d']) # 引发错误
```

Chainable Tests
---------------

“that” 方法保留其值并将其用作链中剩余方法的第一个值。
这简化了针对多个标准对单个值的测试。

```python
from assert import Insist assert = Insist(

)

x = 3

persist.that(x).isa(int).greater_equal(0).less(10) # 如果 x 是 0 到 9 之间的整数,则 OK

# "that" 方法的结果也可以存储在变量中。

say_x = assert.that(x)

say_x.isa(int).greater_equal(0)

say_x.less(len(message))


# 除了上面列出的方法,“那个链”还有两个额外的方法。
# has 和 not_has 方法与 is_in 和 not_in 相似,只是
# 参数颠倒了。

x = [ 1, 2, 3 ]

say_x = assert.that(x)

say_x.has(1).has(2) # OK
say_x.has(1).not_has(2) # 引发错误
say_x.has(1) .has(5) # 引发错误

x = { 'a' : 1, 'b' : 2, 'c' : 3 }

say_x = assert.that(x)

say_x.has('a').has('b') # OK
say_x.has('a').has('d') # 引发错误
```



版权和许可
========= ============

版权所有 2014 Ray Harris

根据 Apache 许可证 2.0 版(“许可证”)获得许可;
除非遵守许可,否则您不得使用此模块。 您可以在http://www.apache.org/licenses/LICENSE-2.0
获取许可证的副本 除非适用法律要求或书面同意,否则 根据许可证分发的软件按“原样”分发, 没有任何明示或暗示的保证或条件。






有关许可下的特定语言管理权限和
限制,请参阅许可。

下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

坚持-0.2.tar.gz (5.1 kB 查看哈希)

已上传 source