Skip to content

Instantly share code, notes, and snippets.

@maxkoryukov
Last active February 3, 2016 23:29
Show Gist options
  • Save maxkoryukov/8a26915ee5983af20c65 to your computer and use it in GitHub Desktop.
Save maxkoryukov/8a26915ee5983af20c65 to your computer and use it in GitHub Desktop.
Generates tests for python's unittest module (native mod couldn't generate them in easy way). Works good with unittest and nosetests
# ----------------------------------
# USAGE
# ----------------------------------
from unittest import TestCase
from testargs import TestArgs
class SampleTest(TestCase):
@TestArgs(
(None),
(''),
(' '),
)
def test_test(self, sample_arg1):
""" test description """
self.assertIsNone(sample_arg1)
# ----------------------------------
# thanks to : Xavier Decoret, http://stackoverflow.com/a/4455312/1115187
# ----------------------------------
import sys
def TestArgs(*parameters):
def tuplify(x):
if not isinstance(x, tuple):
return (x,)
return x
def decorator(method, parameters=parameters):
for parameter in (tuplify(x) for x in parameters):
def method_for_parameter(self, method=method, parameter=parameter):
method(self, *parameter)
args_for_parameter = ",".join(repr(v) for v in parameter)
name_for_parameter = method.__name__ + "(" + args_for_parameter + ")"
frame = sys._getframe(1) # pylint: disable-msg=W0212
frame.f_locals[name_for_parameter] = method_for_parameter
frame.f_locals[name_for_parameter].__doc__ = method.__doc__ + '(' + args_for_parameter + ')'
method_for_parameter.__name__ = name_for_parameter + '(' + args_for_parameter + ')'
return None
return decorator
@maxkoryukov
Copy link
Author

It turned out, that the UnitTests in python (at least python2) is a bullshit. It is not cool, when writing the test is more difficult than write the code.

@maxkoryukov
Copy link
Author

$  nosetests
> test description ('      ') ... FAILED
> test description ('') ... FAILED
> test description (None) ... passed
> sss.TestArgs ... passed

@maxkoryukov
Copy link
Author

thanks to : Xavier Decoret, http://stackoverflow.com/a/4455312/1115187

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment