Skip to content

Instantly share code, notes, and snippets.

@dirn
Created June 24, 2013 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dirn/5847166 to your computer and use it in GitHub Desktop.
Save dirn/5847166 to your computer and use it in GitHub Desktop.
Descriptor tests
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