derwiki-adroll / mock

Automatically exported from code.google.com/p/mock
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

mock.patch prevent nosetest from discovering test generator method #172

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Using Mock 0.8.0 and Nose 1.1.2:

test.py:

import mock

import useless_module

class TestClass:

    def check_call(self, number, result):
        assert result == number * 3

    @mock.patch("useless_module.SomeClass")
    def test_method(self, mock_someclass):
        for i in range(3):
            result = i * 3
            yield self.check_call, i, result

useless_module.py:

class SomeClass:
    def return_something():
        raise

When running `nosetests test.py`, here's the output:

% nosetests test.py
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

Obviously, the test is not running the test generator. When I remove the patch, 
it works fine:

% nosetests test.py          
...
----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK

I attached the reproducible code.

Original issue reported on code.google.com by charlesa...@gmail.com on 3 Sep 2012 at 11:51

Attachments:

GoogleCodeExporter commented 9 years ago
Issue 173 has been merged into this issue.

Original comment by fuzzyman on 3 Sep 2012 at 12:01

GoogleCodeExporter commented 9 years ago
The patched function is not a generator - so nose doesn't detect it. I'm not 
sure what the best way to fix this is because calling a generator function 
returns a generator - so even if nose could detect it the patch would be 
removed once the generator is *created* and would not be in place for the 
iterations.

Using patch in a with statement within the test function would be a workaround. 
A fix that keeps the patch in place until the generator is exhausted doesn't 
occur to me immediately (and in the *general case* is probably not possible / 
useful). Do you have any thoughts?

Original comment by fuzzyman on 3 Sep 2012 at 12:05