Yelp / Testify

A more pythonic testing framework.
Other
308 stars 67 forks source link

let decorator shouldn't try to execute unbounded methods. #111

Closed baris closed 12 years ago

baris commented 12 years ago

calling getattr(TestCaseClass, method_name) tries to execute the method. I believe this is not the intended behavior.

baris commented 12 years ago

Actually, is using a descriptor method intentional or can this be done in call ?

sumeet commented 12 years ago

i can't reproduce the case you're talking about.

this test

commit 0e4888f0e49557cdf36e38a91a74f462a1206fc4
Author: Sumeet Agarwal <sumeet.a@gmail.com>
Date:   Thu Sep 27 22:05:22 2012 -0700

    add test for accessing let through the class

diff --git a/test/test_case_test.py b/test/test_case_test.py
index 66f6fd3..2bd2875 100644
--- a/test/test_case_test.py
+++ b/test/test_case_test.py
@@ -2,6 +2,7 @@ import itertools
 import unittest

 from testify import assert_equal
+from testify import assert_isinstance
 from testify import class_setup
 from testify import class_setup_teardown
 from testify import class_teardown
@@ -495,6 +496,10 @@ class LetTest(TestCase):
         assert_equal(self.counter.next(), 0)
         assert_equal(self.counter.next(), 1)

+    def test_returns_itself_when_accessed_through_the_class(self):
+        assert_isinstance(type(self).counter, let)
+
+
 class LetWithLambdaTest(TestCase):

     counter = let(lambda self: itertools.count(0))

passes because of the

        if test_case is None:
            return self

part in

    def __get__(self, test_case, cls):
        if test_case is None:
            return self
        if self._result is self._unsaved:
            self._save_result(self._func(test_case))
            self._register_reset_after_test_completion(test_case)
        return self._result

unbound descriptors receive None for the second value to __get__. which here is test_case. can you post a failing test?

hmm i'm not sure how __call__ would help here. the descriptor is the only way i can think of to implement this other than using __getattr__ / __getattribute__. that would be really gross, though.

baris commented 12 years ago

Thanks for the explanation, you're right, the issue I had was apparently a bit different. I think I was confused by the error I got when I tried to access im_func of a method on an instance, not the class. I'll drop this pull request.