public
Last active

Python Scenario Testing Prototype

  • Download Gist
scerariotest.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
"""
(MIT License)
 
Copyright (c) 2011 Jason Webb
 
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
 
import unittest
 
## Scenario Support ##
 
class ScenarioMeta(type):
def __new__(cls, name, bases, attrs):
new_attrs = {}
for name, val in filter(lambda n: isinstance(n[1], ScenerioTestMeta), attrs.iteritems()):
for id, params in enumerate(val.scenarios if not callable(val.scenarios) else val.scenarios()):
if type(params) in (tuple, list):
params, id = params
# create a unittest discoverable name
test_name = "test_%s_%s" % (val.__name__, id)
# Wrap the scenario in a closure and assign the discoverable
# name.
new_attrs[test_name] = cls._wrap_test(params, val.__test__)
attrs.update(new_attrs)
return super(ScenarioMeta, cls).__new__(cls, name, bases, attrs)
 
@staticmethod
def _wrap_test(kwargs, meth):
def wrapper(self):
meth(self, **kwargs)
return wrapper
 
class ScenerioTestMeta(type):
def __new__(cls, name, bases, attrs):
test_meth = attrs.pop("__test__", None)
if test_meth:
# Now that the __test__ method is pulled off the base it can be
# wrapped as static and rebound. This allows it to be re-composed
# to the parent test case.
attrs["__test__"] = staticmethod(test_meth)
return super(ScenerioTestMeta, cls).__new__(cls, name, bases, attrs)
 
class ScenerioTest(object):
__metaclass__ = ScenerioTestMeta
scenarios = ()
 
## Unit of code to test ##
 
def is_numeric(value_in):
try:
float(value_in)
return True
except Exception:
return False
 
## Test Case ##
 
class TestIsNumeric(unittest.TestCase):
__metaclass__ = ScenarioMeta
class is_numeric_basic(ScenerioTest):
scenarios = [
dict(val="1", expected=True),
dict(val="-1", expected=True),
dict(val=unicode("123" * 9999), expected=True),
dict(val="Bad String", expected=False),
dict(val="Speaks Volumes", expected=False)
]
scenarios += [(dict(val=unicode(x), expected=True),
"check_unicode_%s" % x) for x in range(-2, 3)]
def __test__(self, val, expected):
actual = is_numeric(val)
if expected:
self.assertTrue(actual)
else:
self.assertFalse(actual)
 
 
if __name__ == "__main__":
unittest.main()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.