Python Testing – How to Automate Testing of a C Program Using Python

cpythontest-automationtesting

I was wondering if I can use a Python script to test a C program automatically.

Suppose I have a very simple C program which reads data (numbers as test cases) from the console, calculates them, then outputs it to the console. Now I want to read data from a file then output whatever the program outputs into a file.

Suppose in the original C program I use while loop andscanf to read two numbers into two variables a and b for several times and do different calculations according to the values of a and b, like this:

if(a>4 && a<10){...}
else if(a>=10){...}

Now can I use a Python script to test the program automatically? And do I need to modify my C program? How?

EDIT: If any method is permitted, then what is the best way to test the C program automatically without using frameworks?

Best Answer

might be a bit off topic but you could try looking up the ctypes library in python, it's a means of python interacting with c/c++ programs to call routines in headers, dlls etc..

I use it mainly for programming windows applications in python using the windows api, hope it helps !!

Here's the link:

Python documentation