|
|
@@ -0,0 +1,141 @@
|
|
|
+# Utiliser Jenkins pour tester son code python
|
|
|
+
|
|
|
+> Testé avec Python 3.4
|
|
|
+
|
|
|
+## Librairies python:
|
|
|
+
|
|
|
+Librairies requises:
|
|
|
+
|
|
|
+* nose2 > `pip install nose2`
|
|
|
+* cov-core > `pip install cov-core`
|
|
|
+* junit-xml > `pip install junit-xml`
|
|
|
+* pylint > `pip install pylint`
|
|
|
+> si ImportError au lancement de pylint, executer aussi:
|
|
|
+> `pip install wrapt lazy_object_proxy --upgrade`
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+## Configurer son projet
|
|
|
+
|
|
|
+### Séparer le code des tests
|
|
|
+
|
|
|
+Créer une structure du type:
|
|
|
+
|
|
|
+* projet
|
|
|
+ * projet
|
|
|
+ * tests
|
|
|
+
|
|
|
+### Les tests et leur couverture avec nose2
|
|
|
+
|
|
|
+nose2 executera les tests de tous les fichiers dont le nom commence par 'test_'
|
|
|
+
|
|
|
+En ligne de commande windows: `nose2 --with-coverage --coverage=.\core`
|
|
|
+
|
|
|
+### La qualité du code
|
|
|
+
|
|
|
+pylint contrôle la qualité du code
|
|
|
+
|
|
|
+En ligne de commande windows: `pylint .\core`
|
|
|
+
|
|
|
+
|
|
|
+## Installer et configurer Jenkins
|
|
|
+
|
|
|
+### Installer jenkins
|
|
|
+
|
|
|
+Executer l'installeur Windows ([ici](https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+as+a+Windows+service))
|
|
|
+
|
|
|
+Vérifier que le service Jenkins est actif (dans le gestionnaire de taches)
|
|
|
+
|
|
|
+Pour le démarrer, redémarrer, arrêter, ou vérifier son status:
|
|
|
+
|
|
|
+ cd c:\Program Files\Jenkins\
|
|
|
+ jenkins start
|
|
|
+ jenkins restart
|
|
|
+ jenkins stop
|
|
|
+ jenkins status
|
|
|
+
|
|
|
+Accéder à l'interface via `localhost:8080`
|
|
|
+
|
|
|
+### Configurer jenkins
|
|
|
+
|
|
|
+Installer les plugins:
|
|
|
+
|
|
|
+* Cobertura
|
|
|
+* Git Plugin
|
|
|
+* Violations
|
|
|
+* PostBuildScript
|
|
|
+
|
|
|
+Cliquer sur `Télécharger et redémarrer ensuite`
|
|
|
+
|
|
|
+Dans `Configurer Jenkins`>`Configurer le système`
|
|
|
+
|
|
|
+* Git Plugin: renseigner user.name et user.email
|
|
|
+* Shell: `C:\Windows\system32\cmd.exe`
|
|
|
+
|
|
|
+
|
|
|
+### Créer le Job
|
|
|
+
|
|
|
+Cliquer sur `New Job` > `Freestyle project` > `OK`
|
|
|
+
|
|
|
+Sur la page de configuration du Job, choisir Git comme SCM, avec le chemin du repo local comme repository path
|
|
|
+
|
|
|
+`Add Build Step` > `Execute windows batch command`
|
|
|
+
|
|
|
+Ajouter au champs Command:
|
|
|
+
|
|
|
+ set PYTHONPATH=''
|
|
|
+ nose2 --plugin nose2.plugins.junitxml --junit-xml --with-coverage
|
|
|
+ python -m coverage xml --include=.\*
|
|
|
+ pylint -f parseable [nom_du_module] > pylint.log
|
|
|
+ exit 0
|
|
|
+
|
|
|
+> attention au module pylint: celui-ci doit comprendre un fichier \_\_init__.py'
|
|
|
+
|
|
|
+Puis pour interpréter les résultats:
|
|
|
+
|
|
|
+* `Add Post Build Step` > `Publish Cobertura Coverage Report`
|
|
|
+
|
|
|
+Dans Xml report pattern: `coverage.xml`
|
|
|
+
|
|
|
+
|
|
|
+* `Add Post Build Step` > `Publish JUnit test result report`
|
|
|
+
|
|
|
+Dans Test report xmls: `nose2-junit.xml`
|
|
|
+
|
|
|
+* `Add Post Build Step` > `Report Violations`
|
|
|
+
|
|
|
+Dans `pylint`: `**/pylint.log` *attention au type de slash*
|
|
|
+
|
|
|
+Dans Source Path pattern: `**/`
|
|
|
+
|
|
|
+* `Add Post Build Step` > `Execute a set of scripts` > `Build steps` > `Executer une commande windows`
|
|
|
+
|
|
|
+Insérer la commande:
|
|
|
+
|
|
|
+ echo off
|
|
|
+ setlocal enabledelayedexpansion
|
|
|
+ cd %JENKINS_HOME%\jobs\%JOB_NAME%\builds\%BUILD_NUMBER%\violations
|
|
|
+ for /f "delims=" %%l in (violations.xml) do (
|
|
|
+ set str=%%l
|
|
|
+ set str=!str:\=/!
|
|
|
+ echo !str! >> violations.tmp
|
|
|
+ )
|
|
|
+ ren violations.xml violations.old
|
|
|
+ ren violations.tmp violations.xml
|
|
|
+
|
|
|
+Cocher:
|
|
|
+
|
|
|
+* Execute script only if build succeeds
|
|
|
+* Mark build unstable instead of failed on script error
|
|
|
+
|
|
|
+> ce dernier script permet de corriger le fichier violations.xml et de le rendre lisible par Jenkins
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+### Lancer le Job
|
|
|
+
|
|
|
+Cliquer sur `Lancer le build`
|
|
|
+
|
|
|
+Les résultats de l'éxecution sont accessibles dans Console Output
|
|
|
+
|
|
|
+Depuis l'écran principal, on a accès aux résultats des tests, à leur couverture, ainsi qu'aux violations pylint
|