forked from nipraxis/textbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassert.Rmd
77 lines (62 loc) · 1.69 KB
/
assert.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
---
jupyter:
jupytext:
text_representation:
extension: .Rmd
format_name: rmarkdown
format_version: '1.2'
jupytext_version: 1.11.5
---
# Using `assert` for testing
The Python `assert` statement means - "raise an error unless
the following expression is equivalent to True".
By "equivalent to True", we mean the expression returns True
from Python [truth value testing](truthiness.Rmd).
`assert` raises an `AssertionError` if the statement is equivalent to False.
It does nothing if the statement is equivalent to True.
So, if you `assert an_expression` and there is no error, then
the result of `an_expression` was equivalent to True. The
following expressions evaluate to True, and therefore the
asserts do not raise an error:
```{python}
# No errors here.
assert True
assert 10 == 10
assert 10 % 2 == 0
```
These expressions are equivalent to False, and the asserts do
raise errors:
```{python tags=c("raises-exception")}
# Raises error
assert False
```
```{python tags=c("raises-exception")}
# Raises error
assert 10 == 11
```
Although `assert` does work with expression values of True and
False, the test that assert uses is more general than
`expr_result == True`. In fact, assert uses {doc}`truth value
testing <truthiness>` to decide whether to raise an
`AssertionError` or not:
```{python}
# No error
assert ['some', 'elements'] # not-empty list tests as True
```
```{python tags=c("raises-exception")}
# Error
assert [] # an empty list tests as False
```
```{python}
# No errors
assert 10 # any number other than zero evaluates as True
assert 1
```
```{python tags=c("raises-exception")}
# Error
assert 0
```
```{python tags=c("raises-exception")}
# Error
assert None
```