Yelp / Testify

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

test failure when run from python, but passes with testify #167

Closed blampe closed 10 years ago

blampe commented 11 years ago

Crazy edge case. Save this to test.py:

import mock
import testify

class DerpTest(testify.TestCase):

    class Herp(object):
        def function(self):
            raise Exception("You're not supposed to be here!")

    def test_herp_derp(self):
        # Fails on `python`, passes on `testify`
        with mock.patch('test.DerpTest.Herp.function') as mocked_function:
            self.Herp().function()
            mocked_function.assert_any_call()

    def test_correct_herp_derp(self):
        # Passes on both `python` and `testify`
        with mock.patch.object(self.Herp, 'function') as mocked_function:
            self.Herp().function()
            mocked_function.assert_any_call()

    def test_correct_herp_derp_two(self):
        # Passes on both `python` and `testify`
        with mock.patch.object(DerpTest.Herp, 'function') as mocked_function:
            self.Herp().function()
            mocked_function.assert_any_call()

if __name__ == '__main__':
    testify.run()

Testify:

$ testify test.py
test DerpTest.test_correct_herp_derp ... ok in 0.00s
test DerpTest.test_correct_herp_derp_two ... ok in 0.00s
test DerpTest.test_herp_derp ... ok in 0.00s

========================================================================
FAILURES
None!

PASSED.  3 tests / 1 case: 3 passed, 0 failed.  (Total test time 0.00s)

Python:

$ python test.py
..error: DerpTest.test_herp_derp
Traceback (most recent call last):
  File "test.py", line 15, in test_herp_derp
    self.Herp().function()
  File "test.py", line 10, in function
    raise Exception("You're not supposed to be here!")
Exception: You're not supposed to be here!

E
FAILED.  3 tests / 1 case: 2 passed, 1 failed.  (Total test time 0.00s)
dnephin commented 10 years ago

I don't think this is an issue with testify at all. It has to do with mock.patch and names: http://www.voidspace.org.uk/python/mock/patch.html#where-to-patch).

You're accessing self.Herp().function(), but you're patching a different name.

bukzor commented 10 years ago

Note that there is a stdlib module named 'test' so the magical str-to-object machinery of mock.patch(str) may be getting confused there. mock.patch.object is less fuzzy in that regard.

I believe that's exactly what's happening; testify adds the $PWD to the python-path, while plain-old python does not, therefore 'import test' will give you a different module in each case. Further, in the second case, the module's name is __main__ rather than test.

bukzor commented 10 years ago

notabug