jeudi 24 novembre 2016

write unit test in c using unity

Im trying to automate some tests with unity.

For example I have a c program that works like a very basic calculator where I have for example the division operation.

And I would like to automate the test envioronment. I have some test cases, where the inputs of the program will be always the same. And I want to verify if the output is icual to what was expected. So for the division I have some test cases:

4 / 2 = 2 (teste case to test division with positive numbers)

-40 / -10 = 4 (test case to test division with negative numbers)

40 / 0 = "error msg" (test case to test division by zero)

Now I would like to use unity to automate this tests.

Can you give some help on how we can achieve this? Im trying to do this but I dont gey any correct result. I started to learn the unity basics doing some simple exmaples like:

   #include "unity.h"

    void test1(void)
    {
        TEST_ASSERT(1==1);
        TEST_ASSERT(2==2);
    }
    void test2(void)
    {
       TEST_ASSERT_EQUAL(2,2);
       TEST_ASSERT_EQUAL_INT(2,3);
       TEST_ASSERT_EQUAL_FLOAT(1.1,1.2);
    }

    int main()
    {
        UNITY_BEGIN();
        RUN_TEST(teste_theFirst);
        RUN_TEST(teste_2);
        return UNITY_END();
    }

But how can we use unity to interact to a command line program? For example I have a part of the calculator program relative to the division operation, below:

calc.c:

void division();

printf("Enter / symbol for Division \n");

void division()
{
    int a, b, d=0;
    printf("\nPlease enter first number  : ");
    scanf("%d", &a);
    printf("Please enter second number : ");
    scanf("%d", &b);
    d=a/b;
    printf("\nDivision of entered numbers=%d\n",d);
}

The other part of the program not so relevant for the divison example:

int main()
{
    int X=1;
    char Calc_oprn;
    // Function call
    calculator_operations();
    while(X)
    {
        printf("\n");
        printf("%s : ", KEY);
        Calc_oprn=getche();
        switch(Calc_oprn)
        {
            case '+': addition();
                      break;



            case '/': division();
                      break;

            case 'H':
            case 'h': calculator_operations();
                      break;

            case 'Q':
            case 'q': exit(0);
                      break;
            case 'c':
            case 'C': system("cls");
                      calculator_operations();
                      break;

            default : system("cls");

    printf("\n**********You have entered unavailable option");
    printf("***********\n");
    printf("\n*****Please Enter any one of below available ");
    printf("options****\n");
                      calculator_operations();
        }
    }
}
//Function Definitions
void calculator_operations()
{
    //system("cls");  use system function to clear
    //screen instead of clrscr();
    printf("\n             Welcome to C calculator \n\n");

    printf("******* Press 'Q' or 'q' to quit ");
    printf("the program ********\n");
    printf("***** Press 'H' or 'h' to display ");
    printf("below options *****\n\n");
    printf("Enter 'C' or 'c' to clear the screen and");
    printf(" display available option \n\n");
    printf("Enter * symbol for Multiplication \n");
    printf("Enter / symbol for Division \n");

}

Aucun commentaire:

Enregistrer un commentaire