Skip to content

Commit

Permalink
Update content
Browse files Browse the repository at this point in the history
  • Loading branch information
sivaprasadreddy committed Dec 6, 2023
1 parent d2916d9 commit 42b14e1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
33 changes: 29 additions & 4 deletions guide/getting-started-with-testcontainers-for-python/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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
Expand Down
10 changes: 7 additions & 3 deletions tests/test_customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 42b14e1

Please sign in to comment.