- January 7, 2010
- dynamic unittest load test loader loading unit
- 1 (after 1 ratings)
Ok... this is really a hack. But I love it. I hate setting up all of my test cases into suites, and making sure that I remember to add them each time I add a new python file... annoying! This allows me to have a tests package and then just add python files and packages to that test package (mirroring my app setup). Each of the files are then dynamically imported and every test case is automatically executed. If you don't want one to execute, add it to the ignore list. If you add 'views' to the ignore list, it will ignore all views, otherwise you would have to specify 'package.views' if it is in a package.
So... in short this is a bit ghetto, but it saves me a lot of time just setting up all my tests... now I can just write them! Hope it's useful to someone.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
# testhelper.py def get_sub_modules(f, ignore = , base = None): import os modules =  top = os.path.split(f) for root, dirs, files in os.walk(top, topdown=False): for name in files: if '.svn' in root: continue if not name.endswith('.py'): continue pkg = root.replace(top, '') if pkg.startswith(os.sep): pkg = pkg[1:] pkg = pkg.replace('/', '.').replace('\\', '.') module = os.path.splitext(name) if name.startswith('__') or name in ignore or module in ignore: continue path = '.'.join([pkg, module]) if len(pkg.strip()) == 0: path = module if base is not None: path = '.'.join([base, path]) modules.append(path) return modules # app.tests.__init__.py import testhelper modules = testhelper.get_sub_modules(__file__, base='time_punch.tests') for m in modules: exec('from %s import *' %(m))
More like this
- Automatically setup raw_id_fields ForeignKey & OneToOneField by agusmakmun 6 months, 2 weeks ago
- Crispy Form by sourabhsinha396 7 months, 1 week ago
- ReadOnlySelect by mkoistinen 7 months, 3 weeks ago
- Verify events sent to your webhook endpoints by santos22 8 months, 2 weeks ago
- Django Language Middleware by agusmakmun 9 months ago