Hamcrest matchers for Python

Related tags

TestingPyHamcrest
Overview

PyHamcrest

Introduction

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

When writing tests it is sometimes difficult to get the balance right between overspecifying the test (and making it brittle to changes), and not specifying enough (making the test less valuable since it continues to pass even when the thing being tested is broken). Having a tool that allows you to pick out precisely the aspect under test and describe the values it should have, to a controlled level of precision, helps greatly in writing tests that are "just right." Such tests fail when the behavior of the aspect under test deviates from the expected behavior, yet continue to pass when minor, unrelated changes to the behaviour are made.

Installation

Hamcrest can be installed using the usual Python packaging tools. It depends on distribute, but as long as you have a network connection when you install, the installation 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's assertEqual method, we'll use PyHamcrest's assert_that construct and the standard set of matchers:

from hamcrest import *
import unittest


class 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 test assertion. In this example, the subject of the assertion is the object theBiscuit, which is the first method parameter. The second method parameter is a matcher for Biscuit objects, here a matcher that checks one object is equal to another using the Python == operator. The test passes since the Biscuit class defines an __eq__ method.

If you have more than one assertion in your test you can include an identifier for 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 because it'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, but another matcher, so matchers can be composed for greater flexibility. For example, only_contains(less_than(5)) will match any sequence where every item is less than 5.

Syntactic sugar

PyHamcrest strives to make your tests as readable as possible. For example, the is_ matcher is a wrapper that doesn't add any extra behavior to the underlying 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 arguments with equal_to. But if the argument is a type, it is wrapped with instance_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 find that 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 set of properties over and over again (and in different tests), and you want to bundle the fragment into a single assertion. By writing your own matcher you'll eliminate 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 BaseMatcher
from hamcrest.core.helpers.hasmethod import hasmethod


class 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 - which calls the weekday method after confirming that the argument (which may not be a date) has such a method - and the describe_to method - which is used to produce a failure message when a test fails. Here's an example of how the failure 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. We could use it in our test by importing the factory function on_a_saturday:

from hamcrest import *
import unittest
from isgivendayofweek import on_a_saturday


class 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 it is called, you should not assume this is the only usage pattern for your matcher. Therefore you should make sure your matcher is stateless, so a single instance can be reused between matches.

More resources

