示例#1
0
def main(argv=None):
	'''Run either all tests, or those specified in argv'''
	if argv is None:
		argv = sys.argv

	# parse options
	coverage = None
	alltests = True
	loglevel = logging.WARNING
	opts, args = getopt.gnu_getopt(argv[1:], 'hVD', ['help', 'coverage', 'fast', 'debug', 'verbose'])
	for o, a in opts:
		if o in ('-h', '--help'):
			print '''\
usage: %s [OPTIONS] [MODULES]

Where MODULE should a module name from ./tests/
If no module is given the whole test suite is run.

Options:
  -h, --help     print this text
  --fast         skip a number of slower tests
  --coverage     report test coverage statistics
  -V, --verbose  run with verbose output from logging
  -D, --debug    run with debug output from logging
''' % argv[0]
			return
		elif o == '--coverage':
			try:
				import coverage as coverage_module
			except ImportError:
				print >>sys.stderr, '''\
Can not run test coverage without module coverage.
On Ubuntu or Debian install package 'python-coverage'.
'''
				sys.exit(1)
			coverage = coverage_module
			coverage.erase() # clean up old date set
			coverage.exclude('assert ')
			coverage.exclude('raise NotImplementedError')
			coverage.start()
		elif o == '--fast':
			alltests = False
		elif o in ('-V', '--verbose'):
			loglevel = logging.INFO
		elif o in ('-D', '--debug'):
			loglevel = logging.DEBUG
		else:
			assert False

	# Set logging handler
	logging.basicConfig(level=loglevel, format='%(levelname)s: %(message)s')

	# Set environment - so we can be sure we don't see
	# any data from a previous installed version
	tests.set_environ()

	# Collect the test cases
	suite = unittest.TestSuite()
	loader = FastTestLoader(alltests=alltests)

	if args:
		modules = [ 'tests.'+name for name in args ]
	else:
		suite.addTest(TestCompileAll())
		modules = [ 'tests.'+name for name in tests.__all__ ]

	for name in modules:
		test = loader.loadTestsFromName(name)
		suite.addTest(test)

	# And run them
	MyTextTestRunner(verbosity=3,
		ignored=loader.ignored, skipped=loader.skipped).run(suite)

	# Check the modules were loaded from the right location
	# (so no testing based on modules from a previous installed version...)
	mylib = os.path.abspath('./zim')
	for module in [m for m in sys.modules.keys()
			if m == 'zim' or m.startswith('zim.')]:
		if sys.modules[module] is None:
			continue
		file = sys.modules[module].__file__
		assert file.startswith(mylib), \
			'Module %s was loaded from %s' % (module, file)


	if coverage:
		coverage.stop()
		report_coverage(coverage)
示例#2
0
	def tearDown(self):
		tests.set_environ() # re-set the environment
		zim.config._set_basedirs() # refresh