Last active
February 3, 2016 23:29
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ---------------------------------- | |
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ---------------------------------- | |
# 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 |
$ nosetests
> test description (' ') ... FAILED
> test description ('') ... FAILED
> test description (None) ... passed
> sss.TestArgs ... passed
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
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.