Comments
  • PyHamcrest 1.10.0 is not py2-compatible. It should set python_requires>=3 to avoid breaking users who are still on Py2.

    PyHamcrest 1.10.0 is not py2-compatible. It should set python_requires>=3 to avoid breaking users who are still on Py2.

    import hamcrest no longer works on Python 2 with pyhamcrest==1.10.0.

    To avoid installation of pyhamcrest==1.10.0 and newer versions on Python 2, it would be better to add python_requires>=3.0 stanza in setup.py starting from 1.10.0. To fix this for 1.10.0, we would need to update already released 1.10.0 pypi artifacts.

    As of now, all python 2 users who install pyhamcrest without restricting the version to 1.9.0, will be broken.

    A better user experience would be if Python 2 users get the last version of pyhamcrest that is supported on Python 2. This is convention is often followed by major python libraries, for example numpy.

    opened by tvalentyn 16
  • Created calling/raises matcher

    Created calling/raises matcher

    New functional implementation of assert raises that uses the matcher style of hamcrest to check that exceptions are being raised as expected with the new matcher raises. There is a helper called calling that wraps a call with arguments to delay execution to where the matcher can catch any raised exception, but you can happily use raises on any function that takes no arguments. As a bonus, you can provide a regex to raises and it will look for that pattern in the stringification of the assertion - important in cases where a verification stage may throw variations of the same assertion.

    Accepts any subclass of the exception provided. Does not try to run uncallable actuals.

    assert_that(calling(parse, bad_data), raises(ValueError))
    assert_that(calling(translate).with_(curse_words), raises(LanguageError, "\w+very naughty"))
    assert_that(broken, raises(Exception))
    # This will fail and complain that 23 is not callable
    # assert_that(23, raises(IOError))
    
    opened by perfa 16
  • The test suite fails with pytest 4

    The test suite fails with pytest 4

    While packaging PyHamcrest for openSUSE/Tumbledweed, I have discovered that the test suite fails with Pytest 4.

    [   34s] + PYTHONPATH=:/home/abuild/rpmbuild/BUILDROOT/python-PyHamcrest-1.9.0-0.x86_64/usr/lib/python2.7/site-packages
    [   34s] + py.test-2.7 --ignore=_build.python2 --ignore=_build.python3 --ignore=_build.pypy3 -v
    [   34s] ============================= test session starts ==============================
    [   34s] platform linux2 -- Python 2.7.16, pytest-4.6.5, py-1.8.0, pluggy-0.13.0 -- /usr/bin/python2
    [   34s] cachedir: .pytest_cache
    [   34s] hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('/home/abuild/rpmbuild/BUILD/PyHamcrest-1.9.0/.hypothesis/examples')
    [   34s] rootdir: /home/abuild/rpmbuild/BUILD/PyHamcrest-1.9.0
    [   34s] plugins: hypothesis-4.40.2
    [   36s] collecting ... collected 453 items / 2 errors / 451 selected
    [   36s] 
    [   36s] ==================================== ERRORS ====================================
    [   36s] __________ ERROR collecting tests/hamcrest_unit_test/core/is_test.py ___________
    [   36s] /usr/lib/python2.7/site-packages/pluggy/hooks.py:286: in __call__
    [   36s]     return self._hookexec(self, self.get_hookimpls(), kwargs)
    [   36s] /usr/lib/python2.7/site-packages/pluggy/manager.py:92: in _hookexec
    [   36s]     return self._inner_hookexec(hook, methods, kwargs)
    [   36s] /usr/lib/python2.7/site-packages/pluggy/manager.py:86: in <lambda>
    [   36s]     firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
    [   36s] /usr/lib/python2.7/site-packages/_pytest/python.py:234: in pytest_pycollect_makeitem
    [   36s]     res = list(collector._genfunctions(name, obj))
    [   36s] /usr/lib/python2.7/site-packages/_pytest/python.py:410: in _genfunctions
    [   36s]     self.ihook.pytest_generate_tests(metafunc=metafunc)
    [   36s] /usr/lib/python2.7/site-packages/pluggy/hooks.py:286: in __call__
    [   36s]     return self._hookexec(self, self.get_hookimpls(), kwargs)
    [   36s] /usr/lib/python2.7/site-packages/pluggy/manager.py:92: in _hookexec
    [   36s]     return self._inner_hookexec(hook, methods, kwargs)
    [   36s] /usr/lib/python2.7/site-packages/pluggy/manager.py:86: in <lambda>
    [   36s]     firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
    [   36s] /usr/lib/python2.7/site-packages/_pytest/python.py:137: in pytest_generate_tests
    [   36s]     metafunc.parametrize(*marker.args, **marker.kwargs)
    [   36s] /usr/lib/python2.7/site-packages/_pytest/python.py:1004: in parametrize
    [   36s]     function_definition=self.definition,
    [   36s] /usr/lib/python2.7/site-packages/_pytest/mark/structures.py:130: in _for_parametrize
    [   36s]     if len(param.values) != len(argnames):
    [   36s] E   TypeError: object of type 'MarkDecorator' has no len()
    [   36s] _____ ERROR collecting tests/hamcrest_unit_test/core/isinstanceof_test.py ______
    [   36s] /usr/lib/python2.7/site-packages/pluggy/hooks.py:286: in __call__
    [   36s]     return self._hookexec(self, self.get_hookimpls(), kwargs)
    [   36s] /usr/lib/python2.7/site-packages/pluggy/manager.py:92: in _hookexec
    [   36s]     return self._inner_hookexec(hook, methods, kwargs)
    [   36s] /usr/lib/python2.7/site-packages/pluggy/manager.py:86: in <lambda>
    [   36s]     firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
    [   36s] /usr/lib/python2.7/site-packages/_pytest/python.py:234: in pytest_pycollect_makeitem
    [   36s]     res = list(collector._genfunctions(name, obj))
    [   36s] /usr/lib/python2.7/site-packages/_pytest/python.py:410: in _genfunctions
    [   36s]     self.ihook.pytest_generate_tests(metafunc=metafunc)
    [   36s] /usr/lib/python2.7/site-packages/pluggy/hooks.py:286: in __call__
    [   36s]     return self._hookexec(self, self.get_hookimpls(), kwargs)
    [   36s] /usr/lib/python2.7/site-packages/pluggy/manager.py:92: in _hookexec
    [   36s]     return self._inner_hookexec(hook, methods, kwargs)
    [   36s] /usr/lib/python2.7/site-packages/pluggy/manager.py:86: in <lambda>
    [   36s]     firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
    [   36s] /usr/lib/python2.7/site-packages/_pytest/python.py:137: in pytest_generate_tests
    [   36s]     metafunc.parametrize(*marker.args, **marker.kwargs)
    [   36s] /usr/lib/python2.7/site-packages/_pytest/python.py:1004: in parametrize
    [   36s]     function_definition=self.definition,
    [   36s] /usr/lib/python2.7/site-packages/_pytest/mark/structures.py:130: in _for_parametrize
    [   36s]     if len(param.values) != len(argnames):
    [   36s] E   TypeError: object of type 'MarkDecorator' has no len()
    [   36s] !!!!!!!!!!!!!!!!!!! Interrupted: 2 errors during collection !!!!!!!!!!!!!!!!!!!!
    [   36s] =========================== 2 error in 1.48 seconds ============================
    

    The full build log is also available.

    opened by mcepl 15
  • add has_return_value matcher.

    add has_return_value matcher.

    Hello, maintainers of PyHamcrest! Long-time admirer, first time pull-requester.

    This new matcher will match against the return value of an object's method. In the world of web automation, this matcher will be particularly useful to test for things like whether an element is displayed by matching against its is_displayed() method return value, or checking class strings, or any number of things an automated tester might need to check.

    I've already created a version of this matcher locally in a test suite that's using my ScreenPy library, but i think the work i did there would be useful to others in this more generalized form.

    I tried my best to follow the conventions i saw elsewhere in this codebase. Please let me know if i've left something out or am not following the conventions correctly—i am happy to make any changes you might want!

    opened by perrygoy 11
  • Python version compatibility metadata

    Python version compatibility metadata

    This PR adds packaging metadata that marks the package as only supporting Python 3.5+. This prevents Pip from installing it on Python 2.7.

    You can find documentation here: https://setuptools.readthedocs.io/en/latest/setuptools.html#new-and-changed-setup-keywords (search in page for "python_requires").

    I'm not sure how you do releases, but I'll note that you may need non-ancient (last four years or so) setuptools to generate a wheel that contains this metadata.

    Fixes #131.

    opened by twm 11
  • Output of has_properties over verbose

    Output of has_properties over verbose

    I think the mismatch description of has_porperties is very verbose and actually making readability worse.

    assert_that(b, has_properties(x=6))
    Expected: (an object with a property 'x' matching <6>)
         but: an object with a property 'x' matching <6> property 'x' was <7>
    

    this is from all_of repeating the offending matcher, which does make sense when the user composes various matchers. But has_properties to the user is a single matcher and the output is only confusing if you don't know it's made up of multiple matchers.

    opened by keis 11
  • Don't use describe_to of mock objects

    Don't use describe_to of mock objects

    When using hamcrest together with mocks and asserting that the same mock objects are being passed around the assertion messages produce are bad because hamcrest ends up calling describe_to of the mocks which does nothing.

    before

    >>> from hamcrest import assert_that, equal_to
    >>> from unittest.mock import Mock
    >>> a,b = Mock(),Mock()
    >>> assert_that(a, equal_to(b))
    AssertionError: 
    Expected: 
         but: was 
    

    after

    >>> from hamcrest import assert_that, equal_to
    >>> from unittest.mock import Mock
    >>> a,b = Mock(),Mock()
    AssertionError: 
    Expected: <Mock id='139861799912056'>
         but: was <Mock id='139861799911832'>
    
    opened by keis 11
  • Provide better mismatch descriptions for `any_of` and `only_contains`

    Provide better mismatch descriptions for `any_of` and `only_contains`

    What problem does this merge request solve?

    running

    from hamcrest import *
    
    if __name__ == '__main__':
        assert_that(['a'], only_contains(has_length(3)))
    

    in master reports

    Traceback (most recent call last):
    ...
    AssertionError:
    Expected: a sequence containing items matching (an object with length of <3>)
         but: was <['a']>
    

    after the merge request, the error message becomes

    Traceback (most recent call last):
    ...
    AssertionError:
    Expected: a sequence containing items matching (an object with length of <3>)
         but: was <['a']>, first sequence item not matching was 'a' with length of <1>
    

    However, this merge request is not perfect.

    Known (Guessed) Problems

    • if sequence is a stream generator that can only be iterated once, then describe_mismatch will fail trying to iterate it again. I believe it would be safe to encapsulate list(sequence) as an instance variable (or hide behind a memoized property) where it could be used from both methods, but the tutorial/docs are pushing in the direction of stateless matcher preferences. Thus not being familiar enough with the architecture and usages of hamcrest, I didn't want to do that initially

    Potentatial Improvements

    • prefix/postfix could be handled as TestCase class variables (like a "Template Method")
    • any_of had the same problem (8f0bcce) as only_contains (5a6c990) - not having a proper delegation to the actual matchers to provide the mismatch description. I suspect there can be other (composite) matchers that have the same issue.
    • now issequence_onlycontaining_test is coupled to the has_length implementation. Probably there is (should be) a way to have a matcher with test-controllable describe_foo values, so there is no need for coupling. Then the initial assertions from testDescribeMismatchDisplayUsedMatchersDescriptionForFirstFailingItem could be removed
    • there is too much duplication between describe_mismatch and _matches. However, to resolve that by introducing a get_first_mismatch method would need to be an object (can't use None due to the need to distinguish between no mismatches and TypeError return type cases), and I didn't want to invest in that until I knew that change was welcome
    opened by zsoldosp 11
  • is_not/not_ will not invert raises() matcher

    is_not/not_ will not invert raises() matcher

    As far as I can tell is_not should invert any matcher. Recently had a situation where I need an to invert a raises and it did not work as expected. In some ways inverting raises is an antipattern but in my case alternatives are uglier. Below is a simplified example of the issue.

    Please advise if this is a bug? or is there an alternative way to address this.

    Content example.py

    from hamcrest import assert_that, calling, raises, not_                            
    assert_that(calling(int).with_args('q'), not_(raises(ValueError)))
    

    Output:

    Traceback (most recent call last):
      File "/home/test/example1.py", line 2, in <module>
        assert_that(calling(int).with_args('q'), not_(raises(ValueError)))
      File "/home/test/_venv/lib/python3.6/site-packages/hamcrest/core/assert_that.py", line 43, in assert_that
        _assert_match(actual=arg1, matcher=arg2, reason=arg3)
      File "/home/test/_venv/lib/python3.6/site-packages/hamcrest/core/assert_that.py", line 57, in _assert_match
        raise AssertionError(description)
    AssertionError: 
    Expected: not Expected a callable raising <class 'ValueError'>
         but: was <hamcrest.core.core.raises.DeferredCallable object at 0x7ffbaaba97f0>
    

    Expected: test passing

    opened by hersheleh 10
  • Improve has_properties description.

    Improve has_properties description.

    Before:

    str(has_properties(x=6, y=5, z=7))
    (an object with a property 'x' matching <6> and an object with a property 'y' matching <5> and an object with a property 'z' matching <7>)
    

    After:

    str(has_properties(x=6, y=5, z=7))
    an object with properties 'x' matching <6> and 'y' matching <5> and 'z' matching <7>
    
    opened by brunns 10
  • Add `not_` to the matcher-list in the Readme.

    Add `not_` to the matcher-list in the Readme.

    not_ is an alias for is_not which is used for readability reasons. It should be mentioned in the Readme because this is the list where many people look for a list of matchers, especially as long as #74 is not fixed.

    opened by Kaligule 9
  • Matchers should be contravariant

    Matchers should be contravariant

    Currently, the Matcher class has a type arg T as it should, but Matchers are currently invariant with respect to T. However, I think it's quite clear that the following should be legal code:

    matcher: Matcher[str] = has_length(greater_than(0))
    

    I've recreated that definition here:

    from typing import Any, Generic, Sequence, Sized, TypeVar
    
    T = TypeVar("T")
    
    
    class Matcher(Generic[T]):
      def matches(self, _: T) -> bool:
        ...
    
    
    def greater_than(_: Any) -> Matcher[Any]:
      ...
    
    
    def has_length(_: int | Matcher[int]) -> Matcher[Sized]:
      ...
    
    
    matcher: Matcher[str] = has_length(greater_than(0))
    

    Pyright provides the following error:

    » pyright test.py
    [...]
    test.py
      test.py:23:25 - error: Expression of type "Matcher[Sized]" cannot be assigned to declared type "Matcher[str]"
        "Matcher[Sized]" is incompatible with "Matcher[str]"
          TypeVar "[email protected]" is invariant
            "Sized" is incompatible with "str" (reportGeneralTypeIssues)
    1 error, 0 warnings, 0 informations
    Completed in 0.582sec
    

    Changing line 3 to T = TypeVar('T', contravariant=True) fixes the issue.

    python version: 3.10.8 pyhamcrest version: 2.0.4 pyright version: 1.1.280

    opened by mezuzza 7
  • equal_to describes itself incompletely

    equal_to describes itself incompletely

    Consider how equal_to describes itself:

    >>> d = StringDescription()
    >>> d.append_description_of(equal_to(3))
    <hamcrest.core.string_description.StringDescription object at 0x7fd8de172c40>
    >>> str(d)
    '<3>'
    

    What does <3> mean? It's hard to tell. Compare this with other built-in matcher self-descriptions:

    >>> d = StringDescription()
    >>> d.append_description_of(greater_than(3))
    <hamcrest.core.string_description.StringDescription object at 0x7fd8de0c0d90>
    >>> str(d)
    'a value greater than <3>'
    
    feature request 
    opened by exarkun 1
  • object comparison override

    object comparison override

    Hi there, I am new to Hamcrest and would like to know if there is an example to override the standard object comparison equal operator. I basically have to change the list comparison for the properties of the object otherwise the default one is order dependent. Cheers.

    opened by priamai 3
  • contains_inanyorder fails although I would expect it to pass

    contains_inanyorder fails although I would expect it to pass

    version 2.0.2

    assert_that(['a',4], contains_inanyorder(*[greater_than(0), 'a']))
    Expected: a sequence over [a value greater than <0>, 'a'] in any order
         but: was <['a', 4]>
    

    I would expect is to pass as the definition of the matcher is: "This matcher iterates the evaluated sequence, seeing if each element satisfies any of the given matchers."

    Reason for thie behavior is that:

    class MatchInAnyOrder(object):
    [...]
            def ismatched(self, item: T) -> bool:
            for index, matcher in enumerate(self.matchers):
                if matcher.matches(item):
                    del self.matchers[index]
                    return True
    

    matcher.matches(item) throughs a TypeErrror when running a against greater_than(0 TypeError: '>' not supported between instances of 'str' and 'int'

    Maybe it should be catched like
    try:
               if matcher.matches(item):
                   del self.matchers[index]
                   return True
    except:
               pass
    

    What is curious though is that it is catched here:

    class IsSequenceContainingInAnyOrder(BaseMatcher[Sequence[T]]):
        def __init__(self, matchers: Sequence[Matcher[T]]) -> None:
            self.matchers = matchers
    
        def matches(
            self, item: Sequence[T], mismatch_description: Optional[Description] = None
        ) -> bool:
            try:
                sequence = list(item)
                matchsequence = MatchInAnyOrder(self.matchers, mismatch_description)
                for element in sequence:
                    if not matchsequence.matches(element):
                        return False
                return matchsequence.isfinished(sequence)
            except TypeError:
                if mismatch_description:
                    super(IsSequenceContainingInAnyOrder, self).describe_mismatch(
                        item, mismatch_description
                    )
                return False
    

    But is this the correct and expected behaviour though?

    matcher bug good first issue 
    opened by redsox10 5
Releases(V2.0.3)
A Django plugin for pytest.

Welcome to pytest-django! pytest-django allows you to test your Django project/applications with the pytest testing tool. Quick start / tutorial Chang

pytest-dev 1.1k Dec 31, 2022
API mocking with Python.

apyr apyr (all lowercase) is a simple & easy to use mock API server. It's great for front-end development when your API is not ready, or when you are

Umut Seven 55 Nov 25, 2022
A toolbar overlay for debugging Flask applications

Flask Debug-toolbar This is a port of the excellent django-debug-toolbar for Flask applications. Installation Installing is simple with pip: $ pip ins

863 Dec 29, 2022
模仿 USTC CAS 的程序,用于开发校内网站应用的本地调试。

ustc-cas-mock 模仿 USTC CAS 的程序,用于开发校内网站应用阶段调试。 请勿在生产环境部署! 只测试了最常用的三个 CAS route: /login /serviceValidate(验证 CAS ticket) /logout 没有测试过 proxy ticket。(因为我

taoky 4 Jan 27, 2022
PyBuster A directory busting tool for web application penetration tester, written in python

PyBuster A directory busting tool for web application penetration tester, written in python. Supports custom wordlist,recursive search. Screenshots Pr

Anukul Pandey 4 Jan 30, 2022
The successor to nose, based on unittest2

Welcome to nose2 nose2 is the successor to nose. It's unittest with plugins. nose2 is a new project and does not support all of the features of nose.

736 Dec 16, 2022
:game_die: Pytest plugin to randomly order tests and control random.seed

pytest-randomly Pytest plugin to randomly order tests and control random.seed. Features All of these features are on by default but can be disabled wi

pytest-dev 471 Dec 30, 2022
Aioresponses is a helper for mock/fake web requests in python aiohttp package.

aioresponses Aioresponses is a helper to mock/fake web requests in python aiohttp package. For requests module there are a lot of packages that help u

402 Jan 06, 2023
pytest plugin providing a function to check if pytest is running.

pytest-is-running pytest plugin providing a function to check if pytest is running. Installation Install with: python -m pip install pytest-is-running

Adam Johnson 21 Nov 01, 2022
Avocado is a set of tools and libraries to help with automated testing.

Welcome to Avocado Avocado is a set of tools and libraries to help with automated testing. One can call it a test framework with benefits. Native test

Ana Guerrero Lopez 1 Nov 19, 2021
Mypy static type checker plugin for Pytest

pytest-mypy Mypy static type checker plugin for pytest Features Runs the mypy static type checker on your source files as part of your pytest test run

Dan Bader 218 Jan 03, 2023
Based on the selenium automatic test framework of python, the program crawls the score information of the educational administration system of a unive

whpu_spider 该程序基于python的selenium自动化测试框架,对某高校的教务系统的成绩信息实时爬取,在检测到成绩更新之后,会通过电子邮件的方式,将更新的成绩以文本的方式发送给用户,可以使得用户在不必手动登录教务系统网站时,实时获取成绩更新的信息。 该程序仅供学习交流,不可用于恶意攻

1 Dec 30, 2021
Scraping Bot for the Covid19 vaccination website of the Canton of Zurich, Switzerland.

Hi 👋 , I'm David A passionate developer from France. 🌱 I’m currently learning Kotlin, ReactJS and Kubernetes 👨‍💻 All of my projects are available

1 Nov 14, 2021
HTTP load generator, ApacheBench (ab) replacement, formerly known as rakyll/boom

hey is a tiny program that sends some load to a web application. hey was originally called boom and was influenced from Tarek Ziade's tool at tarekzia

Jaana Dogan 14.9k Jan 07, 2023
自动化爬取并自动测试所有swagger-ui.html显示的接口

swagger-hack 在测试中偶尔会碰到swagger泄露 常见的泄露如图: 有的泄露接口特别多,每一个都手动去试根本试不过来 于是用python写了个脚本自动爬取所有接口,配置好传参发包访问 原理是首先抓取http://url/swagger-resources 获取到有哪些标准及对应的文档地

jayus 534 Dec 29, 2022
Subprocesses for Humans 2.0.

Delegator.py — Subprocesses for Humans 2.0 Delegator.py is a simple library for dealing with subprocesses, inspired by both envoy and pexpect (in fact

Amit Tripathi 1.6k Jan 04, 2023
How to Create a YouTube Bot that Increases Views using Python Programming Language

YouTube-Bot-in-Python-Selenium How to Create a YouTube Bot that Increases Views using Python Programming Language. The app is for educational purpose

Edna 14 Jan 03, 2023
XSSearch - A comprehensive reflected XSS tool built on selenium framework in python

XSSearch A Comprehensive Reflected XSS Scanner XSSearch is a comprehensive refle

Sathyaprakash Sahoo 49 Oct 18, 2022
Automated testing tool developed in python for Advanced mathematical operations.

Advanced-Maths-Operations-Validations Automated testing tool developed in python for Advanced mathematical operations. Requirements Python 3.5 or late

Nikhil Repale 1 Nov 16, 2021
Doggo Browser

Doggo Browser Quick Start $ python3 -m venv ./venv/ $ source ./venv/bin/activate $ pip3 install -r requirements.txt $ ./sobaki.py References Heavily I

Alexey Kutepov 9 Dec 12, 2022