You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tests/README.md
+28-23Lines changed: 28 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,13 +12,17 @@ backgroundColor: #fff
12
12
## Quick start
13
13
14
14
### Installation
15
+
15
16
Install pytest. e.g., from the "dev" dependencies
17
+
16
18
```bash
17
19
pip install ".[dev]"
18
20
```
19
21
20
22
### How to use
23
+
21
24
To execute the tests run e.g.
25
+
22
26
```bash
23
27
pytest
24
28
```
@@ -30,43 +34,46 @@ pytest
30
34
---
31
35
32
36
### Folder and test naming
37
+
33
38
1. The tests for functions in `<filename>.py` should go in `tests/test_<filename>.py`
34
-
35
-
e.g., the tests for [python_package/mockup.py](../src/python_package/mockup.py) are in [tests/test_mockup.py](test_mockup.py)
36
39
37
-
2. The test names should start with `def test_<corresponding_function_name> ...`
40
+
e.g., the tests for [python_package/mockup.py](../src/python_package/mockup.py) are in [tests/test_mockup.py](test_mockup.py)
41
+
42
+
2. The test names should start with `def test_<corresponding_function_name> ...`
38
43
39
-
e.g., `def test_hello_world(): ...`
44
+
e.g., `def test_hello_world(): ...`
40
45
41
46
---
42
47
43
48
### Some Pytest decorators
44
49
45
50
1. To indicate that the test function is expected to fail you can prepend
46
-
```python
47
-
@pytest.mark.xfail(raises=TypeError)
48
-
deftest_hello_world_str(): ...
49
-
```
51
+
52
+
```python
53
+
@pytest.mark.xfail(raises=TypeError)
54
+
deftest_hello_world_str(): ...
55
+
```
50
56
51
57
---
52
58
53
59
2. To setup and cleanup any resources for a test you can use [pytest fixtures with `yield`](https://dev.to/dawidbeno/understanding-yield-in-pytest-fixtures-4m38)
54
60
55
-
```python
56
-
@pytest.fixture
57
-
def temp_file():
58
-
# set up
59
-
< code to create a file>
60
-
# return
61
-
yield
62
-
the_file
63
-
# clean up
64
-
< code to remove the file>
65
-
```
61
+
```python
62
+
@pytest.fixture
63
+
deftemp_file():
64
+
# set up
65
+
< code to create a file>
66
+
# return
67
+
yield
68
+
the_file
69
+
# clean up
70
+
< code to remove the file>
71
+
```
66
72
67
73
---
68
74
69
75
### Doctests
76
+
70
77
You can also include tests in your docstrings using `>>>` followed by the expected result e.g.
71
78
72
79
```python
@@ -82,7 +89,8 @@ def hello_world(n):
82
89
...
83
90
"""
84
91
```
85
-
*Needs `addopts = --doctest-modules` in pytest.ini
92
+
93
+
Needs `addopts = --doctest-modules` in `pytest.ini` in root of directory
0 commit comments