<div dir="ltr"><div><div><div><div><div><div><div><div><div>Sorry, I&#39;ve just figured out it was about reading a file, but that&#39;s not really different, it&#39;s easier.<br><br></div>case 1, with &quot;data1&quot; I expect that &quot;do_my_stuff1&quot; is called when calling &quot;do_all_my_stuff&quot; for example.<br></div>you just can inject do_my_stuff1 and &quot;data1&quot; in &quot;do_all_my_stuff&quot;<br><br></div>next in case 2, with &quot;data2&quot; I expect that &quot;do_my_stuff2&quot; is called when calling &quot;do_all_my_stuff&quot;.<br></div>Now you need to inject &quot;do_my_stuff1&quot; and &quot;do_my_stuff2&quot; as arguments to &quot;do_all_my_stuff&quot;...<br><br></div>Continuing this way you&#39;ll see a pattern and with refactoring you&#39;ll be able to remove a long list of arguments in &quot;do_all_my_stuff&quot;.<br></div>Generraly this means the creation of a new type of object and a call on differents methods: &quot;my_new_object.do_my_stuff1&quot; and &quot;my_new_object.do_my_stuff2&quot; and do_all_my_stuff(data, my_new_type_of_object)<br></div><div>To test this you just need to mock &quot;my_new_object&quot;<br><br></div><div>do you feel emergent design here ?<br></div><div><br></div>Why ?<br></div>The aim is to be able to change/adapt your code faster.<br></div>If you&#39;ve got a new case that require a different behaviour, just create a new type of object with the same methods than &quot;my_new_object&quot; and inject it in &quot;do_all_my_stuff&quot; instead, require less changes to your code.<br><div><div><div><br><br><br><br><br><br><br><div><div><div><div><br><div><div><div><div><div><br></div></div></div></div></div></div></div></div></div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-05-09 12:50 GMT+02:00 Gregory Salvan <span dir="ltr">&lt;<a href="mailto:apieum@gmail.com" target="_blank">apieum@gmail.com</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div><div>Ok let&#39;s try differently, I&#39;ll make assumptions about your case correct me if I&#39;m wrong :)<br><br></div>It smells you&#39;re talking about BDD (TDD London school), in this case you&#39;re testing the behaviour of your functions.<br></div>For example you want to test in case1 if you&#39;re writing &quot;data1&quot; to a file.<br>I would just mock a file object I&#39;ll inject in the function (let&#39;s call it &quot;do_my_stuff1&quot;) responsible to do this job.<br></div>do_my_stuff1 will take a file_object as argument and by mocking this file object you&#39;re able to test if &quot;write&quot; method is called with &quot;data1&quot;.<br><br></div>Let&#39;s do with a more complex example, you want to be ensured that &quot;data2&quot; are written to file &quot;filename2&quot;, in this case you&#39;ll inject the function that make the file_object and the filename.<br></div>do_my_stuff2 will take 2 arguments: a function and a file name, by mocking the function in your tests you&#39;re able to check if it&#39;s called with &quot;filename2&quot; and if the method &quot;write&quot; of the returned object is called with &quot;data2&quot;.<br><br></div>It&#39;s to generic to have complex examples, but you really can test everything without writing data in files, and it&#39;s not really relevant to test if data are really written as it&#39;s the same as testing if there&#39;s no errors in python &quot;open&quot; implementation. I think we can trust python developpers for that ;)<br><div><div><br><div><div><div><div><div><div><div><br></div></div></div></div></div></div></div></div></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">2017-05-09 12:21 GMT+02:00 David Palao <span dir="ltr">&lt;<a href="mailto:dpalao.python@gmail.com" target="_blank">dpalao.python@gmail.com</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi, Gregory.<br>
Thank you, but it&#39;s not clear.<br>
First, my program is supposed to read a file and give an answer<br>
depending on the contents of the file. This is a functionality I want<br>
it to have.<br>
<br>
Now, concerning TDD, for unit tests, I agree with you. But when it<br>
comes to functional tests (or acceptance tests), I&#39;m not testing a<br>
*single* function of my code. I&#39;m doing kind of a global black-box<br>
test to ensure that the user will get the expected result. And I want<br>
to do provide the correct result under different conditions. Therefore<br>
I need to find a way to provide a fake environment for my (functional,<br>
acceptance, black-box) tests. What I understand about TDD is that<br>
although the speed of execution of the tests is important, speed means<br>
nothing if correctness is sacrificed. So if I want to simulate a<br>
situation where some file has different contents in it, I need to test<br>
the program in an environment where that file has those different<br>
values. I don&#39;t see a way around it. (I could go through the net and<br>
try to access a different system for each of the different properties<br>
I&#39;m looking for, but that would be even worse, wouldn&#39;t it?)<br>
But maybe I&#39;m missing something.<br>
<br>
Best.<br>
<div class="m_-4946879272575728578HOEnZb"><div class="m_-4946879272575728578h5"><br>
<br>
2017-05-09 11:55 GMT+02:00 Gregory Salvan &lt;<a href="mailto:apieum@gmail.com" target="_blank">apieum@gmail.com</a>&gt;:<br>
&gt; Hello,<br>
&gt; I&#39;ve seen no answer concerning TDD, I&#39;ll try to give you one.<br>
&gt; TDD means fast feedback, so tests must execute fast, and so you&#39;re looking<br>
&gt; for a design that&#39;s testable without reading your data in files.<br>
&gt; In the TDD point of view, you&#39;ll have a code design where data are raw<br>
&gt; injected in the function you&#39;re testing and testing if data are well read is<br>
&gt; a non sense (for TDD only)<br>
&gt; Hope it&#39;s clear.<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; 2017-05-09 11:29 GMT+02:00 David Palao &lt;<a href="mailto:dpalao.python@gmail.com" target="_blank">dpalao.python@gmail.com</a>&gt;:<br>
&gt;&gt;<br>
&gt;&gt; Hello,<br>
&gt;&gt; After playing around with systemfixtures, I don&#39;t find a way to make<br>
&gt;&gt; it work for my purposes.<br>
&gt;&gt; I&#39;m inclined to think that chroot is the answer to my question (thank<br>
&gt;&gt; you, Johan, for pointing it out from the beginning).<br>
&gt;&gt;<br>
&gt;&gt; As I  explained in my first post, I need a way to provide a fake<br>
&gt;&gt; filesystem to my functional tests. What I mean by functional tests is<br>
&gt;&gt; testing from the point of view of the user. What I do for that is: I<br>
&gt;&gt; call the executable from within my FTs, catch the output and compare<br>
&gt;&gt; it with the expected output.<br>
&gt;&gt; To call the executable I use subprocess.Popen. In principle it should<br>
&gt;&gt; be possible to provide a fake fs to it but, isn&#39;t it, at the end of<br>
&gt;&gt; the day, the same as creating a chroot jail?<br>
&gt;&gt;<br>
&gt;&gt; Unless there is an easy way to do what I need using systemfixtures (or<br>
&gt;&gt; fakefs), which I don&#39;t see, I think I will need to go to chroot (or<br>
&gt;&gt; docker). Any recommended tool to create a basic chroot jail with<br>
&gt;&gt; python in it that I can use to run my program and modify files in<br>
&gt;&gt; /proc?<br>
&gt;&gt;<br>
&gt;&gt; Thanks in advance.<br>
&gt;&gt;<br>
&gt;&gt; Best<br>
&gt;&gt;<br>
&gt;&gt; 2017-01-24 15:08 GMT+01:00 Johan Olsen &lt;<a href="mailto:ulf.johan.olsen@gmail.com" target="_blank">ulf.johan.olsen@gmail.com</a>&gt;:<br>
&gt;&gt; &gt; Hello,<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; I would solve this by running my program in a chroot environment in some<br>
&gt;&gt; &gt; way. Chroot is used to change the apparent root directory for some<br>
&gt;&gt; &gt; running<br>
&gt;&gt; &gt; process and its children. You can then set up a mock /proc under e.g.<br>
&gt;&gt; &gt; /tmp/mock, and run your tests with chroot under pretense that /tmp/mock<br>
&gt;&gt; &gt; is<br>
&gt;&gt; &gt; your actual root folder. From python you can do this with os.chroot.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Regards,<br>
&gt;&gt; &gt; Johan Olsén<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; 2017-01-24 14:51 GMT+01:00 David Palao &lt;<a href="mailto:dpalao.python@gmail.com" target="_blank">dpalao.python@gmail.com</a>&gt;:<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Hello,<br>
&gt;&gt; &gt;&gt; I am writing a program that does something based on information read<br>
&gt;&gt; &gt;&gt; from /proc (the target OS is linux). But I have some problems figuring<br>
&gt;&gt; &gt;&gt; out how to mock the /proc filesystem, or at least, some files in it.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; The rationale is that I want to change some files inside /proc to<br>
&gt;&gt; &gt;&gt; simulate different configurations of the host computer so that I can<br>
&gt;&gt; &gt;&gt; run my functional tests against those different configurations.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; I was thinking in using docker for this task. But<br>
&gt;&gt; &gt;&gt; 1) this is offtopic here :)<br>
&gt;&gt; &gt;&gt; and<br>
&gt;&gt; &gt;&gt; 2) I would like to learn what is the standard way to deal with such<br>
&gt;&gt; &gt;&gt; situations. If there is a &quot;standard way&quot; at all... I mean, what am I<br>
&gt;&gt; &gt;&gt; supposed to do from the point of view of TDD?<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; I would appreciate if someone with experience in such problems could<br>
&gt;&gt; &gt;&gt; share some advice.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Best,<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; David<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; ______________________________<wbr>_________________<br>
&gt;&gt; &gt;&gt; testing-in-python mailing list<br>
&gt;&gt; &gt;&gt; <a href="mailto:testing-in-python@lists.idyll.org" target="_blank">testing-in-python@lists.idyll.<wbr>org</a><br>
&gt;&gt; &gt;&gt; <a href="http://lists.idyll.org/listinfo/testing-in-python" rel="noreferrer" target="_blank">http://lists.idyll.org/listinf<wbr>o/testing-in-python</a><br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt;<br>
&gt;&gt; ______________________________<wbr>_________________<br>
&gt;&gt; testing-in-python mailing list<br>
&gt;&gt; <a href="mailto:testing-in-python@lists.idyll.org" target="_blank">testing-in-python@lists.idyll.<wbr>org</a><br>
&gt;&gt; <a href="http://lists.idyll.org/listinfo/testing-in-python" rel="noreferrer" target="_blank">http://lists.idyll.org/listinf<wbr>o/testing-in-python</a><br>
&gt;<br>
&gt;<br>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>