3

I'm trying to automate some tests with Unity.

For example, I have a C program that works like a very basic calculator with operations such as division, etc.

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

  • 4 / 2 = 2 (test 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)

I would like to use Unity to automate this tests.

How can I achieve this? I don't get any correct result. I started to learn the Unity basics doing some simple examples 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 I use Unity to interact with 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 division 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");
}

Test division:

void run_test_division_positive_integers(int expected, int x, int y){
 UNITY_BEGIN();
 int actual;
 actual = x/y;
 TEST_ASSERT_EQUAL(expected, actual);
 return UNITY_END();
}
 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;
 run_test_division_positive_integers(d, a,b)
 printf("\nDivision of entered numbers=%d\n",d);
}
c32hedge
2,70920 silver badges39 bronze badges
asked Nov 24, 2016 at 10:56
4
  • Your code is not complete, what does teste_theFirst do? what does teste_2 do? Commented Nov 24, 2016 at 22:58
  • Thanks for your answer. That was a typo, it was supossed to be test1. But that example I understood, I think that should be the basic of unity. But a more real example, with interaction with a program like the calculator one I dont see any example how to do this. Commented Nov 24, 2016 at 23:17
  • yeah, you can not simply test this calculator application using Unity out of box. As this calculator takes input from the standard input, e.g. keyboard. You may need to find a workaround. Commented Nov 24, 2016 at 23:21
  • Thanks again. But for example, the inputs will be always the same. I will have always for example 4 / 2 = 2 to test the division of integeres. Even in this case its not possible to use unity for this? Commented Nov 24, 2016 at 23:27

1 Answer 1

2

There is no quick easy answer.

You will need to write a small Unity application to interact with this calculator application,

  1. Start this Calculator application
  2. Start your own Unity application
  3. Your application will locate this calculator's command line window and interact with it by providing inputs, e.g. your own Unity application will type in "2", "Enter", "+", "Enter", "2" to this Calculator application's command line window
  4. Your application will read the result back from the calculator application's command line window and verify it.

This is not a quick easy task.

#include "unity.h"
void test1(void)
{
 int firstInput = 2;
 int secondInput = 2;
 int expectedResult = 4;
 int actualResult = 0;
 actualResult = TypeInputsIntoCalcualtorCMD(firstInput,secondInput); //you will have to implement this function yourself.
 TEST_ASSERT(expectedResult==actualResult);
}
int main()
{
 Locate_Calculator_CMD(); //you will have to implement this function yourself.
 UNITY_BEGIN();
 RUN_TEST(test1);
 return UNITY_END();
}

OR

You can modify the Calculator's source code by changing its input source from the standard input to your Unity functions. But this is a really dangerous idea to take.

answered Nov 24, 2016 at 23:46
1
  • Thanks for your answer really. I dont have much experience with c so I think that it will be difficult to implement that function but I wil try anyway. But in any case, can you give an example of the other option that you said that is not go idea? It is someting like what I update in the question? Commented Nov 25, 2016 at 0:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.