<br><br><div class="gmail_quote">On Fri, May 8, 2009 at 7:24 AM, Ned Batchelder <span dir="ltr">&lt;<a href="mailto:ned@nedbatchelder.com">ned@nedbatchelder.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div></div><div class="h5">Alfredo Deza wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi,<br>
<br>
I have just started to get into the habit of testing everything that I code, and a good friend of mine suggested the coverage plugin when using Nosetests.<br>
<br>
Right now I am working on a Daemon module for Python scripts. The module, when imported will make the current process detach from the terminal and run in the background as a daemon.<br>
<br>
When trying to test the module (importing it to test_module.py) I fail to correctly test any actions since it will convert test_module.py into a daemon.<br>
<br>
To circumvent this problem I created a &quot;daemon script&quot; (test_script.py) that would import the daemon module and effectively run in the background.<br>
<br>
The tests would check upon this daemon and the actions the module should be doing when working correctly (write to a file, kill a process etc).<br>
<br>
And this solution works fine for testing, but the &quot;--with-coverage&quot; flag does not cover the daemon module at all.<br>
<br>
When going further and specifying the package with &quot;--cover-package=daemonModule&quot; I get 0% coverage. I&#39;m thinking this is expected since I am not importing the daemon module within test_module.py but rather on the daemon test script (test_script.py).<br>

<br>
And just to avoid some confusion, this is what I have:<br>
test_script.py =&gt; Script that will correctly daemonize so I can test behavior test_module.py =&gt; Where all the tests go. Can&#39;t import the daemon module here or this will detach from the terminal.<br>
<br>
Do you guys know any other way that I could effectively test the daemon while still being able to maintain the Coverage plugin with a nice percentage value?<br>
<br>
<br>
Thanks,<br>
<br>
<br>
Alfredo<br>
</blockquote></div></div>
I&#39;m not sure I&#39;ve got all the details right, but it sounds like the basic problem is that you have a test that nose can measure, but that test spawns a new process, and the code in the new process is not measured.  Ironically, I have the same problem when coverage-testing coverage.py! <br>

One possibility is to remove daemonization from the test run.  This is similar to the testing technique of using test doubles to remove complicated dependencies from tests.  If your code is organized so that the main() of the daemon is callable directly, you can write a test that invokes the main directly, rather than in a separate process, and measure code coverage that way.<br>

<br>
Another method would be to invoke coverage programmatically in the daemon itself.  This is less desirable because you have to modify your product code to deal with testing issues, but would give you more realistic measurement since the code really is running as a daemon.<br>

<br>
--Ned.<br><font color="#888888">
<br>
-- <br>
Ned Batchelder, <a href="http://nedbatchelder.com" target="_blank">http://nedbatchelder.com</a><br>
</font></blockquote><div><br>Ned,<br><br>You are correct, the test spawns a new process.<br><br>I like the idea of removing the daemonization from the test run, or testing all the way up to when the module kicks the daemonization call.<br>
<br>Thanks for you input.<br><br><br>-Alfredo <br></div></div><br>