PySweeper: Python test collector 14

Posted by tottinger Sat, 17 Feb 2007 18:00:00 GMT

I wrote a number of separate test files for the pysweeper refactoring exercise, and very quickly got tired of running them all individually. I created a stupid test composite tool and I run this one instead.

Here’s what it is looks like right now.

import os
import unittest
import sys

def collect_tests(directoryName):
    """Gather all tests matching "test*.py" in the current directory""" 
    alltests = []
    for filename in os.listdir(directoryName):
        if filename.startswith('test') and filename.endswith('.py'):
            module = __import__(filename.replace(".py",""))
            for item_name in dir(module):
                item = getattr(module,item_name)
                if isinstance(item,type) and issubclass(item,unittest.TestCase):
                    test = unittest.makeSuite(item)
                    alltests.append(test)
    return alltests

def suite():
    "Collect all local tests into a monster suite" 
    suite = unittest.TestSuite()
    here = os.path.dirname(sys.argv[0]) or os.getcwd()
    alltests = collect_tests(here)
    suite.addTests(alltests)
    return suite

if __name__ == "__main__":
    tester = unittest.TextTestRunner()
    tester.run(suite())

Thre may be easier ways or better ways, but right now I don’t know them. I figure it will be good enough until I learn something cooler. I can think of several ways to extend it, but don’t need them.

Trackbacks

Use the following link to trackback from your own site:
http://blog.objectmentor.com/articles/trackback/186

Comments

Leave a response

  1. Avatar
    JB 1 day later:

    Tim,

    A better way to go may be to use the TestLoader class (which is part of the Python unittest package – http://docs.python.org/lib/testloader-objects.html) which has a loadTestsFromModule method. Here is what I did for my tests:

    
    import AcceptanceTests
    import UnitTests
    import IntegrationTests
    import unittest
    
    suite = unittest.TestSuite()
    for test_module in [UnitTests, IntegrationTests, AcceptanceTests]:
        tests = unittest.TestLoader().loadTestsFromModule(test_module)
        suite.addTests(tests)
    unittest.TextTestRunner(verbosity=2).run(suite)
    

    Hope that this helps. Jake

  2. Avatar
    JB 1 day later:
    This makes your code a bit simpler:
    
    import os
    import unittest
    import sys
    
    def collect_tests(directoryName):
        """Gather all tests matching "test*.py" in the current directory""" 
        alltests = []
        onlyTestFile = lambda filename: filename.startswith('test') and filename.endswith('.py')
        for filename in filter(onlyTestFile, os.listdir(directoryName)):
            module = __import__(filename.replace(".py",""))
            alltests += unittest.TestLoader().loadTestsFromModule(module)
        return alltests
    
    def suite():
        "Collect all local tests into a monster suite" 
        suite = unittest.TestSuite()
        here = os.path.dirname(sys.argv[0]) or os.getcwd()
        alltests = collect_tests(here)
        suite.addTests(alltests)
        return suite
    
    if __name__ == "__main__":
        tester = unittest.TextTestRunner()
        tester.run(suite())
    
    
  3. Avatar
    Tim 1 day later:

    Ah. TestLoader is nice. This works great, and has fewer layers of if logic. I’m using this code exactly as given.

    This is the cool thing about programming in the public eye. It’s good to have a remote, delayed pair partner.

    Thank you very much!

  4. Avatar
    Criminal Records over 4 years later:

    This is the cool thing about programming in the public eye. It’s good to have a remote, delayed pair partner.

  5. Avatar
    Tenant Screening over 4 years later:

    Thre may be easier ways or better ways, but right now I don’t know them. I figure it will be good enough until I learn something cooler. I can think of several ways to extend it, but don’t need them.

  6. Avatar
    Cazare in Bucuresti over 4 years later:

    Unlike most Python programmers I actually have a lot of separate directories and modules.) It recursively searches the current directory for things to test and runs them all at once.

  7. Avatar
    okey oyunu oyna over 4 years later:

    Certainly, your posts are nice for us. Thanks

    Tüm dunyadaki okey oyunculari ile ayni platform içerisinde sohbet ederek canli okey oyunu oyna ve ve internette online oyun oynamanin zevkini çikar.

  8. Avatar
    Monster Beats By Dr. Dre Studio over 4 years later:

    The Monster Beats By Dr. Dre Studio Headphones are classic shoes.You will be proud of owning them. Don’t hesitate!Take Monster Beats By Dr. Dre Solo Headphones home!

  9. Avatar
    christian louboutin shoes on sale over 4 years later:

    Have the christian louboutin patent leather pumps is a happy thing. Here have the most complete kinds of christian louboutin leather platform pumps.

  10. Avatar
    beats by dr dre over 5 years later:

    It recursively searches the current directory for things to test and runs them all at once.beats by dr dre beats by dre sale

  11. Avatar
    Cathy over 5 years later:

    Brand fashion Men G-star Jackets from China for wholesale on line store

  12. Avatar
    DR OZ african Mango over 5 years later:

    hmm ,i’m not sure if this is what i’m looking for but anyway this is interresting and could be useful some day,thanks for taking time to write such cool stuff

  13. Avatar
    iPhone contacts backup over 5 years later:

    http://www.uscriminalrecord.com/

  14. Avatar
    MOV Converter over 5 years later:

    I wanted to follow up and allow you to know how , a great deal I treasured discovering your web site today. , free download mov converter to convert mov to video and all video to mov format

Comments