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 8af9753 commit 541d043
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 32 deletions.
22 changes: 11 additions & 11 deletions python/examples/pytest/test_fixture.out
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
setup_module: <TemporaryDirectory '/tmp/tmp18kxxqva'>
setup_function <TemporaryDirectory '/tmp/tmp_bd8lazj'>
test_one <TemporaryDirectory '/tmp/tmp18kxxqva'> <TemporaryDirectory '/tmp/tmp_bd8lazj'>
setup_module: <TemporaryDirectory '/tmp/tmpaq1r7lnj'>
setup_function <TemporaryDirectory '/tmp/tmpvynb1e5h'>
test_one <TemporaryDirectory '/tmp/tmpaq1r7lnj'> <TemporaryDirectory '/tmp/tmpvynb1e5h'>
test_one after
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'>
teardown_function <TemporaryDirectory '/tmp/tmpvynb1e5h'>
setup_function <TemporaryDirectory '/tmp/tmp6cman2br'>
test_two <TemporaryDirectory '/tmp/tmpaq1r7lnj'> <TemporaryDirectory '/tmp/tmp6cman2br'>
teardown_function <TemporaryDirectory '/tmp/tmp6cman2br'>
setup_function <TemporaryDirectory '/tmp/tmpbi9pwo3j'>
test_three <TemporaryDirectory '/tmp/tmpaq1r7lnj'> <TemporaryDirectory '/tmp/tmpbi9pwo3j'>
test_three after
teardown_function <TemporaryDirectory '/tmp/tmp5u8n90cx'>
teardown_module <TemporaryDirectory '/tmp/tmp18kxxqva'>
. teardown_function <TemporaryDirectory '/tmp/tmpbi9pwo3j'>
teardown_module <TemporaryDirectory '/tmp/tmpaq1r7lnj'>

14 changes: 7 additions & 7 deletions python/examples/pytest/test_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@
def setup_module():
global db_server
db_server = tempfile.TemporaryDirectory()
print(f"setup_module: {db_server}")
print(f"setup_module: {db_server}")

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


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

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


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

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

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

setup db_server
new db_serverironment
setup_db <TemporaryDirectory '/tmp/tmpsqk01wqt'>
test_one <TemporaryDirectory '/tmp/tmpsqk01wqt'>
setup_db <TemporaryDirectory '/tmp/tmpvct7ng6r'>
test_one <TemporaryDirectory '/tmp/tmpvct7ng6r'>
test_one after
teardown_db <TemporaryDirectory '/tmp/tmpsqk01wqt'>
teardown_db <TemporaryDirectory '/tmp/tmpvct7ng6r'>
setup db_server
setup_db <TemporaryDirectory '/tmp/tmp8qyhchd6'>
test_two <TemporaryDirectory '/tmp/tmp8qyhchd6'>
setup_db <TemporaryDirectory '/tmp/tmptmaql_a_'>
test_two <TemporaryDirectory '/tmp/tmptmaql_a_'>
setup db_server
setup_db <TemporaryDirectory '/tmp/tmpu4ei9yc5'>
test_three <TemporaryDirectory '/tmp/tmpu4ei9yc5'>
setup_db <TemporaryDirectory '/tmp/tmpwb18nwil'>
test_three <TemporaryDirectory '/tmp/tmpwb18nwil'>
test_three after
teardown_db <TemporaryDirectory '/tmp/tmpu4ei9yc5'>
teardown_db <TemporaryDirectory '/tmp/tmpwb18nwil'>

10 changes: 5 additions & 5 deletions python/examples/pytest/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
def test_one():
db_server = setup_db_server()
db = setup_db()
print(f" test_one {db}")
print(f" test_one {db}")
assert True
print(" test_one after")
teardown_db(db)
Expand All @@ -12,7 +12,7 @@ def test_one():
def test_two():
db_server = setup_db_server()
db = setup_db()
print(f" test_two {db}")
print(f" test_two {db}")
assert False
print(" test_two after")
teardown_db(db)
Expand All @@ -21,7 +21,7 @@ def test_two():
def test_three():
db_server = setup_db_server()
db = setup_db()
print(f" test_three {db}")
print(f" test_three {db}")
assert True
print(" test_three after")
teardown_db(db)
Expand All @@ -30,12 +30,12 @@ def test_three():
def setup_db():
db = tempfile.TemporaryDirectory()
...
print(f"setup_db {db}")
print(f"setup_db {db}")
return db

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


def setup_db_server():
Expand Down
12 changes: 12 additions & 0 deletions python/pytest-fixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,22 @@ def serve_bolognese(pasta, sauce):
{id: pytest-tmpdir}
{i: tmpdir}

* Probably the simples fixture that PyTest can provide is the `tmpdir`.
* Pytest will prepare a temporary directory and call the test function passing the path to the `tmpdir`.
* PyTest will also clean up the temporary folder, though it will keep the 3 most recent ones. (this is configurable)

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

## Pytest and tempdir
{id: pytest-tempdir}
{i: tmpdir}

* This is a simple application that reads and writes config files (ini file).
* We can test the `parse_file` by preparing some input files and check if we get the expected data structure.
* In order to test the `save_file` we need to be able to save a file somewhere.
* Saving it in the current folder will create garbage files. (and the folder might be read-only in some environments).
* For each test we'll have to come up with a separate filename so they won't collide.
* Using a `tmpdir` solves this problem.

![](examples/pytest/mycfg.py)
![](examples/pytest/a.cfg)
Expand Down

0 comments on commit 541d043

Please sign in to comment.