Created
June 24, 2013 01:11
-
-
Save dirn/5847166 to your computer and use it in GitHub Desktop.
Descriptor tests
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
import unittest | |
from weakref import WeakKeyDictionary | |
class Skippable(object): | |
def __init__(self, field_name): | |
self.data = WeakKeyDictionary() | |
self.field_name = field_name | |
def __get__(self, instance, owner): | |
def f(): | |
if instance.a == 1: | |
message = 'Skipping test for {}.'.format(self.field_name) | |
return unittest.skip(message) | |
instance.assertEqual(getattr(instance, self.field_name), 1) | |
f.__doc__ = "Test that `{}` has a value.".format(self.field_name) | |
return f | |
def __set__(self, instance, value): | |
pass | |
class MyTestCase(unittest.TestCase): | |
test_a = Skippable('a') | |
test_b = Skippable('b') | |
a = 1 | |
b = 2 | |
class MyTestCase2(unittest.TestCase): | |
test_a = Skippable('a') | |
test_b = Skippable('b') | |
a = 2 | |
b = 3 | |
if __name__ == '__main__': | |
unittest.main(verbosity=2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment