1 Intro
L'idée est d'utiliser kcov afin d'obtenir la couverture des tests de non-regression sur un projet bash,
comme le fait gcov pour les projets C.
2 Installation
La version de Debian n'est pas à jour.
$ git clone https://github.com/SimonKagstrom/kcov.git
$ cat INSTALL.md
# apt-get install binutils-dev libcurl4-openssl-dev zlib1g-dev libdw-dev libiberty-dev
$ cd /path/to/kcov/source/dir
$ mkdir build
$ cd build
$ cmake ..
$ make
# make install
3 Test
$ cat >test.sh <<EOF
#!/bin/bash
echo coucou
exit
echo bof
EOF
$ chmod +x test.sh
$ kcov out ./test.sh
Le résultat est consultable via la page file:///.../out/index.html
4 Intégration aux tests
L'idée est de l'appliquer à une librairie appellée via une baterie de tests.
$ cat >lib.sh <<EOF
#!/bin/bash
function func1 {
echo func1
}
function func2 {
echo func2
}
EOF
$ cat >ut1.sh <<EOF
#!/bin/bash
. lib.sh
func1
echo ok
EOF
$ cat >ut2.sh <<EOF
#!/bin/bash
. lib.sh
func2
echo ok
EOF
$ chmod +x ut*.sh
$ for I in 1 2; do kcov --exclude-pattern=ut out ut$I.sh; done
Le résultat est consultable via la page file:///.../out/kcov-merged/index.html
5 Framework de test
Il s'agit de remplacer l'architecture ``maison'', à savoir :
- un fichier résultat,
- un script supplémentaire appelant la librairie,
- et faisant un diff sur les résultats obtenus par rapport aux résultats attendus
Sous Debian on a bats de disponible.
# apt-get install bats
$ man 7 bats
$ cat >ut1.bats <<EOF
#!/usr/bin/env bats
. lib.sh
@test "call func1" {
result="$(func1)"
[ "$result" == "func1" ]
}
@test "re call func1" {
result="$(func1 bof)"
[ "$result" == "func1" ]
}
$ cat >ut2.bats <<EOF
#!/usr/bin/env bats
. lib.sh
@test "call func2" {
result="$(func2)"
[ "$result" == "func2" ]
}
$ bats -t ut*.bats
1..3
ok 1 call func1
ok 2 re call func1
ok 3 call func2
Intégration de kcov :
$ rm -fr out
$ for I in 1 2; do \
kcov --bash-dont-parse-binary-dir --exclude-pattern=bats out bats -t ut$I.bats; \
done
Le résultat est consultable via la page file:///.../out/index.html
|