mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Test A Function With Pytest as a python til
This commit is contained in:
@@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
|
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
|
||||||
|
|
||||||
_913 TILs and counting..._
|
_914 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -520,6 +520,7 @@ _913 TILs and counting..._
|
|||||||
### Python
|
### Python
|
||||||
|
|
||||||
- [Access Instance Variables](python/access-instance-variables.md)
|
- [Access Instance Variables](python/access-instance-variables.md)
|
||||||
|
- [Test A Function With Pytest](python/test-a-function-with-pytest.md)
|
||||||
|
|
||||||
### Rails
|
### Rails
|
||||||
|
|
||||||
|
|||||||
33
python/test-a-function-with-pytest.md
Normal file
33
python/test-a-function-with-pytest.md
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
# Test A Function With Pytest
|
||||||
|
|
||||||
|
The [`pytest` framework](https://docs.pytest.org/en/latest/index.html) is a
|
||||||
|
solid choice for unit testing your python project.
|
||||||
|
|
||||||
|
Any file whose name is preceeded with `test_` that contains functions whose
|
||||||
|
names are preceeded with `test_` will be processed and executed by the pytest
|
||||||
|
test runner.
|
||||||
|
|
||||||
|
```python
|
||||||
|
# test_taco.py
|
||||||
|
|
||||||
|
def taco(day):
|
||||||
|
return "Taco " + day
|
||||||
|
|
||||||
|
def test_taco_tuesday():
|
||||||
|
assert taco("Tuesday") == "Taco Tuesday" # passes
|
||||||
|
|
||||||
|
def test_taco_blank():
|
||||||
|
assert taco("") == "Taco" # fails, missing trailing space
|
||||||
|
```
|
||||||
|
|
||||||
|
Use `assert` statements to check that a comparison is `true`. If it isn't the
|
||||||
|
`assert` statement will result in a test failure with some output about what
|
||||||
|
went wrong.
|
||||||
|
|
||||||
|
Ensure you have `pytest` installed and then run the following from your project
|
||||||
|
directory:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ pytest
|
||||||
|
#=> ... you'll see the test output below
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user