Skip to content

Commit

Permalink
Add pytest support
Browse files Browse the repository at this point in the history
  • Loading branch information
gaogaotiantian committed Apr 24, 2024
1 parent 0d66f02 commit 8056e86
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ coredumpy saves your crash site so you can better debug your python program.

In most cases, you only need to hook `coredumpy` to some triggers

For `Exception` and `unittest`, patch with a simple line

```python
import coredumpy
# Create a dump in "./dumps" when there's an unhandled exception
Expand All @@ -25,6 +27,12 @@ coredumpy.patch_except(directory='./dumps')
coredumpy.patch_unittest(directory='./dumps')
```

For `pytest`, you can use `coredumpy` as a plugin

```
pytest --enable-coredumpy --coredumpy-dir ./dumps
```

Or you can dump the current frame stack manually

```python
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@ Homepage = "https://github.com/gaogaotiantian/coredumpy"
[project.scripts]
coredumpy = "coredumpy:main"

[project.entry-points.pytest11]
coredumpy = "coredumpy:pytest_hook"

[tool.setuptools.dynamic]
version = {attr = "coredumpy.__version__"}
3 changes: 3 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ twine
flake8
mypy
coverage

# Test
pytest
2 changes: 2 additions & 0 deletions src/coredumpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

__version__ = "0.0.1"

import coredumpy.pytest_hook as pytest_hook
from .coredumpy import Coredumpy, dump, load
from .except_hook import patch_except
from .main import main
Expand All @@ -19,4 +20,5 @@
"main",
"patch_except",
"patch_unittest",
"pytest_hook",
]
31 changes: 31 additions & 0 deletions src/coredumpy/pytest_hook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import coredumpy


def pytest_addoption(parser):
parser.addoption(
"--enable-coredumpy",
action="store_true",
help="Enable coredumpy plugin.",
)

parser.addoption(
"--coredumpy-dir",
action="store",
default="./coredumpy",
help="The directory to store the core dump files.",
)


def pytest_exception_interact(node, call, report):
if not node.config.getoption("--enable-coredumpy"):
return

import pytest
if isinstance(report, pytest.TestReport):
try:
tb = call.excinfo.tb
while tb.tb_next:
tb = tb.tb_next
coredumpy.dump(tb.tb_frame, directory=node.config.getoption("--coredumpy-dir"))
except Exception:
pass
43 changes: 43 additions & 0 deletions tests/test_pytest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/gaogaotiantian/coredumpy/blob/master/NOTICE.txt


import os
import tempfile
import textwrap

from .base import TestBase


class TestPytest(TestBase):
def test_pytest(self):
with tempfile.TemporaryDirectory() as tmpdir:
test = textwrap.dedent(f"""
import pytest
import os
def test_for_pytest_equal():
assert 1 == 2
def test_for_pytest_greater():
assert 1 > 2
""")
with open(os.path.join(tmpdir, "test.py"), "w") as f:
f.write(test)

test_path = os.path.join(tmpdir, "test.py")
dump_path = os.path.join(tmpdir, "dump")

script = f"""
import pytest
pytest.main(["--enable-coredumpy", "--coredumpy-dir", {repr(dump_path)}, {repr(test_path)}])
"""
self.run_script(script)
self.assertEqual(len(os.listdir(dump_path)), 2)

# Without the enable, it should not produce dumps
script = f"""
import pytest
pytest.main(["--coredumpy-dir", {repr(dump_path)}, {repr(test_path)}])
"""
self.run_script(script)
self.assertEqual(len(os.listdir(dump_path)), 2)

0 comments on commit 8056e86

Please sign in to comment.