Skip to content

Auto-label containers with test class and field in @Testcontainers extension#11918

Closed
lekhrocks wants to merge 1 commit into
testcontainers:mainfrom
lekhrocks:feature/auto-label-containers
Closed

Auto-label containers with test class and field in @Testcontainers extension#11918
lekhrocks wants to merge 1 commit into
testcontainers:mainfrom
lekhrocks:feature/auto-label-containers

Conversation

@lekhrocks

@lekhrocks lekhrocks commented Jul 8, 2026

Copy link
Copy Markdown

Closes #11919

Before starting a container via @Testcontainers, the JUnit extension now sets two identifying labels on the container:

testcontainers/test-class  -> "com.example.MyPostgresTest"
testcontainers/test-field  -> "postgresContainer"

This lets users identify which container belongs to which test at a glance:

docker ps --filter label=testcontainers/test-class=com.example.MyPostgresTest

And trace CI failures back to the owning test from container metadata.

…tension

Before starting a container via @testcontainers, the JUnit extension now
sets two identifying labels on the container:
```
testcontainers/test-class  -> "com.example.MyPostgresTest"
testcontainers/test-field  -> "postgresContainer"
```

This lets users identify which container belongs to which test at a glance:

```
docker ps --filter label=testcontainers/test-class=com.example.MyPostgresTest
```
And trace CI failures back to the owning test from container metadata.
@lekhrocks lekhrocks requested a review from a team as a code owner July 8, 2026 06:38
@eddumelendez

Copy link
Copy Markdown
Member

Can we please discuss the ideas before raising a PR?

@lekhrocks

lekhrocks commented Jul 8, 2026

Copy link
Copy Markdown
Author

Can we please discuss the ideas before raising a PR?

@eddumelendez

Makes sense! I should have started with a discussion first. I've opened one here: #11919. Happy to iterate on the approach based on maintainer feedback before reopening the PR.

@eddumelendez

Copy link
Copy Markdown
Member

Thanks for your contribution. Two things:

  1. Testcontainers uses org.testcontainers prefix for labels and those are immutable. I think this can be addressed.
  2. There will be an inconsistency about how containers are created using JUnit integration.

I think this is useful information, in the past thought about it as well but didn't dig deeper into the second item. I also thought about an specific section in the JUnit report.

For now, I would avoid having different ways to create a container.

@lekhrocks

Copy link
Copy Markdown
Author

Thanks for your contribution. Two things:

  1. Testcontainers uses org.testcontainers prefix for labels and those are immutable. I think this can be addressed.
  2. There will be an inconsistency about how containers are created using JUnit integration.

I think this is useful information, in the past thought about it as well but didn't dig deeper into the second item. I also thought about an specific section in the JUnit report.

For now, I would avoid having different ways to create a container.

@eddumelendez
Thanks for the thoughtful review, both points make sense.

  1. The label prefix is easy to fix - org.testcontainers is the right namespace.
  2. The inconsistency between container creation paths is a real problem I hadn't fully accounted for. Labels on containers started via @Testcontainers but not on manually created containers would be confusing.

I think a better approach would be something in the JUnit report layer, a test execution listener that captures container IDs and maps them to test class/method in the report output. That way it works uniformly regardless of how containers are created. I'd be happy to explore that direction in a future contribution.

Thanks again for the feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants