Skip to content

Commit

Permalink
Add fail & between_equal()
Browse files Browse the repository at this point in the history
  • Loading branch information
okken committed Jan 17, 2024
1 parent 7fd9e2b commit bfde7d5
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ These methods do NOT need to be inside of a `with check:` block.
- **check.greater_equal** - *a >= b*
- **check.less** - *a < b*
- **check.less_equal** - *a <= b*
- **check.between** - *a < b < c*
- **check.between(b, a, c, ge=False, le=False)** - *a < b < c*
- **check.between_equal(b, a, c)** - *a <= b <= c*
- **check.raises** - *func raises given exception* similar to [pytest.raises](https://docs.pytest.org/en/latest/reference/reference.html#pytest-raises)
- **check.fail(msg)** - *Log a failure*

The httpx example can be rewritten with helper functions:

Expand Down
7 changes: 7 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ All notable changes to this project be documented in this file.
-->

## [2.3.0] - 2024-Jan-17

### Added
- `between_equal(b, a c)`
- as a shortcut for `between(b, a, c, ge=True, le=True)`
- `fail(msg)` - indicate test failure, but don't stop testing

## [2.2.5] - 2024-Jan-17

- fix [155](https://github.com/okken/pytest-check/issues/155)
Expand Down
4 changes: 4 additions & 0 deletions examples/test_example_functions_fail.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,7 @@ def test_between_le():

def test_between_ge_le():
check.between(21, 0, 20, ge=True, le=True)


def test_between_equal():
check.between_equal(21, 0, 20, ge=True, le=True)
6 changes: 6 additions & 0 deletions examples/test_example_functions_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,9 @@ def test_between_ge_le():
check.between(0, 0, 20, ge=True, le=True)
check.between(10, 0, 20, ge=True, le=True)
check.between(20, 0, 20, ge=True, le=True)


def test_between_equal():
check.between_equal(0, 0, 20)
check.between_equal(10, 0, 20)
check.between_equal(20, 0, 20)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ authors = [{name = "Brian Okken"}]
readme = "README.md"
license = {file = "LICENSE.txt"}
description="A pytest plugin that allows multiple failures per test."
version = "2.2.5"
version = "2.3.0"
requires-python = ">=3.7"
classifiers = [
"License :: OSI Approved :: MIT License",
Expand Down
12 changes: 12 additions & 0 deletions src/pytest_check/check_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
"less",
"less_equal",
"between",
"between_equal",
"check_func",
"fail",
]


Expand Down Expand Up @@ -243,3 +245,13 @@ def between(b, a, c, msg="", ge=False, le=False):
else:
log_failure(f"check {a} < {b} < {c}", msg)
return False


def between_equal(b, a, c, msg=""):
__tracebackhide__ = True
return between(b, a, c, msg, ge=True, le=True)


def fail(msg):
__tracebackhide__ = True
log_failure(msg)
16 changes: 16 additions & 0 deletions tests/test_fail_func.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def test_fail_func(pytester):
pytester.copy_example("examples/test_example_fail_func.py")
result = pytester.runpytest("--check-max-tb=2")
result.assert_outcomes(failed=2)
result.stdout.fnmatch_lines(
[
"*FAILURE: one",
"*test_one_failure() -> check.fail('one')",
"Failed Checks: 1",
"*FAILURE: one",
"*test_two_failures() -> check.fail('one')",
"*FAILURE: two",
"*test_two_failures() -> check.fail('two')",
"Failed Checks: 2",
],
)
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=22)
result.assert_outcomes(failed=0, passed=23)


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

0 comments on commit bfde7d5

Please sign in to comment.