Skip to content

Commit

Permalink
Fix assertions for raises (#118)
Browse files Browse the repository at this point in the history
* Use a past time in assertion builder test to remove test flakiness.

Add test for assertions on exception type mismatch in raises clause. Fix a minor incorrect traceback.format_exc api usage.

* Rename test for raises.
  • Loading branch information
codito authored and timofurrer committed Jun 22, 2016
1 parent 162b34b commit 3bc2598
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion sure/old.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def raises(self, exc, msg=None):
if not isinstance(e, exc):
raise AssertionError(
'%r should raise %r, but raised %r:\nORIGINAL EXCEPTION:\n\n%s' % (
self._src, exc, e.__class__, traceback.format_exc(e)))
self._src, exc, e.__class__, traceback.format_exc()))

if isinstance(msg, string_types) and msg not in err:
raise AssertionError('''
Expand Down
4 changes: 2 additions & 2 deletions tests/test_assertion_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import mock
from collections import OrderedDict

from datetime import datetime
from datetime import datetime, timedelta
from sure import this, these, those, it, expect, anything, AssertionBuilder
from six import PY3
from sure.compat import compat_repr
Expand All @@ -38,7 +38,7 @@ def test_assertion_builder_synonyms():
def test_4_equal_2p2():
("this(4).should.equal(2 + 2)")

time = datetime.now()
time = datetime.now() - timedelta(0, 60)

assert this(4).should.equal(2 + 2)
assert this(time).should_not.equal(datetime.now())
Expand Down
28 changes: 25 additions & 3 deletions tests/test_old_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,28 @@ def test_that_looks_like():
assert that('String\n with BREAKLINE').looks_like('string with breakline')


def test_that_raises_does_raise_for_exception_type_mismatch():
"that(callable(RuntimeError)).raises(TypeError)"

error = r'.*should raise <class .*FooError.*, but raised <class .*BarError.*'

class FooError(Exception):
pass

class BarError(Exception):
pass

def my_function():
raise BarError('OOps')

try:
assert that(my_function).raises(FooError, 'OOps')
assert False, 'should never reach here'
except AssertionError as e:
import re
assert re.match(error, text_type(e))


def test_that_raises_with_args():
"that(callable, with_args=['foo']).raises(FooError)"

Expand Down Expand Up @@ -895,7 +917,7 @@ def test_depends_on_failing_due_nothing_found():
from sure import action_for, scenario

fullpath = os.path.abspath(__file__).replace('.pyc', '.py')
error = 'the action "lonely_action" defined at %s:904 ' \
error = 'the action "lonely_action" defined at %s:926 ' \
'depends on the attribute "something" to be available in the' \
' context. It turns out that there are no actions providing ' \
'that. Please double-check the implementation' % fullpath
Expand All @@ -921,10 +943,10 @@ def test_depends_on_failing_due_not_calling_a_previous_action():
from sure import action_for, scenario

fullpath = os.path.abspath(__file__).replace('.pyc', '.py')
error = 'the action "my_action" defined at {0}:934 ' \
error = 'the action "my_action" defined at {0}:956 ' \
'depends on the attribute "some_attr" to be available in the context.'\
' You need to call one of the following actions beforehand:\n' \
' -> dependency_action at {0}:930'.replace('{0}', fullpath)
' -> dependency_action at {0}:952'.replace('{0}', fullpath)

def with_setup(context):
@action_for(context, provides=['some_attr'])
Expand Down

0 comments on commit 3bc2598

Please sign in to comment.