-
Notifications
You must be signed in to change notification settings - Fork 13
/
assert.Rmd
103 lines (82 loc) · 2.63 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
---
jupyter:
jupytext:
notebook_metadata_filter: all,-language_info
split_at_heading: true
text_representation:
extension: .Rmd
format_name: rmarkdown
format_version: '1.2'
jupytext_version: 1.13.7
kernelspec:
display_name: Python 3
language: python
name: python3
---
# 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
```
<!-- #md -->
```{warning}
Note that `assert` *should not* be used in order to raise errors at runtime.
The use cases for assertions are development, testing and debugging your code,
and are for declaring "This condition is expected to be to true".
If this always succeeds during testing, it is considered safe to remove
[in production](https://en.wikipedia.org/wiki/Deployment_environment#Production).
If Python is run [with optimization](https://docs.python.org/3/using/cmdline.html#cmdoption-O),
`assert` statements will be removed for speed, and you as the developer do not
control whether your code is run with optimization or not.
For error conditions that you expect to encounter in runtime,
[raise exceptions](https://docs.python.org/3/tutorial/errors.html#raising-exceptions).
```
<!-- #endmd -->