Skip to content

Commit 42b14e1

Browse files
Update content
1 parent d2916d9 commit 42b14e1

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

guide/getting-started-with-testcontainers-for-python/index.adoc

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,37 @@ We will create a PostgreSQL database in a container using Testcontainers and use
107107
Also, we will delete all the customer records before every test so that our tests will run with a clean database.
108108

109109
We are going to use pytest fixtures for implementing the setup and teardown logic.
110+
A recommended approach to implement the setup and teardown logic is to use https://pytest.org/en/7.4.x/how-to/fixtures.html#yield-fixtures-recommended[yield fixtures].
111+
112+
[source,python]
113+
----
114+
@pytest.fixture
115+
def setup():
116+
# setup code
117+
yield some_value
118+
# teardown code
119+
----
120+
121+
However, with this approach, if there is an exception occurred in the setup code, the teardown code will not be executed. So, a better approach is to use https://pytest.org/en/7.4.x/how-to/fixtures.html#adding-finalizers-directly[finalizers] as follows:
122+
123+
[source,python]
124+
----
125+
@pytest.fixture
126+
def setup(request):
127+
# setup code
128+
129+
def cleanup():
130+
# teardown code
131+
132+
request.addfinalizer(cleanup)
133+
return some_value
134+
----
110135

111136
Let's create *tests/test_customers.py* file and implement the fixtures as follows:
112137

113138
[source,python]
114139
----
115-
include::{codebase}/tests/test_customers.py[lines="1..26"]
140+
include::{codebase}/tests/test_customers.py[lines="1..30"]
116141
----
117142

118143
We have used *module* scoped fixture to create a PostgreSQL container using Testcontainers.
@@ -126,7 +151,7 @@ Now let's implement the tests as follows:
126151

127152
[source,python]
128153
----
129-
include::{codebase}/tests/test_customers.py[lines="28..40"]
154+
include::{codebase}/tests/test_customers.py[lines="32..44"]
130155
----
131156

132157
* In the *test_get_all_customers()* test, we are inserting two customer records into the database,
@@ -152,14 +177,14 @@ You should see the following output:
152177
[source,shell]
153178
----
154179
pytest
155-
=========== test session starts ==============
180+
=============== test session starts ==============
156181
platform darwin -- Python 3.12.0, pytest-7.4.3, pluggy-1.3.0
157182
rootdir: /Users/siva/dev/tc-python-demo
158183
collected 2 items
159184
160185
tests/test_customers.py .. [100%]
161186
162-
========= 2 passed in 3.02s =================
187+
============== 2 passed in 3.02s =================
163188
----
164189

165190
== Conclusion

tests/test_customers.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@
88

99

1010
@pytest.fixture(scope="module", autouse=True)
11-
def setup():
11+
def setup(request):
1212
postgres.start()
13+
14+
def remove_container():
15+
postgres.stop()
16+
17+
request.addfinalizer(remove_container)
18+
os.environ["DB_CONN"] = postgres.get_connection_url()
1319
os.environ["DB_HOST"] = postgres.get_container_host_ip()
1420
os.environ["DB_PORT"] = postgres.get_exposed_port(5432)
1521
os.environ["DB_USERNAME"] = postgres.POSTGRES_USER
1622
os.environ["DB_PASSWORD"] = postgres.POSTGRES_PASSWORD
1723
os.environ["DB_NAME"] = postgres.POSTGRES_DB
1824
customers.create_table()
19-
yield
20-
postgres.stop()
2125

2226

2327
@pytest.fixture(scope="function", autouse=True)

0 commit comments

Comments
 (0)