Created
November 28, 2012 11:21
-
-
Save johannth/4160572 to your computer and use it in GitHub Desktop.
A more pythonic unittest.TestCase that follows PEP 8 conventions
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
A more pythonic unittest TestCase that allows you to follow PEP8 | |
conventions and use self.assert_equal(A, B) instead of self.assertEqual(A, B) | |
""" | |
import unittest | |
def camel_case(pythonic_attribute_name): | |
""" | |
Input: A pythonic attribute_name, i.e. "assert_equal". | |
Returns: A non-pythonic camel cased attribute name "assertEqual". | |
""" | |
words = pythonic_attribute_name.split("_") | |
return "".join(w.title() if i > 0 else w for i, w in enumerate(words)) | |
class PEP8TestCase(unittest.TestCase): | |
""" | |
A replacement for unittest.TestCase that allows you to use | |
self.assert_* instead of the non PEP8 self.assert*. | |
""" | |
def __getattribute__(self, attribute_name): | |
if attribute_name.startswith("assert_"): | |
camel_cased_attribute = camel_case(attribute_name) | |
try: | |
return super(PEP8TestCase, self).__getattribute__(camel_cased_attribute) | |
except AttributeError: | |
pass | |
return super(PEP8TestCase, self).__getattribute__(attribute_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment