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);
}
1 Answer 1
There is no quick easy answer.
You will need to write a small Unity application to interact with this calculator application,
- Start this Calculator application
- Start your own Unity application
- 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
- 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.
-
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?Ozzy– Ozzy2016年11月25日 00:07:27 +00:00Commented Nov 25, 2016 at 0:07
Explore related questions
See similar questions with these tags.
teste_theFirst
do? what doesteste_2
do?