17.12.2012 Views

Programmation PYTHON - Zenk - Security - Repository

Programmation PYTHON - Zenk - Security - Repository

Programmation PYTHON - Zenk - Security - Repository

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Script de test<br />

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

CHAPITRE 12<br />

Le script de lancement des tests unitaires vu précédemment peut être modifié pour<br />

prendre en compte les doctests des modules de code rencontrés sur le chemin, et les<br />

fichiers textes de tests. Dans l’extension proposée, ces derniers doivent être préfixés<br />

par test et suffixés par .txt.<br />

doctest fournit des objets permettant de transformer les tests extraits des docstrings<br />

en objets de type TestCase, qui peuvent être insérés dans les test suites.<br />

Script tester.py modifié<br />

import doctest<br />

[...]<br />

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

[...]<br />

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

test_modules = []<br />

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

for fichier in fichiers:<br />

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

fichier notin ('test.py', 'test.txt')):<br />

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

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

test_modules.append((nom_complet, fichier.split('.')[0],<br />

tests))<br />

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

sys.stdout.flush()<br />

[...]<br />

for fichier, module, dossier_tests in test_modules:<br />

module_path = os.path.dirname(fichier)<br />

if module_path notin sys.path:<br />

sys.path.append(module_path)<br />

added_paths.append(module_path)<br />

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

if dossier_tests:<br />

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

else:<br />

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

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

execfile(contexte)<br />

dernier_contexte = contexte<br />

423

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

Saved successfully!

Ooh no, something went wrong!