Skip to content

Commit

Permalink
Add kwargs to unit_eq and iterable_eq: passed on to `pytest.app…
Browse files Browse the repository at this point in the history
…rox()` (#336)
  • Loading branch information
trappitsch committed Mar 17, 2022
1 parent 9c167d4 commit 9af0cc9
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions instruments/tests/__init__.py
Expand Up @@ -105,12 +105,18 @@ def expected_protocol(ins_class, host_to_ins, ins_to_host, sep="\n", repeat=1):
# """Only read {} bytes out of {}""".format(current, end)


def unit_eq(a, b):
def unit_eq(a, b, **kwargs):
"""
Asserts that two unitful quantites ``a`` and ``b``
are equal up to a small numerical threshold.
Asserts that two unitful quantites ``a`` and ``b`` are equal up to a small numerical
threshold. Keyword arguments ``kwargs`` are passed on to ``pytest.approx()``.
:param a: First quantity to compare to second.
:type a: `~pint.Quantity`
:param b: Second quantity to compare to first.
:type b: `~pint.Quantity`
:param kwargs: Keyword arguments, passed on to ``pytest.approx()``.
"""
assert a.magnitude == pytest.approx(b.magnitude)
assert a.magnitude == pytest.approx(b.magnitude, **kwargs)
assert a.units == b.units, f"{a} and {b} have different units"


Expand All @@ -127,9 +133,14 @@ def test():
return test


def iterable_eq(a, b):
def iterable_eq(a, b, **kwargs):
"""
Asserts that the contents of two iterables are the same.
Asserts that the contents of two iterables are the same. Keyword arguments
``kwargs`` are passed on ``unit_eq``.
:param a: First iterable to compare to second.
:param b: Second iterable to compare to first.
:param kwargs: Keyword arguments, passed on to ``pytest.approx()``
"""
if numpy and (isinstance(a, numpy.ndarray) or isinstance(b, numpy.ndarray)):
# pylint: disable=unidiomatic-typecheck
Expand All @@ -141,6 +152,6 @@ def iterable_eq(a, b):
), f"Length of iterables is not the same, got {len(a)} and {len(b)}"
assert (a == b).all()
elif isinstance(a, u.Quantity) and isinstance(b, u.Quantity):
unit_eq(a, b)
unit_eq(a, b, **kwargs)
else:
assert a == b

0 comments on commit 9af0cc9

Please sign in to comment.