• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

PyHamcrest: Hamcrest matchers for Python

原作者: [db:作者] 来自: 网络 收藏 邀请

PyHamcrest

Documentation Status CI Build Status PyPI Package latest release PyPI Package monthly downloads

Introduction

PyHamcrest is a framework for writing matcher objects, allowing you todeclaratively define "match" rules. There are a number of situations wherematchers are invaluable, such as UI validation, or data filtering, but it is inthe area of writing flexible tests that matchers are most commonly used. Thistutorial shows you how to use PyHamcrest for unit testing.

When writing tests it is sometimes difficult to get the balance right betweenoverspecifying the test (and making it brittle to changes), and not specifyingenough (making the test less valuable since it continues to pass even when thething being tested is broken). Having a tool that allows you to pick outprecisely the aspect under test and describe the values it should have, to acontrolled level of precision, helps greatly in writing tests that are "justright." Such tests fail when the behavior of the aspect under test deviatesfrom the expected behavior, yet continue to pass when minor, unrelated changesto the behaviour are made.

Installation

Hamcrest can be installed using the usual Python packaging tools. It depends ondistribute, but as long as you have a network connection when you install, theinstallation process will take care of that for you.

My first PyHamcrest test

We'll start by writing a very simple PyUnit test, but instead of using PyUnit'sassertEqual method, we'll use PyHamcrest's assert_that construct andthe standard set of matchers:

from hamcrest import *import unittestclass BiscuitTest(unittest.TestCase):    def testEquals(self):        theBiscuit = Biscuit("Ginger")        myBiscuit = Biscuit("Ginger")        assert_that(theBiscuit, equal_to(myBiscuit))if __name__ == "__main__":    unittest.main()

The assert_that function is a stylized sentence for making a testassertion. In this example, the subject of the assertion is the objecttheBiscuit, which is the first method parameter. The second methodparameter is a matcher for Biscuit objects, here a matcher that checks oneobject is equal to another using the Python == operator. The test passessince the Biscuit class defines an __eq__ method.

If you have more than one assertion in your test you can include an identifierfor the tested value in the assertion:

assert_that(theBiscuit.getChocolateChipCount(), equal_to(10), "chocolate chips")assert_that(theBiscuit.getHazelnutCount(), equal_to(3), "hazelnuts")

As a convenience, assert_that can also be used to verify a boolean condition:

assert_that(theBiscuit.isCooked(), "cooked")

This is equivalent to the assert_ method of unittest.TestCase, but becauseit's a standalone function, it offers greater flexibility in test writing.

Predefined matchers

PyHamcrest comes with a library of useful matchers:

  • 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-value pairs
    • has_entry - match dictionary containing a key-value pair
    • 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 - see Syntactic sugar below

The arguments for many of these matchers accept not just a matching value, butanother matcher, so matchers can be composed for greater flexibility. Forexample, only_contains(less_than(5)) will match any sequence where everyitem is less than 5.

Syntactic sugar

PyHamcrest strives to make your tests as readable as possible. For example, theis_ matcher is a wrapper that doesn't add any extra behavior to theunderlying matcher. The following assertions are all equivalent:

assert_that(theBiscuit, equal_to(myBiscuit))assert_that(theBiscuit, is_(equal_to(myBiscuit)))assert_that(theBiscuit, is_(myBiscuit))

The last form is allowed since is_(value) wraps most non-matcher argumentswith equal_to. But if the argument is a type, it is wrapped withinstance_of, so the following are also equivalent:

assert_that(theBiscuit, instance_of(Biscuit))assert_that(theBiscuit, is_(instance_of(Biscuit)))assert_that(theBiscuit, is_(Biscuit))

Note that PyHamcrest's ``is_`` matcher is unrelated to Python's ``is``operator. The matcher for object identity is ``same_instance``.

Writing custom matchers

PyHamcrest comes bundled with lots of useful matchers, but you'll probably findthat you need to create your own from time to time to fit your testing needs.This commonly occurs when you find a fragment of code that tests the same setof properties over and over again (and in different tests), and you want tobundle the fragment into a single assertion. By writing your own matcher you'lleliminate code duplication and make your tests more readable!

Let's write our own matcher for testing if a calendar date falls on a Saturday.This is the test we want to write:

def testDateIsOnASaturday(self):    d = datetime.date(2008, 4, 26)    assert_that(d, is_(on_a_saturday()))

And here's the implementation:

from hamcrest.core.base_matcher import BaseMatcherfrom hamcrest.core.helpers.hasmethod import hasmethodclass IsGivenDayOfWeek(BaseMatcher):    def __init__(self, day):        self.day = day  # Monday is 0, Sunday is 6    def _matches(self, item):        if not hasmethod(item, "weekday"):            return False        return item.weekday() == self.day    def describe_to(self, description):        day_as_string = [            "Monday",            "Tuesday",            "Wednesday",            "Thursday",            "Friday",            "Saturday",            "Sunday",        ]        description.append_text("calendar date falling on ").append_text(            day_as_string[self.day]        )def on_a_saturday():    return IsGivenDayOfWeek(5)

For our Matcher implementation we implement the _matches method - whichcalls the weekday method after confirming that the argument (which may notbe a date) has such a method - and the describe_to method - which is usedto produce a failure message when a test fails. Here's an example of how thefailure message looks:

assert_that(datetime.date(2008, 4, 6), is_(on_a_saturday()))

fails with the message:

AssertionError:Expected: is calendar date falling on Saturday     got: <2008-04-06>

Let's say this matcher is saved in a module named isgivendayofweek. Wecould use it in our test by importing the factory function on_a_saturday:

from hamcrest import *import unittestfrom isgivendayofweek import on_a_saturdayclass DateTest(unittest.TestCase):    def testDateIsOnASaturday(self):        d = datetime.date(2008, 4, 26)        assert_that(d, is_(on_a_saturday()))if __name__ == "__main__":    unittest.main()

Even though the on_a_saturday function creates a new matcher each time itis called, you should not assume this is the only usage pattern for yourmatcher. Therefore you should make sure your matcher is stateless, so a singleinstance can be reused between matches.

More resources


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap