Skip to content

Instantly share code, notes, and snippets.

@jlaura
Created May 23, 2019 18:42
Show Gist options
  • Save jlaura/6c480156f3b17088025e1c906e574b4c to your computer and use it in GitHub Desktop.
Save jlaura/6c480156f3b17088025e1c906e574b4c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class Foo:\n",
" @property \n",
" def x(self):\n",
" return 1\n",
" \n",
"class Bar(Foo):\n",
" @property\n",
" def xp1(self):\n",
" return self.x + 1\n",
" \n",
"b = Bar()\n",
"b.xp1"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from unittest.mock import PropertyMock, patch"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n"
]
}
],
"source": [
"with patch('__main__.Foo.x', new_callable=PropertyMock) as mock_foo: # __main__ might be another file, e.g. ale.drivers.pds3_label\n",
" mock_foo.return_value = 2\n",
" b = Bar()\n",
" assert b.xp1 == 3\n",
" print(b.xp1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "AutoCNet-Refactor",
"language": "python",
"name": "autocnet_refactor"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment