Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix --most-allocations with the value of 0 #94

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/pytest_memray/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ def pytest_terminal_summary(
)

max_results = cast(int, value_or_ini(self.config, "most_allocations"))
if max_results == 0:
max_results = len(total_sizes)

for test_id, total_size in total_sizes.most_common(max_results):
result = self.results[test_id]
Expand Down
2 changes: 1 addition & 1 deletion src/pytest_memray/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def parse_memory_string(mem_str: str) -> float:

def value_or_ini(config: Config, key: str) -> object:
value = config.getvalue(key)
if value:
if value is not None:
return value
try:
return config.getini(key)
Expand Down
33 changes: 33 additions & 0 deletions tests/test_pytest_memray.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,39 @@ def test_bar():
assert "results for test_memray_report_limit.py::test_bar" in output


def test_memray_report_limit_without_limit(pytester: Pytester) -> None:
pytester.makepyfile(
"""
import pytest
from memray._test import MemoryAllocator
allocator = MemoryAllocator()

def allocating_func1():
allocator.valloc(1024)
allocator.free()

def allocating_func2():
allocator.valloc(1024*2)
allocator.free()

def test_foo():
allocating_func1()

def test_bar():
allocating_func2()
"""
)

result = pytester.runpytest("--memray", "--most-allocations=0")

assert result.ret == ExitCode.OK

output = result.stdout.str()

assert "results for test_memray_report_limit_without_limit.py::test_foo" in output
assert "results for test_memray_report_limit_without_limit.py::test_bar" in output


def test_failing_tests_are_not_reported(pytester: Pytester) -> None:
pytester.makepyfile(
"""
Expand Down