úterý 20. ledna 2009

Jednoduchý Unit framework pro C/C++

Máte rádi Test Driven Development jako já? Používáte v Javě JUnit a chcete něco podobného v C? Dal jsem si ten čas a projel všechny frameworky, které mi google nabídnul. Na první pohled jsem si oblíbil následující 2:

Pro začátek nemusíme definovat, TestRunner, tj. říci které všechny testy se mají spustit. U simplectests je toto implementováno pomocí header files a preprocesoru, kdy za makro definující testovací metodu dosazují main(). Jednoduché a účinné :) Simplectest mi přijde nejminimalističtější řešení jaké si dokážu představit, naproti tomu Easyunit toho umí víc, je psaný v C++ a to čitelně pro javistu.

Hello world v simplectests

/*
* This demonstrates the simplest way to do some tests using simplectest.
* See "readme.txt" for more information, or the website at
* http://simplectest.sf.net/.
*
* Jevon Wright 2004-05
*/
#include "tests.h"

// Start the overall test suite
START_TESTS()

// A new group of tests, with an identifier
START_TEST("simple")
  // We then write the tests we want to check
  ASSERT(1 == 1);
  ASSERT_EQUALS_FLOAT(1, 1);
END_TEST()

START_TEST("fail")
  // These tests will fail, and we will get output.
  ASSERT(1 == 0);
  ASSERT_EQUALS_FLOAT(1, 0);

  // Lets give a description of the test, before it
  // fails - this will be printed out instead.
  TEST("we expect this test to fail. (3==2)");
  ASSERT(3 == 2);
END_TEST()

// End the overall test suite
END_TESTS()

Hello world v Easyunit

stacktest.cpp

#include "easyunit\test.h"
#include "stack.h"

using namespace easyunit;

TEST(Stack,Constructor)
{
  Stack s;
  ASSERT_TRUE(s.size() == 0);
  ASSERT_TRUE(s.pull() == 0);
}

TEST(Stack, PushTop)
{
  Stack s;
  s.push(1);
  ASSERT_TRUE(s.top() == 1);
  s.push(2);
  s.push(3);
  ASSERT_TRUE(s.top() == 3);
} 

main.cpp

#include "easyunit\testharness.h"

using namespace easyunit;

int main()
{
  TestRegistry::runAndPrint();
} 

Žádné komentáře: