From db1482a8d0fed3a93ea48b5b8ec3c5edacc04396 Mon Sep 17 00:00:00 2001 From: Michael Dubner Date: Thu, 19 Jun 2025 13:12:14 +0300 Subject: [PATCH] Closes #53: Adds --container-tail option and parameters to wrappers.Container.logs() --- pytest_docker_tools/plugin.py | 10 +++++++++- pytest_docker_tools/wrappers/container.py | 4 ++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/pytest_docker_tools/plugin.py b/pytest_docker_tools/plugin.py index d94142e..93618dc 100644 --- a/pytest_docker_tools/plugin.py +++ b/pytest_docker_tools/plugin.py @@ -31,6 +31,8 @@ def pytest_runtest_makereport(item, call): if "request" not in item.funcargs: return + container_tail = item.funcargs["request"].config.option.container_tail + container_tail = int(container_tail) if container_tail.isdigit() else container_tail for name, fixturedef in item.funcargs["request"]._fixture_defs.items(): if not hasattr(fixturedef, "cached_result") or not fixturedef.cached_result: continue @@ -39,7 +41,7 @@ def pytest_runtest_makereport(item, call): rep.sections.append( ( name + ": " + fixture.name, - fixture.logs(), + fixture.logs(tail=container_tail), ) ) @@ -53,3 +55,9 @@ def pytest_addoption(parser): help="reuse existing containers instead of always creating new ones. Requires the 'name' attribute to be set" "on container definition", ) + group.addoption( + "--container-tail", + dest="container_tail", + default="all", + help="limit logs of containers", + ) diff --git a/pytest_docker_tools/wrappers/container.py b/pytest_docker_tools/wrappers/container.py index a615bc3..f4085e0 100644 --- a/pytest_docker_tools/wrappers/container.py +++ b/pytest_docker_tools/wrappers/container.py @@ -157,8 +157,8 @@ def remove(self, *args, **kwargs): "Do not remove this container manually. It will be removed automatically by py.test after the test finishes." ) - def logs(self): - return self._container.logs().decode("utf-8") + def logs(self, **kwargs): + return self._container.logs(**kwargs).decode("utf-8") def get_files(self, path): """