17.12.2012 Views

Programmation PYTHON - Zenk - Security - Repository

Programmation PYTHON - Zenk - Security - Repository

Programmation PYTHON - Zenk - Security - Repository

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Programmation</strong> dirigée par les tests<br />

CHAPITRE 12<br />

options = [{'noms': ('-r', '--repertoire'), 'dest': 'rep',<br />

'help': ('Spécifie le répertoire à utiliser, si non fourni, '<br />

'le chemin courant est utilisé ')}]<br />

def _print_line():<br />

print('-' * 70)<br />

def main(options, arguments, parser):<br />

if options.rep isnot None:<br />

chemin = options.rep<br />

else:<br />

if len(arguments) > 0:<br />

print parser.usage<br />

sys.exit(2)<br />

chemin = getcwd()<br />

chemin = os.path.normpath(chemin)<br />

if chemin.endswith(os.path.sep):<br />

chemin = chemin[:-1]<br />

print('Parcours du répertoire')<br />

test_modules = []<br />

for racine, reps, fichiers in walk(chemin):<br />

dossier = os.path.basename(racine)<br />

if dossier == 'tests':<br />

for fichier in fichiers:<br />

if (fichier.startswith('test') and<br />

fichier.endswith('.py') and<br />

fichier != 'test.py'):<br />

nom_complet = os.path.join(racine, fichier)<br />

test_modules.append((nom_complet, fichier[:-3]))<br />

sys.stdout.write('.')<br />

sys.stdout.flush()<br />

print('\n%d module(s) de test trouvé(s)\n' % len(test_modules))<br />

suite = unittest.TestSuite()<br />

dernier_contexte = None<br />

added_paths = []<br />

for module in test_modules:<br />

module_path = os.path.dirname(module[0])<br />

# chargement d'un script si nécessaire<br />

contexte = os.path.join(module_path, 'contexte.py')<br />

if os.path.exists(contexte) and dernier_contexte != contexte:<br />

execfile(contexte)<br />

dernier_contexte = contexte<br />

409

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!