[TIP] "disable" in patch

Alexandre Conrad alexandre.conrad at gmail.com
Mon Dec 13 17:32:35 PST 2010


Hi Herman,

Thanks for your response. I did take your advice and came up with the
following solution:

-----------utils.py-----------------
from mock import patch

__all__ = ('patch')

# this is flag is actually passed on the command line
INTEGRATION = True

class nopatch(object):
    """nopatch class.

    This class is meant to replace the mock.patch object for integration
    tests.

    """
    def __init__(self, *args, **kwargs): pass
    def __call__(self, func): return func
    def __enter__(self): pass
    def __exit__(self, exc_type, exc_value, traceback): pass

if INTEGRATION:
    patch = nopatch
--------------------------------------

and in all my tests I did the following:

-from mock import patch
+from utils import patch


Again, thanks for your pointers. I am happy with this solution.

Cheers,


2010/12/12 Herman Sheremetyev <herman at swebpage.com>:
> If I understand your use case properly then what you really want is a
> set of integration tests. If you really want to use the same tests for
> integration as your unittests the way you describe, then you can
> already accomplish the same goal by replacing the stubbing/patching
> function with a function that doesn't stub them out when you run in
> integration mode.
>
> So instead of:
>
> HTTP_CALLS = False
> @patch(blah, disable=HTTP_CALLS)
> def test():
>  ......
>
> You would write:
>
> patch_function = patch
> @patch(blah):
> def test():
>  ...
>
> And then just change the value of patch to lambda or some empty
> function when you want to test against the real server. The amount of
> code is basically the same on your end.
>
> Cheers,
>
> -Herman
>
> On Sun, Dec 12, 2010 at 11:23 AM, Alexandre Conrad
> <alexandre.conrad at gmail.com> wrote:
>> Hey there,
>>
>> I have created an issue on Google Code for a feature enhancement. Although I
>> have learned about this mailing list *after* I created the issue and I wish
>> I could have exposed my idea before.
>>
>> Nevertheless, I wanted to know how this idea would be accepted by the python
>> mock community. The details are here:
>> http://code.google.com/p/mock/issues/detail?id=60
>>
>> Thanks,
>> --
>> Alex | twitter.com/alexconrad
>>
>> _______________________________________________
>> testing-in-python mailing list
>> testing-in-python at lists.idyll.org
>> http://lists.idyll.org/listinfo/testing-in-python
>>
>>
>



-- 
Alex | twitter.com/alexconrad



More information about the testing-in-python mailing list