Hello folks,

i'm attaching a sample very small unit tests using both check unit testing
library and the current curl testing framework integrated with curl, i was
hoping we could asses both and see which is more suitable to write unit
tests.

the code uses check is in the file llist_test.c. It's a classic unit testing
code, with all basic functions there setup, teardown and macros for defining
your tests, the output is well organized, you can see sample output for both
all success, and one failed test in the attached files.
The only downside for using check is that we're adding an extra dependency
to curl, my suggesstion is to add the .so version and check.h to the code
base.

the other option is to use the testing setup already in libtest directory,
it's pretty easy to add more tests, but it's not as well-organized as using
check, all the tests use a function "int test(char *URL)" which may not be
suitable for unit tests.

personally, i'm into using check. I'd love to hear what you guys think?

*Amr*
Running suite(s): Curl_llust
0%: Checks: 1, Failures: 1, Errors: 0
llist_test.c:28:F:Core:test_llist_initiate:0: list initial size should be zero
/*****************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 */

#include "test.h"

#include "memdebug.h"
#include "llist.h"

int test(char *URL)
{
  struct curl_llist *llist;
  
  llist = Curl_llist_alloc( NULL );
  
  if( llist->size != 0 ) {
    fprintf(stderr, "llist size didn't initiate to zero\n");
    return TEST_ERR_MAJOR_BAD;
  }
  
  if( llist->head != NULL ) {
    fprintf(stderr, "llist head didn't initiate to NULL\n");
    return TEST_ERR_MAJOR_BAD;
  }
  
  if( llist->tail != NULL ) {
    fprintf(stderr, "llist tail didn't initiate to NULL\n");
    return TEST_ERR_MAJOR_BAD;
  }
  
  if( llist->dtor != NULL ) {
    fprintf(stderr, "llist dtor didn't initiate to NULL\n");
    return TEST_ERR_MAJOR_BAD;
  }
  
  return 0;
  
}

#include <stdlib.h>
#include "curl_config.h"
#include "setup.h"

#include "llist.h"

#include <check.h>

struct curl_llist *llist;

void test_curl_llist_dtor(void *key , void *value)
{
  // nothing for now
}

void setup( void )
{
  llist = Curl_llist_alloc( test_curl_llist_dtor );
}

void teardown( void )
{
  free( llist );
}

START_TEST( test_llist_initiate )
{
  fail_unless( llist->size == 0 , "list initial size should be zero" );
  fail_unless( llist->head == NULL , "list head should initiate to NULL" );
  fail_unless( llist->tail == NULL , "list tail should intiate to NULL" );
  fail_unless( llist->dtor == test_curl_llist_dtor , "list dtor shold initiate to test_curl_llist_dtor" );
}
END_TEST

Suite *
llist_suit (void)
{
  Suite *s = suite_create ("Curl_llust");

  /* Core test case */
  TCase *tc_core = tcase_create ("Core");
  tcase_add_checked_fixture (tc_core, setup, teardown);
  tcase_add_test (tc_core, test_llist_initiate);
  suite_add_tcase (s, tc_core);


  return s;
}

int main( void )
{
  int number_failed = 0;
  Suite *s = llist_suit ();
  SRunner *sr = srunner_create (s);
  srunner_run_all (sr, CK_NORMAL);
  number_failed = srunner_ntests_failed (sr);
  srunner_free (sr);
  return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
Running suite(s): Curl_llust
100%: Checks: 1, Failures: 0, Errors: 0
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html

Reply via email to