forked from RasmussenLab/python_package
-
Notifications
You must be signed in to change notification settings - Fork 0
init testing docs and added example tests #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c8c283b
:memo: marp to create slides from readme
angelphanth 4f2a476
note pytest.ini for doctest
angelphanth a535ef2
add function
angelphanth c5e949a
add tests from demo
angelphanth 6a4c34e
ruff lint and format
angelphanth 160cf40
:art: format README.md
enryH 7bbae54
rm pres
angelphanth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
--- | ||
marp: true | ||
theme: uncover | ||
paginate: true | ||
backgroundColor: #fff | ||
--- | ||
|
||
# **Getting started with pytesting** | ||
|
||
--- | ||
|
||
## Quick start | ||
|
||
### Installation | ||
|
||
Install pytest. e.g., from the "dev" dependencies | ||
|
||
```bash | ||
pip install ".[dev]" | ||
``` | ||
|
||
### How to use | ||
|
||
To execute the tests run e.g. | ||
|
||
```bash | ||
pytest | ||
``` | ||
|
||
--- | ||
|
||
## Test development tips | ||
|
||
--- | ||
|
||
### Folder and test naming | ||
|
||
1. The tests for functions in `<filename>.py` should go in `tests/test_<filename>.py` | ||
|
||
e.g., the tests for [python_package/mockup.py](../src/python_package/mockup.py) are in [tests/test_mockup.py](test_mockup.py) | ||
|
||
2. The test names should start with `def test_<corresponding_function_name> ...` | ||
|
||
e.g., `def test_hello_world(): ...` | ||
|
||
--- | ||
|
||
### Some Pytest decorators | ||
|
||
1. To indicate that the test function is expected to fail you can prepend | ||
|
||
```python | ||
@pytest.mark.xfail(raises=TypeError) | ||
def test_hello_world_str(): ... | ||
``` | ||
|
||
--- | ||
|
||
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) | ||
|
||
```python | ||
@pytest.fixture | ||
def temp_file(): | ||
# set up | ||
< code to create a file> | ||
# return | ||
yield | ||
the_file | ||
# clean up | ||
< code to remove the file> | ||
``` | ||
|
||
--- | ||
|
||
### Doctests | ||
|
||
You can also include tests in your docstrings using `>>>` followed by the expected result e.g. | ||
|
||
```python | ||
def hello_world(n): | ||
""" | ||
Prints 'hello world' n-times. | ||
... | ||
|
||
Examples | ||
-------- | ||
>>> hello_world(3) | ||
'hello world hello world hello world' | ||
... | ||
""" | ||
``` | ||
|
||
Needs `addopts = --doctest-modules` in `pytest.ini` in root of directory | ||
|
||
--- | ||
|
||
### Skipping in doctests | ||
|
||
If you know that the test cannot succeed but would like to include an example usage in the docstring still then you can add `# doctest: +SKIP` e.g. | ||
|
||
```python | ||
def saved_world(filename): | ||
""" | ||
Count how many times 'hello world' is in a file. | ||
... | ||
|
||
Examples | ||
-------- | ||
>>> saved_world("not-real.txt") # doctest: +SKIP | ||
... | ||
""" | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,31 @@ | ||
from python_package import hello_world | ||
from python_package import hello_world, saved_world | ||
import pytest | ||
|
||
|
||
def test_hello_world_3times(): | ||
expected = "hello world hello world hello world" | ||
result = hello_world(3) | ||
assert result == expected | ||
|
||
|
||
@pytest.mark.xfail(raises=TypeError) | ||
def test_hello_world_str(): | ||
hello_world("3") | ||
|
||
|
||
@pytest.fixture | ||
def temp_file(): | ||
# set up | ||
filename = "temp_hellooo.txt" | ||
with open(filename, "w") as f: | ||
f.write("hello world hello world hello world") | ||
yield filename | ||
# clean up | ||
import os | ||
|
||
os.remove(filename) | ||
|
||
|
||
def test_saved_world_3times(temp_file): | ||
result = saved_world(temp_file) | ||
assert result == 3 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are these different hyphens?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these hyphens were for autogenerating slides from markdown using Marp
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interesting! But I removed it for GitHub.