Skip to content

Latest commit

 

History

History

11-cpp

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

C++

Unit-Test Frameworks

Google Test

TODO

C++14 features

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

Mock

Google Mock

TODO Fournir exemples

Trompeloeil

trompeloeil logo

  • 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