How do I mock a class and control a returned value in py.test with pytest-mock?

I think you may be getting confused between https://docs.python.org/3/library/unittest.mock.html and pytest monkey patch. I don't think both behave in the same way.

You can make it work using mock patch (https://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch)

@pytest.fixture()
def no_ldap(self):
    patcher = mock.patch('ldap.initialize')
    patcher.start()
    yield patcher
    patcher.stop()
  

You can just use patch directly (and something was off about your structure):

from mock import patch, Mock
import pytest
# Here is some code to simply test mocking out ldap.initialize(), and
# controlling the return value from calls to search_s()
import ldap

def find_users(ldap_url, admin_user, admin_password, userbase):
    lobj = ldap.initialize(ldap_url)
    lobj.simple_bind_s(admin_user, admin_password)
    for i in lobj.search_s(userbase, ldap.SCOPE_SUBTREE, '*'):
        yield i[1]['uid'][0]

class TestMocking:

    @patch('ldap.initialize')
    def test_ad_one_user(self, no_ldap):
        # try and modify how search_s() would return
        data = [('', {'uid': ['happy times']})]
        search_s = Mock(return_value=data)
        no_ldap.return_value = Mock(search_s=search_s)
        count = 0
        for i in find_users('', '', '', ''):
            count += 1
            assert i=='happy times'
        assert count == 1