Organising unit tests

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jimburton

    #1

    Organising unit tests

    I have a number of unit tests organised hierarchically, all of which
    inherit fixtures from a base class. To run these all at once I started
    out using

    from basic import map, directionalpan, layerorder, layervisibility ,
    listlayers, streamlayerlist
    # ... lots more ...

    suite0 =
    unittest.TestLo ader().loadTest sFromTestCase(d irectionalpan.T estPan)
    suite1 =
    unittest.TestLo ader().loadTest sFromTestCase(l ayerorder.TestL ayerorder)
    # ... lots more ...

    alltests = unittest.TestSu ite([suite0
    , suite1
    ....])
    unittest.TextTe stRunner(verbos ity=2).run(allt ests)

    Which is unmaintainable. the TestLoader docs warn that
    loadTestsFromMo dule will not play nicely with my situation and indeed
    if I try

    suite0 = unittest.TestLo ader().loadTest sFromModule(bas ic)

    then run the tests, it hasn't picked any up. I suppose I could collect
    and run the tests with a shell script...what are my options here to
    avoid hardcoding and maintaining the list of tests, and how is it
    normally done?

    Thanks.

  • jimburton

    #2
    Re: Organising unit tests

    OK, so I'm trying to collect the tests with python and add them to the
    test suite dynamically, but I have a problem with module names. Here's
    what I've got:
    ############### ##########
    import os
    from os.path import join
    import unittest

    alltests = unittest.TestSu ite()

    def mightBeATest(f) :
    #TODO check whether it really is a test
    return f.endswith('.py ') and f != '__init__.py'

    def isModule(d):
    if not os.path.isdir(d ):
    return False
    for f in os.listdir(d):
    if f == '__init__.py':
    return True
    return False


    def getTestsFromDir (dir, currentmod):
    for f in os.listdir(dir) :
    if not f.startswith('. '):
    if isModule(f):
    #TODO how to check whether we are several modules in?
    mod = __import__(f)
    getTestsFromDir (os.path.join(d ir, f), mod)
    elif mightBeATest(os .path.join(dir, f)):
    fname, etx = os.path.splitex t(f)
    print 'adding test
    with',('alltest s.addTests(unit test.TestLoader ().loadTestsFro mTestCase('+(cu rrentmod.__dict __[fname])+'))')

    eval('alltests. addTests(unitte st.TestLoader() .loadTestsFromT estCase('+curre ntmod.__dict__[fname]+'))')


    getTestsFromDir (os.curdir, None)
    print alltests.countT estCases()
    ############### #############

    it's importing and referring to the current module that I have a
    problem with...it gives me a key error.

    Comment

    Working...