跳转到主要内容

preggy 是一个用于Python的断言库。**你期望的是什么?

项目描述

preggy

Build Status PyPi version PyPi downloads Coverage Status

preggy 是一个 Python 的断言库。你期望什么?

来自 PyVows 项目。

安装

我们推荐使用 pip

pip install preggy

用法

简单告诉测试你期望的 expect()

from preggy import expect

def test_roses_are_red():
    rose = Rose()
    expect(rose.color).to_equal('red')

def test_violets_are_not_red():
    violet = Violet()
    expect(violet.color).not_to_equal('red')

内置期望

相等

expect(4).to_equal(4)
expect(5).Not.to_equal(4)
expect(5).not_to_equal(4)  # same as previous

比较

expect(4).to_be_lesser_than(5)
expect(5).to_be_greater_than(4)
expect(5).Not.to_be_lesser_than(4)
expect(4).not_to_be_greater(5)  # same as previous

expect(4).to_be_lesser_or_equal_to(5)
expect(4).to_be_lesser_or_equal_to(4)
expect(5).not_to_be_lesser_or_equal_to(4)

expect(5).to_be_greater_or_equal_to(4)
expect(5).to_be_greater_or_equal_to(5)
expect(4).not_to_be_greater_or_equal_to(5)

expect("b").to_be_greater_than("a")
expect("a").to_be_lesser_than("b")

expect([1, 2, 3]).to_be_greater_than([1, 2])  # comparing using length
expect((1, 2, 3)).to_be_greater_than((1, 2))  # comparing using length
expect({ "a": "b", "c": "d" }).to_be_greater_than({ "a": "b" })  # comparing using length of keys

相似性

expect('sOmE RandOm     CAse StRiNG').to_be_like('some random case string')

expect(1).to_be_like(1)
expect(1).to_be_like(1.0)
expect(1).to_be_like(long(1))

expect([1, 2, 3]).to_be_like([3, 2, 1])
expect([1, 2, 3]).to_be_like((3, 2, 1))
expect([[1, 2], [3,4]]).to_be_like([4, 3], [2, 1]])

expect({ 'some': 1, 'key': 2 }).to_be_like({ 'key': 2, 'some': 1 })

expect('sOmE RandOm     CAse StRiNG').Not.to_be_like('other string')
expect('sOmE RandOm     CAse StRiNG').not_to_be_like('other string')  # same as previous

expect(1).not_to_be_like(2)
expect([[1, 2], [3,4]]).not_to_be_like([4, 4], [2, 1]])
expect({ 'some': 1, 'key': 2 }).not_to_be_like({ 'key': 3, 'some': 4 })

类型

expect(os.path).to_be_a_function()
expect(1).to_be_numeric()
expect({ 'some': 1, 'key': 2 }).to_be_instance_of(dict)
expect(open(__file__)).to_be_a_file()

expect('some').Not.to_be_a_function()
expect('some').Not.to_be_numeric()
expect('some').Not.to_be_instance_of(dict)
expect('some').Not.to_be_a_file()

真 / 假

expect(True).to_be_true()
expect('some').to_be_true()
expect([1, 2, 3]).to_be_true()
expect({ 'a': 'b' }).to_be_true()
expect(1).to_be_true()

expect(False).to_be_false()  # not_to_be_true() would work, too. but, it's so...eww
expect(None).to_be_false()
expect('').to_be_false()
expect(0).to_be_false()
expect([]).to_be_false()
expect({}).to_be_false()

空值

expect(None).to_be_null()
expect('some').Not.to_be_null()
expect('some').not_to_be_null()  # same as previous

包含

expect([1, 2, 3]).to_include(2)
expect((1, 2, 3)).to_include(2)
expect('123').to_include('2')
expect({ 'a': 1, 'b': 2, 'c': 3}).to_include('b')

expect([1, 3]).Not.to_include(2)  # or, exclusion...

正则表达式

expect('some').to_match(r'^[a-z]+')
expect('Some').Not.to_match(r'^[a-z]+')

长度

expect([1, 2, 3]).to_length(3)
expect((1, 2, 3)).to_length(3)
expect('abc').to_length(3)
expect({ 'a': 1, 'b': 2, 'c': 3}).to_length(3)
expect(lifo_queue).to_length(2)
expect(queue).to_length(3)

expect([1]).Not.to_length(3)
expect([1]).not_to_length(3)  # same as previous

expect([]).to_be_empty()
expect(tuple()).to_be_empty()
expect({}).to_be_empty()
expect('').to_be_empty()

expect([1]).not_to_be_empty()
expect((1,2)).not_to_be_empty()
expect({'a': 1}).not_to_be_empty()
expect('roses are red').not_to_be_empty()

异常

expect(RuntimeError()).to_be_an_error() 
expect(RuntimeError()).to_be_an_error_like(RuntimeError)
expect(ValueError('error')).to_have_an_error_message_of('error')

expect("I'm not an error").Not.to_be_an_error()
expect(ValueError()).Not.to_be_an_error_like(RuntimeError)
expect(ValueError('some')).Not.to_have_an_error_message_of('error')

# when expecting a method to error
err = expect.error_to_happen(RuntimeError)  # attribute to a variable so you can use the exception later

with err:
    raise RuntimeError("something is wrong")

expect(err).to_have_an_error_message_of('something is wrong')

# or the shorter version
with expect.error_to_happen(RuntimeError, message="something is wrong"):
    raise RuntimeError("something is wrong")

# or if you don't care about the message:
with expect.error_to_happen(RuntimeError):
    raise RuntimeError("something is wrong")

# or if you need to make sure error does not happen
with expect.error_not_to_happen(RuntimeError, message="something is wrong"):
    raise RuntimeError("something is wrong")  # Fails with AssertionError

# or if you don't care about the message, only that the error does not happen:
with expect.error_not_to_happen(RuntimeError):
    raise RuntimeError("something is wrong")  # Fails with AssertionError

失败

expect.not_to_be_here()  # raises AssertionError

# raises AssertionError with error message
expect.not_to_be_here("some error message")

链式断言

# assertions may be chained, for brevity:
expect(6).not_to_be_null().to_equal(6)

# a more *sensible* example:
expect(foo).not_to_be_null().to_equal(expected.get('foo'))

贡献

查看 CONTRIBUTING

许可协议

MIT 许可证 (MIT)

版权所有 (c) 2013 Bernardo Heynemann heynemann@gmail.com

在此特此授予任何获得此软件及其相关文档副本(“软件”)的人免费使用软件的权利,不受限制地处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,并允许向软件提供方提供软件的人这样做,但受以下条件约束

上述版权声明和本许可声明应包含在软件的任何副本或主要部分中。

本软件按“原样”提供,不提供任何形式的保证,无论是明示的还是暗示的,包括但不限于适销性、特定用途的适用性和非侵权性。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论这些责任是因合同、侵权或其他原因而产生的,无论这些责任是否与软件或软件的使用或其他方式有关。

项目详情


发布历史 发布通知 | RSS 源

下载文件

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

源分发

preggy-1.4.4.tar.gz (23.4 kB 查看哈希值)

上传时间

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面