Skip to content

Commit c0e2959

Browse files
Update content
1 parent 42b14e1 commit c0e2959

File tree

1 file changed

+10
-9
lines changed
  • guide/getting-started-with-testcontainers-for-python

1 file changed

+10
-9
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ and also we can use different versions of the same package in different projects
3636

3737
[source,shell]
3838
----
39-
mkdir tc-python-demo
40-
cd tc-python-demo
41-
python3 -m venv venv
42-
source venv/bin/activate
39+
$ mkdir tc-python-demo
40+
$ cd tc-python-demo
41+
$ python3 -m venv venv
42+
$ source venv/bin/activate
4343
----
4444

4545
We are going to use https://www.psycopg.org/psycopg3/[psycopg3] for talking to the Postgres database,
@@ -81,7 +81,7 @@ Now, let's add *create_table()* function in *customers/customers.py* file to cre
8181

8282
[source,python]
8383
----
84-
include::{codebase}/customers/customers.py[lines="1..3,4..24"]
84+
include::{codebase}/customers/customers.py[lines="1..3,14..24"]
8585
----
8686

8787
We have obtained a new database connection using *get_connection()* function and created the *customers* table.
@@ -103,7 +103,7 @@ To keep it simple for the purpose of this guide, we are creating a new connectio
103103
In a real-world application, it is recommended to use a connection pool to reuse connections.
104104

105105
== Write tests using Testcontainers
106-
We will create a PostgreSQL database in a container using Testcontainers and use the same database for all the tests.
106+
We will create an instance of PostgreSQL database container using Testcontainers and use the same database for all the tests.
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.
@@ -140,9 +140,8 @@ Let's create *tests/test_customers.py* file and implement the fixtures as follow
140140
include::{codebase}/tests/test_customers.py[lines="1..30"]
141141
----
142142

143-
We have used *module* scoped fixture to create a PostgreSQL container using Testcontainers.
144-
In the *setup()* fixture function, all the statements before *yield* will be executed before running any test.
145-
All the statements after *yield* will be executed after running all the tests in the module.
143+
We have used *module* scoped fixture to start a PostgreSQL container using Testcontainers so that it will only run once for all the tests in the module.
144+
In the *setup()* fixture function, we are starting the PostgreSQL container and creating the *customers* table. We have added a finalizer to remove the container at the end of all the tests.
146145

147146
In the *setup_data()* fixture function, we are deleting all the records in the *customers* table.
148147
This is a *function* scoped fixture, which will be executed before running every test.
@@ -187,6 +186,8 @@ tests/test_customers.py .. [100%]
187186
============== 2 passed in 3.02s =================
188187
----
189188

189+
The tests are executed using a real PostgreSQL database instead of mocks which gives more confidence in our implementation.
190+
190191
== Conclusion
191192

192193
We have explored how to use *testcontainers-python* library for testing a Python application using a PostgreSQL database.

0 commit comments

Comments
 (0)