frameworks para c 3º desif

13
3º Encontro DeSif Teste Unitários em C Gleison Rodrigues [email protected]

Upload: gleison-rodrigues

Post on 20-Jul-2015

509 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Frameworks para C 3º DeSif

3º Encontro DeSif

Teste Unitários em C

Gleison [email protected]

Page 2: Frameworks para C 3º DeSif

3º Encontro DeSif

FrameWork de teste para C/C++

● Check: A unit testing framework for C.

● MinUnit: TDD em C, simples e prático.

● Googletest: Google C++ Testing Framework.

● Cgreen: A unit tester for the C software developer.

Page 3: Frameworks para C 3º DeSif

3º Encontro DeSif

Check● Versão 0.9.8

● Ultima atualização 23-09-2009

● Hospedado no sourceforge

● Faz uso de macros:

● Escrevendo um teste:

Page 4: Frameworks para C 3º DeSif

3º Encontro DeSif

#include <check.h>

START_TEST (test_name){ /* unit test code */}END_TEST

Page 5: Frameworks para C 3º DeSif

3º Encontro DeSif

MinUnit● Realmente minimalista:

apenas um aquivo MinUnit.h

● Pagina de referencia Jera.

● Escrevendo um teste:

Page 6: Frameworks para C 3º DeSif

3º Encontro DeSif #include <stdio.h> #include "minunit.h" int tests_run = 0; int foo = 7; int bar = 4; static char * test_foo() { mu_assert("error, foo != 7", foo == 7); return 0; } static char * test_bar() { mu_assert("error, bar != 5", bar == 5); return 0; } static char * all_tests() { mu_run_test(test_foo); mu_run_test(test_bar); return 0; }

Page 7: Frameworks para C 3º DeSif

3º Encontro DeSif

/* continuação */

int main(int argc, char **argv) {

char *result = all_tests();

if (result != 0) { printf("%s\n", result);

}else { printf("ALL TESTS PASSED\n"); }

printf("Tests run: %d\n", tests_run);

return result != 0;

}

Page 8: Frameworks para C 3º DeSif

3º Encontro DeSif

GoogleTest● Desenvolvido para C++

● Versão 1.6.0

● Hospedado no GoogleCode.

● Faz uso de macros.

Page 9: Frameworks para C 3º DeSif

3º Encontro DeSif #include <limits.h> #include "sample1.h" #include "gtest/gtest.h"

// Tests Factorial().

TEST(FactorialTest, Negative) { EXPECT_EQ(1, Factorial(-5)); EXPECT_TRUE(Factorial(-10) > 0);}

TEST(FactorialTest, Zero) { EXPECT_EQ(1, Factorial(0));}

TEST(FactorialTest, Positive) { EXPECT_EQ(1, Factorial(1)); EXPECT_EQ(2, Factorial(2)); EXPECT_EQ(40320, Factorial(8));}

Page 10: Frameworks para C 3º DeSif

3º Encontro DeSif

Cgreen● Note that this tool is for C programming, not C++.

● Tem um guia em português do Brasil

● Versão 1.0.0-beta2

● Hospedado no sourceforge

● Faz uso de macros

Page 11: Frameworks para C 3º DeSif

3º Encontro DeSif#include "cgreen/cgreen.h"

void this_test_should_pass() {assert_true(1);

}

void this_test_should_fail() {assert_true(0);

}

int main(int argc, char **argv) {TestSuite *suite = create_test_suite();

add_test(suite, this_test_should_pass);add_test(suite, this_test_should_fail);

return run_test_suite(suite, create_text_reporter());}

Page 12: Frameworks para C 3º DeSif

3º Encontro DeSif

Dúvidas???

Page 13: Frameworks para C 3º DeSif

3º Encontro DeSif