最新消息: 关于Git&GitHub 版本控制你了解多少?
您现在的位置是:群英 > 开发技术 > Python语言 >
hamcrest如何实现简单的断言?
CSDN发表于 2020-09-07 09:58 次浏览

         python自带assert可实现简单断言,太复杂的就算了,不过简单的用下面这些方法足够了。下面举例说明。

 

示例

from hamcrest import *


def test_hamcrest():
    the_string = 'Hello Hamcrest'
    my_string = 'Hello Hamcrest'
    assert_that(the_string, equal_to(my_string))

匹配库

Object

关键字 说明
equal_to match equal object
has_length match len()
has_property match value of property with given name
has_properties match an object that has all of the given properties.
has_string match str()
instance_of match object type
none, not_none match None, or not None
same_instance match same object
calling, raises wrap a method call and assert that it raises an exception

Number

关键字 说明
close_to match number close to a given value
greater_than, greater_than_or_equal_to, less_than, less_than_or_equal_to match numeric ordering

Text

关键字 说明
contains_string match part of a string
ends_with match the end of a string
equal_to_ignoring_case match the complete string but ignore case
equal_to_ignoring_whitespace match the complete string but ignore extra whitespace
matches_regexp match a regular expression in a string
starts_with match the beginning of a string
string_contains_in_order match parts of a string, in relative order

Logical

关键字 说明
all_of and together all matchers
any_of or together all matchers
anything match anything, useful in composite matchers when you don’t care about a particular value
is_not, not_ negate the matcher

Sequence

关键字 说明
contains exactly match the entire sequence
contains_inanyorder match the entire sequence, but in any order
has_item match if given item appears in the sequence
has_items match if all given items appear in the sequence, in any order
is_in match if item appears in the given sequence
only_contains match if sequence’s items appear in given list
empty match if the sequence is empty

Dictionary

关键字 说明
has_entries match dictionary with list of key
has_entry match dictionary containing a key
has_key match dictionary with a key
has_value match dictionary with a value

Decorator

关键字 说明
calling wrap a callable in a deferred object, for subsequent matching on calling behaviour
raises Ensure that a deferred callable raises as expected
described_as give the matcher a custom failure description
is_ decorator to improve readability

标签:python教程
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
相关信息推荐