Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Jul 25, 2024
1 parent 541d043 commit 6a2c6b0
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 12 deletions.
5 changes: 5 additions & 0 deletions python/examples/pytest/key-value-store-outside/store.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"color": "Red",
"pid": 2997265,
"size": "42"
}
3 changes: 2 additions & 1 deletion python/examples/pytest/key-value-store-outside/test_store.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import os
import store

def test_store(tmpdir):
os.environ['STORE_DIR'] = str(tmpdir) # str expected, not LocalPath
import store
print(os.environ['STORE_DIR'])
store.set('color', 'Blue')
assert store.get('color') == 'Blue'

Expand Down
8 changes: 8 additions & 0 deletions python/examples/pytest/test_parametrized_fixture.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Fixture before test using apple
Test using apple
Fixture after test using apple

Fixture before test using banana
Test not using param
Fixture after test using banana

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Fixture before test using apple
Test using apple
Fixture after test using apple
Fixture before test using banana
Test not using param
Fixture after test using banana

20 changes: 20 additions & 0 deletions python/examples/pytest/test_parametrized_injection_fixture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os
import pathlib
import time
import pytest


@pytest.fixture(params=["name"])
def generate(name):
print(f"Fixture before test using {name}")
yield
print(f"Fixture after test using {name}")

@pytest.mark.parametrize("name", ["apple"])
def test_with_param(name, generate):
print(f"Test using {name}")

@pytest.mark.parametrize("name", ["banana"])
def test_without_param(generate):
print(f"Test not using param")

38 changes: 27 additions & 11 deletions python/pytest-fixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,17 @@ def serve_bolognese(pasta, sauce):
## Pytest CLI key-value store
{id: pytest-cli-key-value-store}

* This is a similar application - a file-base key-value store - where the data files is computed from the name of the program: `store.json`.
* Runing two tests in parallel will make the tests collide by using the same data file.

![](examples/pytest/key-value-store/store.py)
![](examples/pytest/key-value-store/test_store.py)

## Pytest testing key-value store - environment variable
{id: pytest-cli-testing-key-value-store}

* We need to be able to set the name of the data file externally. e.g. Using an environment variable.

![](examples/pytest/key-value-store-testable/store.py)
![](examples/pytest/key-value-store-testable/test_store.py)

Expand All @@ -171,6 +176,7 @@ def serve_bolognese(pasta, sauce):

## Pytest capture STDOUT and STDERR with capsys
{id: pytest-capsys}
{i: capsys}

Captures everything that is printed to STDOUT and STDERR so we can compare that to the expected output and error.

Expand All @@ -180,6 +186,16 @@ Captures everything that is printed to STDOUT and STDERR so we can compare that
pytest test_greet.py
```

## PyTest - write your own fixture
{id: pytest-write-your-own-fixture}

* `tmpdir` and `capsys` are nice to have, but we will need more complex setup and teardown.

* We can write any function to become fixture, we only need to decorate it with `@pytest.fixture`

* We can implement fixture functions to act like the xUnit fixture we saw ealrier or using dependency injection as `tmpdir` and `capsys` work.


## Pytest Fixture - autouse fixtures
{id: pytest-autouse-fixture}
{i: yield}
Expand Down Expand Up @@ -288,6 +304,11 @@ $ pytest -sq

![](examples/pytest/fixture-mongodb/conftest.py)

## Pytest parameters
{id: pytest-parameters}

![](examples/pytest/test_mymath_parameters.py)

## Pytest parametrized fixture
{id: pytest-parametrized-fixture}

Expand All @@ -296,16 +317,14 @@ Sometimes we would like to pass some parameters to the fixture. We can do this w
{/aside}

![](examples/pytest/test_parametrized_fixture.py)
![](examples/pytest/test_parametrized_fixture.out)

```
Fixture before test using apple
Test using apple
Fixture after test using apple
## Pytest parametrized fixture with dependency injection
{id: pytest-parametrized-fixture-with-dependency-injection}

Fixture before test using banana
Test not using param
Fixture after test using banana
```

![](examples/pytest/test_parametrized_injection_fixture.py)
![](examples/pytest/test_parametrized_injection_fixture.out)

## Pytest parametrized fixture to use Docker
{id: pytest-parametrized-fixture-to-use-docker}
Expand All @@ -317,7 +336,4 @@ At one point I wanted the whole image creation and running in the image be part

![](examples/pytest/test_parametrized_fixture_with_docker.py)

## Pytest parameters
{id: pytest-parameters}

![](examples/pytest/test_mymath_parameters.py)

0 comments on commit 6a2c6b0

Please sign in to comment.