Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Jul 25, 2024
1 parent e3b02ba commit 66b6749
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
6 changes: 4 additions & 2 deletions python/examples/pytest/test_fixture.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import tempfile

def setup_module():
print("setup_module")
global db
db = 42
db = tempfile.TemporaryDirectory()
print(f"setup_module: {db}")

def teardown_module():
print("teardown_module")
Expand Down
13 changes: 13 additions & 0 deletions python/examples/pytest/test_functions.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
$ pytest -qqs test_functions.py

setup_db <TemporaryDirectory '/tmp/tmppjc3h7k2'>
test_one <TemporaryDirectory '/tmp/tmppjc3h7k2'>
test_one after
teardown_db <TemporaryDirectory '/tmp/tmppjc3h7k2'>
setup_db <TemporaryDirectory '/tmp/tmp_0j_gnnb'>
test_two <TemporaryDirectory '/tmp/tmp_0j_gnnb'>
setup_db <TemporaryDirectory '/tmp/tmpl3gvod_3'>
test_three <TemporaryDirectory '/tmp/tmpl3gvod_3'>
test_three after
teardown_db <TemporaryDirectory '/tmp/tmpl3gvod_3'>

34 changes: 34 additions & 0 deletions python/examples/pytest/test_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import tempfile

def test_one():
db = setup_db()
print(f" test_one {db}")
assert True
print(" test_one after")
teardown_db(db)

def test_two():
db = setup_db()
print(f" test_two {db}")
assert False
print(" test_two after")
teardown_db(db)

def test_three():
db = setup_db()
print(f" test_three {db}")
assert True
print(" test_three after")
teardown_db(db)

def setup_db():
db = tempfile.TemporaryDirectory()
...
print(f"setup_db {db}")
return db

def teardown_db(db):
...
print(f"teardown_db {db}")


15 changes: 13 additions & 2 deletions python/pytest-fixtures.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# PyTest Fixtures
{id: pytest-fixtures}


## PyTest What are Fixtures
## PyTest: What are Fixtures?
{id: pytest-what-are-fixture}

* In generally we call [test fixture](https://en.wikipedia.org/wiki/Test_fixture) the environment in which a test is expected to run.
Expand All @@ -18,6 +17,18 @@ Specific examples:
* If I'd like to test the login mechanism, I need that before the test starts running we'll have a verified account in the system.
* If I test the 3rd element in a pipeline I need the results of the 2nd pipeline to get started and after the test runs I need to remove all those files.

## PyTest: test with functions
{id: pytest-test-with-functions}

* We need to call the `setup_db()` in every test.
* We need to call the `teardown_db()` in every test - and it still does not work when the test fails.
* What if there is some work that needs to be done only once and not for every test?

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

![](examples/pytest/test_functions.out)


## PyTest Fixture setup and teardown xUnit style
{id: pytest-fixture-setup-teardown}
{i: setup_function}
Expand Down

0 comments on commit 66b6749

Please sign in to comment.