-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unittest support and fix directory (#11)
- Loading branch information
1 parent
7b05d83
commit 343e130
Showing
10 changed files
with
161 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# 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 unittest | ||
from typing import Callable, Optional, Union | ||
|
||
from .coredumpy import dump | ||
|
||
|
||
def patch_unittest(path: Optional[Union[str, Callable[[], str]]] = None, | ||
directory: Optional[str] = None): | ||
""" Patch unittest to coredump when a test fails/raises an exception. | ||
@param path: | ||
The path to save the dump file. It could be a string or a callable that returns a string. | ||
if not specified, the default filename will be used | ||
@param directory: | ||
The directory to save the dump file, only works when path is not specified. | ||
""" | ||
|
||
_original_addError = unittest.TestResult.addError | ||
_original_addFailure = unittest.TestResult.addFailure | ||
|
||
def addError(self, test, err): | ||
tb = err[2] | ||
while tb.tb_next: | ||
tb = tb.tb_next | ||
try: | ||
filename = dump(tb.tb_frame, path=path, directory=directory) | ||
print(f'Your frame stack has been dumped to "{filename}", ' | ||
f'open it with\ncoredumpy load {filename}') | ||
except Exception: # pragma: no cover | ||
pass | ||
_original_addError(self, test, err) | ||
|
||
def addFailure(self, test, err): | ||
tb = err[2] | ||
while tb.tb_next: | ||
tb = tb.tb_next | ||
try: | ||
filename = dump(tb.tb_frame, path=path, directory=directory) | ||
print(f'Your frame stack has been dumped to "{filename}", ' | ||
f'open it with\ncoredumpy load {filename}') | ||
except Exception: # pragma: no cover | ||
pass | ||
_original_addFailure(self, test, err) | ||
|
||
unittest.TestResult.addError = addError # type: ignore | ||
unittest.TestResult.addFailure = addFailure # type: ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# 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 | ||
|
||
|
||
from .base import TestBase | ||
|
||
|
||
class TestUnittest(TestBase): | ||
def test_unittest_basic(self): | ||
with tempfile.TemporaryDirectory() as tempdir: | ||
script = f""" | ||
import unittest | ||
from coredumpy import patch_unittest | ||
patch_unittest(directory={repr(tempdir)}) | ||
class TestUnittest(unittest.TestCase): | ||
def test_bool(self): | ||
self.assertTrue(False) | ||
def test_eq(self): | ||
self.assertEqual(1, 2) | ||
def test_pass(self): | ||
self.assertEqual(1, 1) | ||
def test_error(self): | ||
raise ValueError() | ||
unittest.main() | ||
""" | ||
stdout, stderr = self.run_script(script, expected_returncode=1) | ||
self.assertIn("FAIL: test_bool", stderr) | ||
self.assertIn("FAIL: test_eq", stderr) | ||
self.assertIn("ERROR: test_error", stderr) | ||
self.assertNotIn("test_pass", stderr) | ||
self.assertEqual(stdout.count(tempdir), 6) | ||
self.assertEqual(len(os.listdir(tempdir)), 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters