Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zackakil/i-promise-to-maybe-write-tests
๐ How I'm going to maybe write code that breaks less. ๐งช
https://github.com/zackakil/i-promise-to-maybe-write-tests
best-practices jupyter-notebook pytest python testing
Last synced: 6 days ago
JSON representation
๐ How I'm going to maybe write code that breaks less. ๐งช
- Host: GitHub
- URL: https://github.com/zackakil/i-promise-to-maybe-write-tests
- Owner: ZackAkil
- Created: 2023-10-18T21:19:49.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-10-18T21:33:11.000Z (about 1 year ago)
- Last Synced: 2024-04-17T22:07:55.457Z (7 months ago)
- Topics: best-practices, jupyter-notebook, pytest, python, testing
- Language: Jupyter Notebook
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# i promise to maybe write tests ๐งช [exhibit_A.ipynb](exhibit_A.ipynb)
## Specifically within jupyter notebooks which is my goto IDE now.
How I'm going to maybe write code that breaks less.#### Step 1, make modules in cells using %%writefile
```python
%%writefile something.pydef something_fun(x):
if type(x) is str:
return str(int(x)+2)if x is None:
return None
return x + 2
```#### Step 2, write tests in a cell in the pytest format, and save to test_.py files with %%writefile
```python
%%writefile test_main.pyimport something
def test_answer():
assert something.something_fun(3) == 5def test_answer2():
assert something.something_fun(9) == 11def test_answer3():
assert something.something_fun(-2) == 0def test_answer4():
assert something.something_fun(None) == Nonedef test_answer_str():
assert something.something_fun('7') == '9'```
#### Step 4, Run tests using pytest (again running in a cell)
```python
!pytest
```#### Step 5, Load the module using importlib.reload to allow for interactive devlopment without restarting notebook
```python
import somethingfrom importlib import reload
reload(something)print(something.something_fun(7))
print(something.something_fun('-2'))
```