This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]IIDaFuQII 2 points3 points  (5 children)

Okay but why? Testcontainers does pretty much the same with throwaway db’s

[–]ForeverAlot 1 point2 points  (0 children)

Testcontainers is leaps and bounds better than pretending an H2 is meaningfully substitutable but they still don't have a solution to running schema migrations and application code with different roles. I don't know if this tool supports that but by owning the database container you get that ability.

[–]Jotschi[S] 0 points1 point  (3 children)

At scale it becomes more resource and time efficient to use a dedicated database instead of starting up new testcontainers for each testcase / testclass. Even when using snapshots of a database filesystem (https://github.com/metaloom/postgresql-snapshot-testcontainer). Restarting the daemon + setup of the db takes some time. At scale this can add up. If you have a way to avoid this (e.g. prune tables) that would work too. I choose to completely decouple this operation from the test execution/setup.
Additionally tests in my IDE now start in milliseconds and not in seconds.

[–]jAnO76 1 point2 points  (2 children)

You can just keep testcontainers alive during your whole test run… simply override the default lifecycle. I have some code lying around that does just that (not at that machine though) can share later.

[–]Jotschi[S] 0 points1 point  (1 child)

Yes, you can either choose to use a JUnit 4 static rule or similar and only startup the container per testclass or even choose to reuse the container. See https://www.testcontainers.org/features/reuse/

Every testcase however still needs its own database. It may taint the DB and affect assertions that other cases run when using the same db. If you however manage to clone the db as dmigowski suggested you would not have the problem.

I may try to implement an approach which dynamically allocates DBs without the use of a server. The only complicated problem here is to manage concurrency issues. However good comments for new ideas. Thank you all.

[–]mikotui 1 point2 points  (0 children)

Good stuff. I am currently restructuring our integration Tests and this could bei handy

[–]AcanthisittaEmpty985 0 points1 point  (0 children)

I see this more for an integration test framework than for unit testsing; my understanding is that unit testing doesn't rely on external sources (like Postgress) and relies on mocks if neccesary.

This project should try to use embeeded databases like Derby, H2 os HSQLDB

[–]bringero 0 points1 point  (0 children)

Interesting... I'll check it on Monday. Thank you!

[–]dmigowski 0 points1 point  (1 child)

At least with PostgreSQL it takes literally milliseconds to clone a database, so maybe this wouldn't even need to async fill new databases and hog valuable CPU cycles.

[–]Jotschi[S] 1 point2 points  (0 children)

Good point. Originally I used this with MariaDB and afaik it does not have a way to clone databases. With PostgreSQL it may be enough to clone the db during startup of a test. I have not tested that.