Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kwargs to unit_eq and iterable_eq: passed on to pytest approx #336

Merged
merged 1 commit into from Mar 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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