diff --git a/guide/getting-started-with-testcontainers-for-python/index.adoc b/guide/getting-started-with-testcontainers-for-python/index.adoc index 42aad35..6556a19 100644 --- a/guide/getting-started-with-testcontainers-for-python/index.adoc +++ b/guide/getting-started-with-testcontainers-for-python/index.adoc @@ -36,10 +36,10 @@ and also we can use different versions of the same package in different projects [source,shell] ---- -mkdir tc-python-demo -cd tc-python-demo -python3 -m venv venv -source venv/bin/activate +$ mkdir tc-python-demo +$ cd tc-python-demo +$ python3 -m venv venv +$ source venv/bin/activate ---- 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 [source,python] ---- -include::{codebase}/customers/customers.py[lines="1..3,4..24"] +include::{codebase}/customers/customers.py[lines="1..3,14..24"] ---- 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 In a real-world application, it is recommended to use a connection pool to reuse connections. == Write tests using Testcontainers -We will create a PostgreSQL database in a container using Testcontainers and use the same database for all the tests. +We will create an instance of PostgreSQL database container using Testcontainers and use the same database for all the tests. Also, we will delete all the customer records before every test so that our tests will run with a clean database. 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 include::{codebase}/tests/test_customers.py[lines="1..30"] ---- -We have used *module* scoped fixture to create a PostgreSQL container using Testcontainers. -In the *setup()* fixture function, all the statements before *yield* will be executed before running any test. -All the statements after *yield* will be executed after running all the tests in the module. +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. +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. In the *setup_data()* fixture function, we are deleting all the records in the *customers* table. This is a *function* scoped fixture, which will be executed before running every test. @@ -187,6 +186,8 @@ tests/test_customers.py .. [100%] ============== 2 passed in 3.02s ================= ---- +The tests are executed using a real PostgreSQL database instead of mocks which gives more confidence in our implementation. + == Conclusion We have explored how to use *testcontainers-python* library for testing a Python application using a PostgreSQL database.