Guest User

Untitled

a guest
May 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. # I'm rather pleased with this use of Python decorators for applying
  2. # stubs to a class before a test method.
  3. #
  4. # It feels a bit like a poor man's shoulda. :-)
  5.  
  6. import os
  7. import unittest2 as unittest
  8.  
  9. import satisfaction
  10.  
  11.  
  12. def fixture(cls, name):
  13. def wrapper(test):
  14. def test_with_fixture(*args):
  15. def stubbed_url(self, topic_id):
  16. fixture = "%s-%s.xml" % (cls.__name__.lower(), name)
  17. return os.path.join(os.getcwd(), "fixtures", fixture)
  18.  
  19. try:
  20. original_url = cls.url
  21. cls.url = stubbed_url
  22. return test(*args)
  23. finally:
  24. cls.url = original_url
  25. return test_with_fixture
  26. return wrapper
  27.  
  28.  
  29. class TopicTest(unittest.TestCase):
  30.  
  31. def topic(self, topic_id="409678"):
  32. return satisfaction.Topic(topic_id)
  33.  
  34. def test_when_topic_doesnt_exist_then_not_found(self):
  35. with self.assertRaises(satisfaction.ResourceNotFound):
  36. self.topic("bad-id").title
  37.  
  38. def test_when_topic_exists_then_title_available(self):
  39. self.assertEqual(self.topic().title, "Fantastic improvement")
  40.  
  41. def test_when_topic_exists_then_content_available(self):
  42. self.assertIn("Well done!", self.topic().content)
  43.  
  44. @fixture(satisfaction.Topic, "without-replies")
  45. def test_when_topic_has_no_replies_then_reply_count_zero(self):
  46. self.assertEqual(0, self.topic().reply_count)
  47.  
  48. @fixture(satisfaction.Topic, "with-replies")
  49. def test_when_topic_has_replies_then_reply_count_set(self):
  50. self.assertEqual(3, self.topic().reply_count)
  51.  
  52.  
  53. if __name__ == "__main__":
  54. unittest.main()
Add Comment
Please, Sign In to add comment