Skip to content

Commit a7acdc5

Browse files
authored
Merge pull request #159 from okken/okken_update_docs
doc update
2 parents d7afe0d + e797262 commit a7acdc5

File tree

6 files changed

+68
-28
lines changed

6 files changed

+68
-28
lines changed

README.md

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,28 +65,33 @@ def test_httpx_get(check):
6565
`check` also helper functions for common checks.
6666
These methods do NOT need to be inside of a `with check:` block.
6767

68-
- **check.equal** - *a == b*
69-
- **check.not_equal** - *a != b*
70-
- **check.is_** - *a is b*
71-
- **check.is_not** - *a is not b*
72-
- **check.is_true** - *bool(x) is True*
73-
- **check.is_false** - *bool(x) is False*
74-
- **check.is_none** - *x is None*
75-
- **check.is_not_none** - *x is not None*
76-
- **check.is_in** - *a in b*
77-
- **check.is_not_in** - *a not in b*
78-
- **check.is_instance** - *isinstance(a, b)*
79-
- **check.is_not_instance** - *not isinstance(a, b)*
80-
- **check.almost_equal** - *a == pytest.approx(b, rel, abs)* see at: [pytest.approx](https://docs.pytest.org/en/latest/reference.html#pytest-approx)
81-
- **check.not_almost_equal** - *a != pytest.approx(b, rel, abs)* see at: [pytest.approx](https://docs.pytest.org/en/latest/reference.html#pytest-approx)
82-
- **check.greater** - *a > b*
83-
- **check.greater_equal** - *a >= b*
84-
- **check.less** - *a < b*
85-
- **check.less_equal** - *a <= b*
86-
- **check.between(b, a, c, ge=False, le=False)** - *a < b < c*
87-
- **check.between_equal(b, a, c)** - *a <= b <= c*
88-
- **check.raises** - *func raises given exception* similar to [pytest.raises](https://docs.pytest.org/en/latest/reference/reference.html#pytest-raises)
89-
- **check.fail(msg)** - *Log a failure*
68+
| Function | Meaning | Notes |
69+
|------------------------------------------------------|-----------------------------------|------------------------------------------------------------------------------------------------------|
70+
| `equal(a, b, msg="")` | `a == b` | |
71+
| `not_equal(a, b, msg="")` | `a != b` | |
72+
| `is_(a, b, msg="")` | `a is b` | |
73+
| `is_not(a, b, msg="")` | `a is not b` | |
74+
| `is_true(x, msg="")` | `bool(x) is True` | |
75+
| `is_false(x, msg="")` | `bool(x) is False` | |
76+
| `is_none(x, msg="")` | `x is None` | |
77+
| `is_not_none(x, msg="")` | `x is not None` | |
78+
| `is_in(a, b, msg="")` | `a in b` | |
79+
| `is_not_in(a, b, msg="")` | `a not in b` | |
80+
| `is_instance(a, b, msg="")` | `isinstance(a, b)` | |
81+
| `is_not_instance(a, b, msg="")` | `not isinstance(a, b)` | |
82+
| `almost_equal(a, b, rel=None, abs=None, msg="")` | `a == pytest.approx(b, rel, abs)` | [pytest.approx](https://docs.pytest.org/en/latest/reference.html#pytest-approx) |
83+
| `not_almost_equal(a, b, rel=None, abs=None, msg="")` | `a != pytest.approx(b, rel, abs)` | [pytest.approx](https://docs.pytest.org/en/latest/reference.html#pytest-approx) |
84+
| `greater(a, b, msg="")` | `a > b` | |
85+
| `greater_equal(a, b, msg="")` | `a >= b` | |
86+
| `less(a, b, msg="")` | `a < b` | |
87+
| `less_equal(a, b, msg="")` | `a <= b` | |
88+
| `between(b, a, c, msg="", ge=False, le=False)` | `a < b < c` | |
89+
| `between_equal(b, a, c, msg="")` | `a <= b <= c` | same as `between(b, a, c, msg, ge=True, le=True)` |
90+
| `raises(expected_exception, *args, **kwargs)` | *Raises given exception* | similar to [pytest.raises](https://docs.pytest.org/en/latest/reference/reference.html#pytest-raises) |
91+
| `fail(msg)` | *Log a failure* | |
92+
93+
**Note: This is a list of relatively common logic operators. I'm reluctant to add to the list too much, as it's easy to add your own.**
94+
9095

9196
The httpx example can be rewritten with helper functions:
9297

@@ -103,8 +108,9 @@ Which you use is personal preference.
103108

104109
## Defining your own check functions
105110

106-
The `@check.check_func` decorator allows you to wrap any test helper that has an assert
107-
statement in it to be a non-blocking assert function.
111+
### Using `@check.check_func`
112+
113+
The `@check.check_func` decorator allows you to wrap any test helper that has an assert statement in it to be a non-blocking assert function.
108114

109115

110116
```python
@@ -121,6 +127,33 @@ def test_all_four():
121127
is_four(4)
122128
```
123129

130+
131+
### Using `check.fail()`
132+
133+
Using `@check.check_func` is probably the easiest.
134+
However, it does have a bit of overhead in the passing cases
135+
that can affect large loops of checks.
136+
137+
If you need a bit of a speedup, use the following style with the help of `check.fail()`.
138+
139+
```python
140+
from pytest_check import check
141+
142+
def is_four(a):
143+
__tracebackhide__ = True
144+
if a == 4:
145+
return True
146+
else:
147+
check.fail(f"check {a} == 4")
148+
return False
149+
150+
def test_all_four():
151+
is_four(1)
152+
is_four(2)
153+
is_four(3)
154+
is_four(4)
155+
```
156+
124157
## Using raises as a context manager
125158

126159
`raises` is used as context manager, much like `pytest.raises`. The main difference being that a failure to raise the right exception won't stop the execution of the test method.

changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ All notable changes to this project be documented in this file.
2020
2121
-->
2222

23+
## [2.3.1] - 2024-Jan-18
24+
25+
### Modified
26+
- Documentation Update, README.md
27+
- Turn help function list into a table with param lists
28+
- Show an alternative method of creating a helper function using `check.fail()`
29+
2330
## [2.3.0] - 2024-Jan-17
2431

2532
### Added

examples/test_example_fail_func.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import pytest_check as check
1+
from pytest_check import check
22

33

44
def test_one_failure():

examples/test_example_fail_in_thread.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from threading import Thread
33

44

5-
import pytest_check as check
5+
from pytest_check import check
66

77

88
def force_fail(comparison):

examples/test_example_pass_in_thread.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from threading import Thread
33

44

5-
import pytest_check as check
5+
from pytest_check import check
66

77

88
def always_pass():

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ authors = [{name = "Brian Okken"}]
44
readme = "README.md"
55
license = {file = "LICENSE.txt"}
66
description="A pytest plugin that allows multiple failures per test."
7-
version = "2.3.0"
7+
version = "2.3.1"
88
requires-python = ">=3.7"
99
classifiers = [
1010
"License :: OSI Approved :: MIT License",

0 commit comments

Comments
 (0)