<br><br><div class="gmail_quote">On Tue, Jan 19, 2010 at 9:55 AM, holger krekel <span dir="ltr"><<a href="mailto:holger@merlinux.eu">holger@merlinux.eu</a>></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;">
Hi Alfredo,<br>
<div class="im"><br>
On Mon, Jan 18, 2010 at 21:37 -0500, Alfredo Deza wrote:<br>
> I am building some tests for a Python installer that copies some files and<br>
> changes some permissions.<br>
><br>
> Nothing weird there.<br>
><br>
> However, in the test class I am starting with a setUp that basically calls<br>
> the installer (so the tests can see if everything is where it should be),<br>
> this is followed by a few assertions and finally a tearDown is called where<br>
> everything gets uninstalled.<br>
><br>
> When running the tests with nosetests, I see that setUp and tearDown are<br>
> called for *every* method in my test class (e.g. installs => runs test<br>
> method => uninstalls ....)<br>
><br>
</div><div class="im">> Isn't setUp supposed to be run once at the beginning of the class? or is<br>
> this expected?<br>
><br>
</div><div class="im">> In case this is expected, is there a way to do it *just once *for all the<br>
> methods?<br>
<br>
</div>The others gave various answers related to unittest and its extensions -<br>
i'd like to add another way using py.test "funcargs". The main advantage<br>
is that you can completely decouple test fixture and actual test code, allowing<br>
for more flexibility and ease of doing functional/system testing.<br>
<br></blockquote><div>Would that be specific to py.test ? I am trying to build agnostic tests that can be run with anything<br>and understood in the original way it was written.<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Example: you write one or multiple test functions that look like this<br>
(also can be put on a class of course):<br>
<br>
def test_installer(setupdir):<br>
# do your tests, setupdir is your fixture state<br>
<br>
This is a test function that runs and works with a "setupdir" function argument.<br>
You define a function argument factory which is invoked to provide<br>
an instance of it:<br>
<br>
def pytest_funcarg__setupdir(request):<br>
return request.cached_setup(scope="session",<br>
setup=lambda: MySetupDir(),<br>
teardown=lambda mysetupdir: mysetupdir.finish()<br>
)<br>
<br>
The specified 'setup' and 'teardown' will be called exactly once per<br>
session. If you don't run a test that needs the funcarg the factory<br>
function will not be called at all. In any case, your test functions<br>
can stay completely ignorant from how fixtures are managed.<br>
<br>
You can change scoping by modifying the factory function - e.g.<br>
add a command line option that forces "function" scoping which usually<br>
avoids "cascading failures" and provides better test isolation at the expensve<br>
of longer test run durations. Useful for a nightly Continous Integration<br>
configuration.<br>
<br>
Lastly, the 'request' object is a handle to the particular test<br>
function - it provides access to the test function object, its module,<br>
class object etc. and you can thus easily mark a test to receive a<br>
specially parametrized "setupdir" ... see <a href="http://tinyurl.com/yfw82l5" target="_blank">http://tinyurl.com/yfw82l5</a><br>
for details.<br>
<br>
cheers,<br>
<font color="#888888">holger<br>
</font></blockquote></div><br><br clear="all"><br>-- <br>Alfredo Deza<br><br>