[TIP] adding custom tests to a unittest test run

Marius Gedminas marius at gedmin.as
Wed May 16 06:27:39 PDT 2012


On Tue, May 15, 2012 at 09:25:28PM -0700, Chris Jerdonek wrote:
> I often have the need to add extra tests to my unittest.main() test
> runs.  The tests I need to add are tests that depend on data obtained
> at run-time, e.g. user-provided command-line options.

I'm used to the following idiom for this:

    def test_suite():
        tests = unittest.TestSuite(...)
        if some_condition:
            tests.addTests(...)
        if some_condition:
            tests.addTests(...)
        return tests

    if __name__ == '__main__':
        unittest.main(defaultTest='test_suite')

Although in real life I'll use a test runner (such as zope.testrunner)
that will add up all the tests returned by test_suite() in all of my
test modules, instead of relying on unittest.main().

> I would like to add these tests to unittest.main() in a way that
> doesn't depend on "global state."

I'm not sure I understand this.

> So far, the best I've come up with is the below.  Is there a better
> solution?  What do others recommend?
> 
> [Also see: https://gist.github.com/2707352 ]

I find this code difficult to understand.

Marius Gedminas
-- 
Life was simple before World War II. After that, we had systems.
        -- Grace Murray Hopper, 1987
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20120516/9cdb46df/attachment.pgp>


More information about the testing-in-python mailing list