From 42b14e1deef99c2c262eadbf0bc3fd21cb019e0e Mon Sep 17 00:00:00 2001 From: K Siva Prasad Reddy Date: Wed, 6 Dec 2023 09:12:16 +0530 Subject: [PATCH] Update content --- .../index.adoc | 33 ++++++++++++++++--- tests/test_customers.py | 10 ++++-- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/guide/getting-started-with-testcontainers-for-python/index.adoc b/guide/getting-started-with-testcontainers-for-python/index.adoc index 65eb030..42aad35 100644 --- a/guide/getting-started-with-testcontainers-for-python/index.adoc +++ b/guide/getting-started-with-testcontainers-for-python/index.adoc @@ -107,12 +107,37 @@ We will create a PostgreSQL database in a container using Testcontainers and use 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. +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]. + +[source,python] +---- +@pytest.fixture +def setup(): + # setup code + yield some_value + # teardown code +---- + +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: + +[source,python] +---- +@pytest.fixture +def setup(request): + # setup code + + def cleanup(): + # teardown code + + request.addfinalizer(cleanup) + return some_value +---- Let's create *tests/test_customers.py* file and implement the fixtures as follows: [source,python] ---- -include::{codebase}/tests/test_customers.py[lines="1..26"] +include::{codebase}/tests/test_customers.py[lines="1..30"] ---- 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: [source,python] ---- -include::{codebase}/tests/test_customers.py[lines="28..40"] +include::{codebase}/tests/test_customers.py[lines="32..44"] ---- * 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: [source,shell] ---- pytest -=========== test session starts ============== +=============== test session starts ============== platform darwin -- Python 3.12.0, pytest-7.4.3, pluggy-1.3.0 rootdir: /Users/siva/dev/tc-python-demo collected 2 items tests/test_customers.py .. [100%] -========= 2 passed in 3.02s ================= +============== 2 passed in 3.02s ================= ---- == Conclusion diff --git a/tests/test_customers.py b/tests/test_customers.py index 5c2e6e2..11f159f 100644 --- a/tests/test_customers.py +++ b/tests/test_customers.py @@ -8,16 +8,20 @@ @pytest.fixture(scope="module", autouse=True) -def setup(): +def setup(request): postgres.start() + + def remove_container(): + postgres.stop() + + request.addfinalizer(remove_container) + os.environ["DB_CONN"] = postgres.get_connection_url() os.environ["DB_HOST"] = postgres.get_container_host_ip() os.environ["DB_PORT"] = postgres.get_exposed_port(5432) os.environ["DB_USERNAME"] = postgres.POSTGRES_USER os.environ["DB_PASSWORD"] = postgres.POSTGRES_PASSWORD os.environ["DB_NAME"] = postgres.POSTGRES_DB customers.create_table() - yield - postgres.stop() @pytest.fixture(scope="function", autouse=True)