Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cours_prog
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
yassin.elhakoun
cours_prog
Commits
4b89aae2
Verified
Commit
4b89aae2
authored
4 years ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
started munit stuff
parent
87530fdc
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
slides/munit.md
+127
-0
127 additions, 0 deletions
slides/munit.md
with
127 additions
and
0 deletions
slides/munit.md
0 → 100644
+
127
−
0
View file @
4b89aae2
---
title
:
Framework de tests
date
:
2021-03-17
---
# Tests manuels (2/2)
## Instructions conditionnelles
Utilisation
*d'instruction conditionnelles*
pour les tests unitaires:
```
C
void test_add() { // test some add function
if (add(1, -2) != -1) {
printf("Error. Expected %d, Actual %d.\n",
-1, add(1, -2));
exit(-1);
}
}
int main() {
test_add();
}
```
```
bash
$
./tests
Error. Expected 1, Actual
-2
.
```
**Erreurs détaillées**
mais
*long à écrire*
et
*pas généralisable*
.
# Tests manuels (2/2)
## Assertions
Utilisation des
*assertions*
pour les tests unitaires:
```
C
void test_add() { // test some add function
assert(add(1, -2) == -1);
}
int main() {
test_add();
}
```
Exécution:
```
bash
$
./tests
tests.c:6: your_function:
Assertion
`
add
(
1,
-2
)
==
-1
' failed.
```
*Simple à écrire*
et
*généralisable*
mais
**pas détaillé**
.
# Frameworks de tests
Grand nombre de frameworks:
*
CUnit,
<https://sourceforge.net/projects/cunit/>
*
Unity,
<https://github.com/ThrowTheSwitch/Unity>
*
Criterion,
<https://github.com/Snaipe/Criterion/>
Ici on utilisera:
*
$
\m
u$nit,
<https://nemequ.github.io/munit/>
,
<https://github.com/nemequ/munit>
*
Simple à installer,
*
Simple à utiliser,
*
Erreurs détaillées et général.
# Petit tuto $\mu$nit
Structure possible de tests:
~~~
{.mermaid format=png}
graph TD;
lib.h --> lib.o
lib.h --> tests.o
lib.c --> lib.o
tests.c --> tests.o
tests.o --> tests
lib.o --> tests
munit/munit.h --> tests.o
munit/munit.h --> munit.o
munit/munit.c --> munit.o
munit.o --> tests
~~~
*Exercice:*
Écrire un
`Makefile`
avec une cible
`tests`
qui compile et exécute les tests.
# Utilisation simple
## Fichier `tests.c`
```
C
#include "munit/munit.h" // inclusion du header munit.h
int main() {
// Le code vient ici
}
```
Mais que mettre dans le code?
# Les tests les plus simples possibles
Une grande collection de macros
```
C
#include "munit/munit.h" // inclusion du header munit.h
int main() {
munit_assert_int(add(1, -2), ==, -1);
}
```
```
bash
$
./tests
ERROR> tests.c:5: assertion failed:
add
(
1,
-2
)
==
-1
(
-2
==
-1
)
```
Les
`munit_assert_TYPE()`
{.C} existent en plusieurs saveurs (
`char`
{.C},
`float`
{.C}, ...).
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment