[TIP] unittest hook in addition to setUp and tearDown

Dan Wandschneider daniel.wandschneider at schrodinger.com
Wed Nov 9 17:26:49 PST 2016


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.

If you do want to do the decorator approach, it would be something like:

import os
import unittest


def check_for_file(fxn):
    def wrapped(self):
        fxn(self)
        self.assertTrue(os.path.exists('file_name.txt'))
    return wrapped


class MyCase(unittest.TestCase):
    @check_for_file
    def testOne(self):
        pass




On Wed, Nov 9, 2016 at 12:00 PM, <testing-in-python-request at lists.idyll.org>
wrote:

> Send testing-in-python mailing list submissions to
>         testing-in-python at lists.idyll.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         http://lists.idyll.org/listinfo/testing-in-python
> or, via email, send a message with subject or body 'help' to
>         testing-in-python-request at lists.idyll.org
>
> You can reach the person managing the list at
>         testing-in-python-owner at lists.idyll.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of testing-in-python digest..."
>
> Today's Topics:
>
>    1. unittest hook in addition to setUp and tearDown (Dana Scott)
>
>
> ---------- Forwarded message ----------
> From: Dana Scott <danawscott0 at gmail.com>
> To: testing-in-python at lists.idyll.org
> Cc:
> Date: Wed, 09 Nov 2016 17:38:16 +0000
> Subject: [TIP] unittest hook in addition to setUp and tearDown
> Hello,
>
> 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'd like it to contain an assert method that determines whether it
> passes or fails.
>
> Thanks,
>
> Dana
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20161109/d32cf691/attachment.htm>


More information about the testing-in-python mailing list