You can actually just do this as `with mock.patch.object(pygeoip.GeoIP, 'country_code_by_addr', self.mock_country_cod_by_attr):`

Also, if you do it in the `setUp` (using `patcher.start` and `patcher.stop` as described here (http://www.voidspace.org.uk/python/mock/patch.html#patch-methods-start-and-stop), then you don't need the same context manager in both tests).