Skip to content

Commit

Permalink
Copied assertion methods, for less/greater checks, from Python 2.7’s …
Browse files Browse the repository at this point in the history
…unittest.TestCase to work with 2.6
  • Loading branch information
heyman committed Sep 7, 2014
1 parent f05ff7f commit e18bf5c
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions locust/test/testcases.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@
from locust.stats import global_stats
from flask import Flask, request, redirect, make_response, send_file, Response, stream_with_context


def safe_repr(obj, short=False):
"""
Function from python 2.7's unittest.util. Used in methods that is copied
from 2.7's unittest.TestCase to work in python 2.6.
"""
_MAX_LENGTH = 80
try:
result = repr(obj)
except Exception:
result = object.__repr__(obj)
if not short or len(result) < _MAX_LENGTH:
return result
return result[:_MAX_LENGTH] + ' [truncated]...'



app = Flask(__name__)

@app.route("/ultra_fast")
Expand Down Expand Up @@ -103,21 +120,34 @@ def assertIn(self, member, container, msg=None):
Just like self.assertTrue(a in b), but with a nicer default message.
Implemented here to work with Python 2.6
"""

_MAX_LENGTH = 80
def safe_repr(obj, short=False):
try:
result = repr(obj)
except Exception:
result = object.__repr__(obj)
if not short or len(result) < _MAX_LENGTH:
return result
return result[:_MAX_LENGTH] + ' [truncated]...'

if member not in container:
standardMsg = '%s not found in %s' % (safe_repr(member),
safe_repr(container))
self.fail(self._formatMessage(msg, standardMsg))

def assertLess(self, a, b, msg=None):
"""Just like self.assertTrue(a < b), but with a nicer default message."""
if not a < b:
standardMsg = '%s not less than %s' % (safe_repr(a), safe_repr(b))
self.fail(self._formatMessage(msg, standardMsg))

def assertLessEqual(self, a, b, msg=None):
"""Just like self.assertTrue(a <= b), but with a nicer default message."""
if not a <= b:
standardMsg = '%s not less than or equal to %s' % (safe_repr(a), safe_repr(b))
self.fail(self._formatMessage(msg, standardMsg))

def assertGreater(self, a, b, msg=None):
"""Just like self.assertTrue(a > b), but with a nicer default message."""
if not a > b:
standardMsg = '%s not greater than %s' % (safe_repr(a), safe_repr(b))
self.fail(self._formatMessage(msg, standardMsg))

def assertGreaterEqual(self, a, b, msg=None):
"""Just like self.assertTrue(a >= b), but with a nicer default message."""
if not a >= b:
standardMsg = '%s not greater than or equal to %s' % (safe_repr(a), safe_repr(b))
self.fail(self._formatMessage(msg, standardMsg))


class WebserverTestCase(LocustTestCase):
Expand Down

0 comments on commit e18bf5c

Please sign in to comment.