<div dir="ltr">You could use a decorator, or subclass unittest.TestCase. But it would probably be cleaner just to call the workup at the end of the test. (another option would be to use something like py.test, which gives you this capability in the form of fixtures.<div><br></div><div>If you do want to do the decorator approach, it would be something like:</div><div><br></div><div><div><div><font face="monospace, monospace">import os</font></div><div><font face="monospace, monospace">import unittest</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">def check_for_file(fxn):</font></div><div><font face="monospace, monospace">    def wrapped(self):</font></div><div><font face="monospace, monospace">        fxn(self)</font></div><div><font face="monospace, monospace">        self.assertTrue(os.path.exists(&#39;file_name.txt&#39;))</font></div><div><font face="monospace, monospace">    return wrapped</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">class MyCase(unittest.TestCase):</font></div><div><font face="monospace, monospace">    @check_for_file</font></div><div><font face="monospace, monospace">    def testOne(self):</font></div><div><font face="monospace, monospace">        pass</font></div></div><div><br><div><br></div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Nov 9, 2016 at 12:00 PM,  <span dir="ltr">&lt;<a href="mailto:testing-in-python-request@lists.idyll.org" target="_blank">testing-in-python-request@lists.idyll.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Send testing-in-python mailing list submissions to<br>
        <a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.<wbr>org</a><br>
<br>
To subscribe or unsubscribe via the World Wide Web, visit<br>
        <a href="http://lists.idyll.org/listinfo/testing-in-python" rel="noreferrer" target="_blank">http://lists.idyll.org/<wbr>listinfo/testing-in-python</a><br>
or, via email, send a message with subject or body &#39;help&#39; to<br>
        <a href="mailto:testing-in-python-request@lists.idyll.org">testing-in-python-request@<wbr>lists.idyll.org</a><br>
<br>
You can reach the person managing the list at<br>
        <a href="mailto:testing-in-python-owner@lists.idyll.org">testing-in-python-owner@lists.<wbr>idyll.org</a><br>
<br>
When replying, please edit your Subject line so it is more specific<br>
than &quot;Re: Contents of testing-in-python digest...&quot;<br>
<br>Today&#39;s Topics:<br>
<br>
   1. unittest hook in addition to setUp and tearDown (Dana Scott)<br>
<br><br>---------- Forwarded message ----------<br>From: Dana Scott &lt;<a href="mailto:danawscott0@gmail.com">danawscott0@gmail.com</a>&gt;<br>To: <a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a><br>Cc: <br>Date: Wed, 09 Nov 2016 17:38:16 +0000<br>Subject: [TIP] unittest hook in addition to setUp and tearDown<br><div dir="ltr"><font face="monospace" class="m_-3642658797668401944gmail_msg" style="color:rgb(33,33,33)">Hello,</font><div class="m_-3642658797668401944gmail_msg" style="color:rgb(33,33,33);font-size:16.25px"><font face="monospace" class="m_-3642658797668401944gmail_msg"><br class="m_-3642658797668401944gmail_msg"></font></div><div class="m_-3642658797668401944gmail_msg" style="color:rgb(33,33,33);font-size:16.25px"><font face="monospace" class="m_-3642658797668401944gmail_msg">Is there a way to add a hook to a unittest.TestCase that would run after or during each test method, before tearDown, and affect the result? That is, I&#39;d like it to contain an assert method that determines whether it passes or fails.</font></div><div class="m_-3642658797668401944gmail_msg" style="color:rgb(33,33,33);font-size:16.25px"><font face="monospace" class="m_-3642658797668401944gmail_msg"><br class="m_-3642658797668401944gmail_msg"></font></div><div class="m_-3642658797668401944gmail_msg" style="color:rgb(33,33,33);font-size:16.25px"><font face="monospace" class="m_-3642658797668401944gmail_msg">Thanks,</font></div><div class="m_-3642658797668401944gmail_msg" style="color:rgb(33,33,33);font-size:16.25px"><font face="monospace" class="m_-3642658797668401944gmail_msg"><br class="m_-3642658797668401944gmail_msg"></font></div><div class="m_-3642658797668401944gmail_msg" style="color:rgb(33,33,33);font-size:16.25px"><font face="monospace" class="m_-3642658797668401944gmail_msg">Dana</font></div></div>
<br>______________________________<wbr>_________________<br>
testing-in-python mailing list<br>
<a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.<wbr>org</a><br>
<a href="http://lists.idyll.org/listinfo/testing-in-python" rel="noreferrer" target="_blank">http://lists.idyll.org/<wbr>listinfo/testing-in-python</a><br>
<br></blockquote></div><br></div></div>