C++
Unit-Test Frameworks
Google Test
TODO
C++14 features
- Catch (C++ Automated Test Cases in Headers)
- Mettle
- 73 C++ UT frameworks listed on Wikipedia
Catch
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
TEST_CASE("A year is not a leap year if it is not divisible by 4", "[leap]") {
REQUIRE( is_leap_year(2015) == false );
}
TEST_CASE("A year is a leap year if it is divisible by 4 but not by 100", "[leap]") {
REQUIRE( is_leap_year(2016) == true );
}
TEST_CASE("A year is not a leap year if it is divisible by 100 but not by 400", "[leap]") {
REQUIRE( is_leap_year(1900) == false );
}
TEST_CASE("A year is a leap year if it is divisible by 400", "[leap]") {
REQUIRE( is_leap_year(2000) == true );
}
TODO Fournir plus d'exemples sur la puissance de Catch
Mettle
#include <mettle.hpp>
mettle::suite<> basic("Leap Year", [](auto &_) {
_.test("A year is not a leap year if it is not divisible by 4", []() {
mettle::expect(false, is_leap_year(2015));
});
_.test("A year is a leap year if it is divisible by 4 but not by 100", []() {
mettle::expect(true, is_leap_year(2016));
});
_.test("A year is not a leap year if it is divisible by 100 but not by 400", []() {
mettle::expect(false, is_leap_year(1900));
});
_.test("A year is a leap year if it is divisible by 400", []() {
mettle::expect(true, is_leap_year(2000));
})
}
lest
lest errors escape testing
https://github.com/martinmoene/lest
Tester les invariants
Couverture de données
Les premiÚres implémentations en C++ ont été C++ QuickCheck, puis AutoCheck.
Aujourdâhui, câest le projet RapidCheck qui est le plus actif et propose le plus de fonctionnalitĂ©s.
RapidCheck
TODO Fournir quelques exemples
- https://github.com/emil-e/rapidcheck
Mock
Google Mock
TODO Fournir exemples
Trompeloeil
- Header only C++14 mocking framework
- GitHub
Mockaron
C++ Great Practices
PrĂ©voire un build-type âCoverageâ
 | Release | Debug | Coverage |
---|---|---|---|
assert() |
-D NDEBUG |
 | -D NDEBUG |
Optimisation | -Ofast (or any other) |
-O0 -fno-inline |
-O0 -fno-inline |
Autres options de compilation
Les mĂȘmes pour les trois build types
- Toujours les symboles de debug =>
-g3 -fno-eliminate-unused-debug-types
(les binaires peuvent ĂȘtre strippĂ©s avant livraison au client) - Câest fini les
i386
=>-march=sandybridge
(Ă adapter selon le contexte)
Pousser les bugs à se révéler
Utiliser différents compilateurs
- MSVC sous Windows (et un autre ?)
- GCC et Clang sous Linux
Multiplier les combinaisons
- GCC, Clang
- Release, Debug, Coverage
- Valgrind ou pas (tester différentes options)
Definition of Done (DoD)
- Source code respects Coding Rules
- No remaining FIXME
- No compiler warnings
- Unit-Tests passed
- Code coverage depending on context
- Valgrind
- CppCheck
- AddressSanitizer using
-fsanitize=*
(clang and gcc) - clang-check
(static code analyzer of control flow graph to detect bug-prone patterns) - clang-tidy
(linter: coding style, readability and fix your C++ code) - No dead code