<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 12 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
{font-family:SimSun;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:SimSun;
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
font-size:10.5pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri","sans-serif";
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;}
/* Page Definitions */
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.WordSection1
{page:WordSection1;}
/* List Definitions */
@list l0
{mso-list-id:484274618;
mso-list-template-ids:1882223508;}
@list l0:level1
{mso-level-number-format:bullet;
mso-level-text:\F0B7;
mso-level-tab-stop:36.0pt;
mso-level-number-position:left;
text-indent:-18.0pt;
mso-ansi-font-size:10.0pt;
font-family:Symbol;}
ol
{margin-bottom:0cm;}
ul
{margin-bottom:0cm;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="ZH-CN" link="blue" vlink="purple" style="text-justify-trim:punctuation">
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-US">Hi,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">In a.py we have a function:<o:p></o:p></span></p>
<p class="MsoNormal" align="left" style="text-align:left;text-autospace:none"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:blue">def</span><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black">
<b>_change_file_mode</b>(filepath):</span><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New""><o:p></o:p></span></p>
<p class="MsoNormal" style="text-indent:18.0pt"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black">utils.execute(</span><i><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:#00AA00">'<u>chmod</u>'</span></i><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black">,
</span><i><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:#00AA00">'600'</span></i><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black">, filepath, run_as_root=</span><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:blue">True</span><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black">)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">In test_xxx.py, there is a testclass:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black">clas<b>s xxxxDriverTestCase(test.TestCase):<o:p></o:p></b></span></p>
<p class="MsoNormal" style="text-indent:21.0pt"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black">def test_a(self)<o:p></o:p></span></b></p>
<p class="MsoNormal" style="text-indent:21.0pt"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black"> …<o:p></o:p></span></b></p>
<p class="MsoNormal" style="text-indent:21.0pt"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black"> Call a. _change_file_mode<o:p></o:p></span></b></p>
<p class="MsoNormal" style="text-indent:40.15pt"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black">…<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black"><o:p> </o:p></span></b></p>
<p class="MsoNormal" style="text-indent:21.0pt"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black">def test_b(self)<o:p></o:p></span></b></p>
<p class="MsoNormal" style="text-indent:21.0pt"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black"> …<o:p></o:p></span></b></p>
<p class="MsoNormal" style="text-indent:21.0pt"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black"> Call a. _change_file_mode<o:p></o:p></span></b></p>
<p class="MsoNormal" style="text-indent:40.15pt"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black">…</span></b><span lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">I have tried to mock like mock out function
</span><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black">_change_file_mode
</span></b><span lang="EN-US">like this:<o:p></o:p></span></p>
<p class="MsoNormal"><i><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:#7D7D7D">@mock.patch.object</span></i><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black">(a,
</span><i><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:#00AA00">'_change_file_mode',
</span></i><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black">return_value=None)</span><span lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black">clas<b>s xxxxDriverTestCase(test.TestCase):<o:p></o:p></b></span></p>
<p class="MsoNormal" style="text-indent:21.0pt"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black">def test_a(self)<o:p></o:p></span></b></p>
<p class="MsoNormal" style="text-indent:21.0pt"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black"> …<o:p></o:p></span></b></p>
<p class="MsoNormal" style="text-indent:21.0pt"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black"> Call a. _change_file_mode<o:p></o:p></span></b></p>
<p class="MsoNormal" style="text-indent:40.15pt"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black">…<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black"><o:p> </o:p></span></b></p>
<p class="MsoNormal" style="text-indent:21.0pt"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black">def test_b(self)<o:p></o:p></span></b></p>
<p class="MsoNormal" style="text-indent:21.0pt"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black"> …<o:p></o:p></span></b></p>
<p class="MsoNormal" style="text-indent:21.0pt"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black"> Call a. _change_file_mode<o:p></o:p></span></b></p>
<p class="MsoNormal" style="text-indent:40.15pt"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Courier New";color:black">…<o:p></o:p></span></b></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">But the mock takes no effort, the real function _change_file_mode is still executed.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">So how to make a mock effactive for all method of a testclass?<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Thanks for any input!<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Wilson Liu<o:p></o:p></span></p>
</div>
</body>
</html>