Carlos Ble

Carlos Ble

I am a professional software developer, I solve problems.

I also teach and mentor developers to build better software.

Developing software since 2001.

Can I help you?

  • Do you need high quality tailor-made software?
  • Need training on TDD, clean code or refactoring?
  • Do you need a technical consultant?
  • May I pair with you to write better code?

Events

Upcoming training courses:

  1. TDD - [en Español] - 6 y 7 Octubre
    Gran Canaria
  2. TDD - [in English] - October 20, 21 & 22
    London, UK
  3. TDD - [en Español] - 29, 30 y 31 de Octubre.
    Madrid, Spain

Conferences:

  1. I'll be at the Agile Testing Days 2014
  2. I'll be at the London Test Gathering Workshops.

Pyunit test suite builder

Pyunit is a unit tests framework for Python. I believe it is the standard one. It works very well, however to run all the tests within a folder might be not trivial. You can do either hardcode the tests names to build the test suite or use this helper. The helper uses introspection (what is called reflection on .Net):

  1.  
  2. import unittest
  3. import os
  4. import re
  5. import sys
  6. import inspect
  7. import types
  8.  
  9. def makeSuite(testsFolder = './tests', testsPackageName = 'tests', testClassSuffix='Tests'):
  10. """Given the testsFolder parameter, loads all the .py files to search for classes
  11. which name contains testClassSuffix and inherit from unittest.TestCase.
  12. It takes these classes to buid the TestSuite and run all the tests.
  13. """
  14. testClasses = []
  15. testModules = {}
  16. testPackage = None
  17. suite = unittest.TestSuite()
  18. folder = os.listdir(testsFolder)
  19. for moduleName in folder:
  20. modulePath = os.path.join(testsFolder, moduleName)
  21. if not os.path.isdir(modulePath) and re.search(".py$", moduleName) :
  22. fullModuleName = testsPackageName + '.' + moduleName[:-3]
  23. # curious: this imports the package not the module
  24. testPackage = __import__(fullModuleName)
  25. # it is weird indeed as __import__('packageName') does not load its modules
  26. for packageItem in dir(testPackage):
  27. if re.search(testClassSuffix, packageItem):
  28. if not testModules.has_key(packageItem):
  29. testModules[packageItem] = True
  30. moduleHandler = getattr(testPackage, packageItem)
  31. for className, classHandler in inspect.getmembers(moduleHandler, callable):
  32. if inspect.isclass(classHandler) and issubclass(classHandler, unittest.TestCase):
  33. testClasses.append(classHandler)
  34. for classHandler in testClasses:
  35. for name, value in inspect.getmembers(classHandler, callable):
  36. if re.match("test", name):
  37. print " - Test:", name
  38. suite.addTest(classHandler(name))
  39. return suite
  40.  
  41. if __name__ == "__main__":
  42. suite = makeSuite()
  43. runner = unittest.TextTestRunner()
  44. runner.run(suite)
  45.  
  46.  
  47.  
Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.
  • http://jadarios.wordpress.com esauro

    Django extends unittest framework adding some web application specific features.
    It uses conventions to create automatically the test suite (the tests must be located inside models.py or tests.py and be subclasses of unittest.TestCase). As well as this you can use doctests too.

  • http://carlosble.com Carlos Ble

    Thanks mate,
    I’m using Django for the ORM only so far. Most parts of SIGA project don’t have to do with web interfaces.
    Are there more features in the django unit testing frammework that are not in Pyunit? (other than the web acceptance testing)

  • http://jadarios.wordpress.com esauro

    I think you may found interesting how Django manage databases in tests suites. I use empty database filled with fixtures data therefore you can be sure of certain data are in the database. You can check the documentation for more. http://www.djangoproject.com/documentation/testing/

  • http://www.thetoryparty.com Tory

    Fantastic! This is exactly what I needed. Before committing, my team is much more likely to run one test script than twenty. Thank you so, so much.

  • http://carlosble.com Carlos Ble

    Glad you find it useful! :-)

  • http://www.bestuggcheapsale.com/ cheap ugg boots

    Your RSS feed doesn’t work in my browser (google chrome) how could I correct it?