2

The function add_first(int n) adds a number to a linked list and it returns 0 if successful and -1 if not.

The test is good but I would like to test a wider variety of situations so to test more of the same function with different parameters.

If I modify test_add_first and add a parameter it gives me an error because MY_RUN_TEST and RUN_TEST will only accept "myfunction(void)". Therefore I have to create individual functions for each number.

I have this code in my test.c file and I am using Unit testing - Unity as the title says.

#define MY_RUN_TEST(func) RUN_TEST(func, 0)

ITEM *list = NULL;

void printList(struct ITEM *node) 
{
  if(node == NULL)
  {
    printf("list is empty");
  } 
    while (node != NULL) 
    { 
        printf("%d ", node -> value); 
        node = node->next; 
    } 
} 

void test_add_first_1()
{
  int ret;
  ret = add_first(&list, 1);
  TEST_ASSERT_EQUAL(0, ret);
}

void setUp(void)
{
    // This is run before EACH test
}

void tearDown(void)
{
  // This is run after EACH test
  printList(list);
  printf("\n");
}

int main (int argc, char * argv[])
{
    
    
    UnityBegin();
   
    MY_RUN_TEST(test_add_first_1);
    
    return UnityEnd();
}

So my question to be more specific is there any way to test these as something like - MY_RUN_TEST(test_add_first(n)); - n being an integer?

If you find something else wrong at this piece of code please tell I am just starting with Unit Testing. :)

Thank you!

1 Answer 1

3

You wont be able to pass a function with different signature to RUN_TEST, but fortunately you don't have to.

What you can do is simply make a more general test case which tries to insert multiple items:

void add_some_items_at_head()
{
    size_t i = 0;
    
    for (i = 0; i < 10; i++)
    {
        TEST_ASSERT_EQUAL(0, add_first(&list, i));
    }   
}

If you want to test specific numbers you can put them in a const array and loop through that array, testing each of these numbers.

By the way if you asked for general improvements that you can make here, I can suggest a few things:

  • Even though you checked for the return code of the function, it might have not actually added the item it got, or it might have not done it well. So I'd make a utility function to loop through the list, and ensure that the items were inserted in the correct order. It can look something like that:

     void validate_items_order(int expected_order[], int len)
     {
         size_t i = 0;
         struct ITEM *node = list;
    
         for (i = 0; i < len; i++)
         {
             TEST_ASSERT_NOT_NULL(node);
             TEST_ASSERT_EQUAL(expected_order[i], node->value);
             node = node->next;
         }
     }
    

    Then call this function after each test to make sure everything works fine.

  • Use test groups to make the test code more organized and readable. A simple thing you can do is make a test group for each function you test, and have it contain all the tests for that specific function:

      static void RunAllTests(void)
      {
          RUN_TEST_GROUP(add_first);
          RUN_TEST_GROUP(add_last);
          RUN_TEST_GROUP(remove_last);
      }
    
      int main(int argc, const char * argv[])
      {
          return UnityMain(argc, argv, RunAllTests);
      }
    
      TEST_GROUP_RUNNER(add_first)
      {
          RUN_TEST_CASE(add_first, add_some_items_at_head);
          RUN_TEST_CASE(add_first, another_test_for_that_function);
      }
    
      TEST_GROUP_RUNNER(add_last)
      {
          ...
      }
    
      TEST_GROUP_RUNNER(remove_last)
      {
          ...
      }
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.