Skip to content

Commit

Permalink
Merge pull request #151 from twosigma/issue-128
Browse files Browse the repository at this point in the history
handle assertion failures in setUp()
  • Loading branch information
leifwalsh authored Dec 21, 2023
2 parents c49d499 + baa413b commit 6bb9bbe
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Changelog
=========

* :bug:`128` Properly handle assertion failures during setUp/tearDown
* :support:`145` Support Python 3.12 (and drop 3.8)
* :release:`0.12.2 <2023-12-20>`
* :support:`144` Support Python 3.11
Expand Down
3 changes: 2 additions & 1 deletion marbles/core/marbles/core/_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def get_stack_info():
# because we always find a test.
for frame, _ in stack: # pragma: no branch
code = frame.f_code
if code.co_name.startswith('test_'):
func_name = code.co_name
if func_name.startswith('test_') or func_name in {'setUp', 'tearDown'}:
return (frame.f_locals.copy(), frame.f_globals['__name__'],
code.co_filename, frame.f_lineno)
22 changes: 22 additions & 0 deletions marbles/core/tests/examples/example_marbles.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,27 @@ def test_create_resource(self):
201)


class SetupFailureTestCase(marbles.core.TestCase):
'''Test when setUp() contains failing assertions.'''

def setUp(self):
local_var = 1
self.assertEqual(local_var, 2, 'oh no')

def test_foo(self):
self.assertTrue(True, 'yay')


class TeardownFailureTestCase(marbles.core.TestCase):
'''Test when tearDown() contains failing assertions.'''

def tearDown(self):
local_var = 1
self.assertEqual(local_var, 2, 'oh no')

def test_foo(self):
self.assertTrue(True, 'yay')


if __name__ == '__main__':
marbles.core.main()
22 changes: 22 additions & 0 deletions marbles/core/tests/examples/example_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,27 @@ def test_create_resource(self):
201)


class SetupFailureTestCase(unittest.TestCase):
'''Test when setUp() contains failing assertions.'''

def setUp(self):
local_var = 1
self.assertEqual(local_var, 2, 'oh no')

def test_foo(self):
self.assertTrue(True, 'yay')


class TeardownFailureTestCase(unittest.TestCase):
'''Test when tearDown() contains failing assertions.'''

def tearDown(self):
local_var = 1
self.assertEqual(local_var, 2, 'oh no')

def test_foo(self):
self.assertTrue(True, 'yay')


if __name__ == '__main__':
unittest.main()
9 changes: 9 additions & 0 deletions marbles/core/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ def test_show_locals(self):
self.assertNotIn('endpoint', self.stderr)
self.assertNotIn('example.com', self.stderr)

def test_stack_info_handles_setup_failure(self):
'''Our stack_info() handles setup failures without crashing.'''
self.assertNotIn('_stack.get_stack_info', self.stderr)

def test_setup_failure_shows_locals(self):
'''Setup failures should show locals when running with marbles.'''
if self.run_with_marbles:
self.assertRegex(self.stderr, r'Locals:\r?\n.*local_var = 1')


class MainWithErrorTestCase(TestScriptRunningTestCase):
'''Test how marbles.core.main and unittest.main handle test errors.'''
Expand Down

0 comments on commit 6bb9bbe

Please sign in to comment.