1

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?

3
  • Is the C program going to be a subprocess, or is it a library that the Python code will invoke directly?CommentedApr 25, 2012 at 8:48
  • @DonalFellows sorry I have no idea at all. I think whatever can achieve the goal is fine.
    – Gnijuohz
    CommentedApr 25, 2012 at 9:11
  • Good question/idea - using a high level language to test a program written in low level languages makes sense to me. If the low level is low enough, than crossing the language border might be worth it.CommentedApr 25, 2012 at 9:47

2 Answers 2

1

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

    0

    Hmmm... If I'll have same task, I will solve it just by calling C program with different params in python. Like this:

    import commands params_results = [(1,2), (2,3), (3,4)] for param in pamams_results: out = commands.getoutput('mycompiledprogram %s' % param[0]) assert out == param[1] 
    1
    • 3
      commands is deprecated since python2.6 and you should use subprocess instead.
      – Zenon
      CommentedApr 27, 2012 at 2:29

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.