Skip to content

Commit

Permalink
examples
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Jul 25, 2024
1 parent 66b6749 commit 8af9753
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 35 deletions.
27 changes: 11 additions & 16 deletions python/examples/pytest/test_fixture.out
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
setup_module

setup_function
test_one
42
setup_module: <TemporaryDirectory '/tmp/tmp18kxxqva'>
setup_function <TemporaryDirectory '/tmp/tmp_bd8lazj'>
test_one <TemporaryDirectory '/tmp/tmp18kxxqva'> <TemporaryDirectory '/tmp/tmp_bd8lazj'>
test_one after
teardown_function

setup_function
test_two
teardown_function

setup_function
test_three
teardown_function <TemporaryDirectory '/tmp/tmp_bd8lazj'>
setup_function <TemporaryDirectory '/tmp/tmp9k2cbqcb'>
test_two <TemporaryDirectory '/tmp/tmp18kxxqva'> <TemporaryDirectory '/tmp/tmp9k2cbqcb'>
teardown_function <TemporaryDirectory '/tmp/tmp9k2cbqcb'>
setup_function <TemporaryDirectory '/tmp/tmp5u8n90cx'>
test_three <TemporaryDirectory '/tmp/tmp18kxxqva'> <TemporaryDirectory '/tmp/tmp5u8n90cx'>
test_three after
teardown_function
teardown_function <TemporaryDirectory '/tmp/tmp5u8n90cx'>
teardown_module <TemporaryDirectory '/tmp/tmp18kxxqva'>

teardown_module
42
22 changes: 11 additions & 11 deletions python/examples/pytest/test_fixture.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import tempfile

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

def teardown_module():
print("teardown_module")
print(db)
print(f"teardown_module {db_server}")


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

def teardown_function():
print(" teardown_function")
print(f" teardown_function {db}")


def test_one():
print(" test_one")
print(db)
print(f" test_one {db_server} {db}")
assert True
print(" test_one after")

def test_two():
print(" test_two")
print(f" test_two {db_server} {db}")
assert False
print(" test_two after")

def test_three():
print(" test_three")
print(f" test_three {db_server} {db}")
assert True
print(" test_three after")
20 changes: 12 additions & 8 deletions python/examples/pytest/test_functions.out
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
$ pytest -qqs test_functions.py

setup_db <TemporaryDirectory '/tmp/tmppjc3h7k2'>
test_one <TemporaryDirectory '/tmp/tmppjc3h7k2'>
setup db_server
new db_serverironment
setup_db <TemporaryDirectory '/tmp/tmpsqk01wqt'>
test_one <TemporaryDirectory '/tmp/tmpsqk01wqt'>
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'>
teardown_db <TemporaryDirectory '/tmp/tmpsqk01wqt'>
setup db_server
setup_db <TemporaryDirectory '/tmp/tmp8qyhchd6'>
test_two <TemporaryDirectory '/tmp/tmp8qyhchd6'>
setup db_server
setup_db <TemporaryDirectory '/tmp/tmpu4ei9yc5'>
test_three <TemporaryDirectory '/tmp/tmpu4ei9yc5'>
test_three after
teardown_db <TemporaryDirectory '/tmp/tmpl3gvod_3'>
teardown_db <TemporaryDirectory '/tmp/tmpu4ei9yc5'>

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

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

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

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

def setup_db():
db = tempfile.TemporaryDirectory()
Expand All @@ -32,3 +38,13 @@ def teardown_db(db):
print(f"teardown_db {db}")


def setup_db_server():
print("setup db_server")
if 'db_server' not in setup_db_server.__dict__:
print("new db_serverironment")
setup_db_server.db_server = tempfile.TemporaryDirectory()
return setup_db_server.db_server

def teardown_db_server(db_server):
print("teardown_db_server")

2 changes: 2 additions & 0 deletions python/pytest-fixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Specific examples:
## PyTest: test with functions
{id: pytest-test-with-functions}

If we don't have any of the fixture services we need to write a lot of code:

* 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?
Expand Down

0 comments on commit 8af9753

Please sign in to comment.