Passing parameters for a C program in Linux.

V

venutaurus539

Hi all,
I have to write an automted script which will test my c
program. That program when run will ask for the commands. For example:

local-host# ./cli
Enter 1 for add
Enter 2 for sub
Enter 3 for mul
1 -------Our option
Enter two numbers
44 33 -------- Our option
Result is 77

As shown in the above example it lists all the options and waits for
user input and once given, it does some operations and waits for some
more. This has to be automated.

Can someone give suggestions how to do this in Python (Linux Platform
in particular).

Thank you,
Venu.
 
P

Petr Messner

Hi,

this can be done using module "subprocess"; there is also function
popen() in module "os" and module popen2, but they are deprecated
since Python 2.6.

PM
 
V

venutaurus539

Hi,

this can be done using module "subprocess"; there is also function
popen() in module "os" and module popen2, but they are deprecated
since Python 2.6.

PM

Can you give some more explanation about how exactly this can be
done..

Thanks for reply,
Venu
 
P

Petr Messner

2009/6/30 [email protected] said:
Can you give some more explanation about how exactly this can be
done..

Popen object enables you to run any program in a newly-created child
process, write to its standard input and read from its standard and
error outputs.

There is an example how your test program could look like:

import subprocess

p = subprocess.Popen("./cli", stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)

result, errs = p.communicate("1\n44 33\n")

assert not errs
assert result.splitlines()[-1] == "Result is 77"


Popen objects have also attributes stdin, stdout and stderr, you can
use them if you do not want to use communicate().

Be aware that you if you call stdout.read(), it reads everything and
blocks until EOF occurrs (usually until the popen-ed program quits).
You can also use readline(), but this can also block until the
subprocess writes any line. In cases where this could be a problem (i
think this automated test programs are not that cases) polling,
nonblocking I/O or threads can be used.

I have pasted complete example (with a script simulating your "cli"
program) here: http://paste.pocoo.org/show/125944/

PM
 
P

pdpi

Hi all,
       I have to write an automted script which will test my c
program. That program when run will ask for the commands. For example:

local-host# ./cli
Enter 1 for add
Enter 2 for sub
Enter 3 for mul
1            -------Our option
Enter two numbers
44 33  -------- Our option
Result is 77

As shown in the above example it lists all the options and waits for
user input and once given, it does some operations and waits for some
more. This has to be automated.

Can someone give suggestions how to do this in Python (Linux Platform
in particular).

Thank you,
Venu.

The easiest (and ugliest) way to do this would probably be to write a
file input.txt and a file output.txt with each input/output value, and
then to do this on the command prompt:
../a.out < input.txt | diff output.txt -

this will run a.out (assuming that's your program's name), feed in
input.txt as input, and pipe that into diff to compute the differences
between its output and output.txt
 
M

Miles Kaufmann

I have to write an automted script which will test my c
program. That program when run will ask for the commands.

Keep in mind that, if your test script checks the program's output
before giving it input, you can run into problems with buffering. The
standard C library uses line-based buffering when a program is using a
terminal for output, but when it's outputting to a pipe it uses block
buffering. This can be a problem when running a process using
subprocess--your program will buffer the prompt, and your test script
won't see it, so the test will deadlock. The problem can also exist
in the opposite direction.

Possible solutions:
- Explicitly set both your test script and your program to have line-
buffered output.
- Add a flush statement whenever you finish writing output and expect
input.
- Use pexpect, which uses a pseudo-tty and will make C stdio default
to line buffering.
- Use pdpi's solution, which, since it doesn't wait for a prompt
before supplying input, doesn't have this issue.
 
B

bobicanprogram

Hi all,
I have to write an automted script which will test my c
program. That program when run will ask for the commands. For example:

local-host# ./cli
Enter 1 for add
Enter 2 for sub
Enter 3 for mul
1 -------Our option
Enter two numbers
44 33 -------- Our option
Result is 77

As shown in the above example it lists all the options and waits for
user input and once given, it does some operations and waits for some
more. This has to be automated.

Can someone give suggestions how to do this in Python (Linux Platform
in particular).

Thank you,
Venu.


Take a look at the examples here:

http://www.icanprogram.com/06py/main.html

You can probably do what you want using the SIMPL toolkit quite
easily.

bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top