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 is_nan #166

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion examples/test_example_functions_fail.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Failing versions of all of the check helper functions.
"""
from pytest_check import check

import math

def test_equal():
check.equal(1, 2)
Expand All @@ -24,6 +24,14 @@ def test_is_not():
check.is_not(x, y)


def test_is_nan():
check.is_nan(42)


def test_is_not_nan():
check.is_not_nan(math.nan)


def test_is_true():
check.is_true(False)

Expand Down
10 changes: 9 additions & 1 deletion examples/test_example_functions_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Passing versions of all of the check helper functions.
"""
from pytest_check import check

import math

def test_equal():
check.equal(1, 1)
Expand All @@ -18,6 +18,14 @@ def test_is():
check.is_(x, y)


def test_is_nan():
check.is_nan(math.nan)


def test_is_not_nan():
check.is_not_nan(0)


def test_is_not():
x = ["foo"]
y = ["foo"]
Expand Down
21 changes: 20 additions & 1 deletion src/pytest_check/check_functions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import functools

import pytest

import math
from .check_log import log_failure

__all__ = [
Expand All @@ -14,6 +14,8 @@
"is_false",
"is_none",
"is_not_none",
"is_nan",
"is_not_nan",
"is_in",
"is_not_in",
"is_instance",
Expand Down Expand Up @@ -121,6 +123,23 @@ def is_not_none(x, msg=""):
return False


def is_nan(a, msg=""):
__tracebackhide__ = True
if math.isnan(a):
return True
else:
log_failure(f"check {a} is NaN", msg)
return False


def is_not_nan(a, msg=""):
__tracebackhide__ = True
if not math.isnan(a):
return True
else:
log_failure(f"check {a} is not NaN", msg)
return False

def is_in(a, b, msg=""):
__tracebackhide__ = True
if a in b:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
def test_passing_check_functions(pytester):
pytester.copy_example("examples/test_example_functions_pass.py")
result = pytester.runpytest()
result.assert_outcomes(failed=0, passed=23)
result.assert_outcomes(failed=0, passed=25)


def test_failing_check_functions(pytester):
pytester.copy_example("examples/test_example_functions_fail.py")
result = pytester.runpytest()
result.assert_outcomes(failed=23, passed=0)
result.assert_outcomes(failed=25, passed=0)
Loading