Search code examples
crobotframeworkfunctional-testing

Functional testing of C aplication. How do I pass standard input to process in Robot Framework using data-driven TSV?


I would to take test an console application (written in C), that simply takes an input from keyboard and writes to standard output.

For simplicity, application is taken from K&R book and mimics standard wc utility:

#include <stdio.h>

#define IN  1
#define OUT 0

int main(void)
{
    int c, nl, nw, nc, state;

    state = OUT;
    nl = nw = nc = 0;
    while ((c = getchar()) != EOF) {
        ++nc;
        if (c == '\n')
            ++nl;
        if (c == ' ' || c == '\n' || c == '\t')
            state = OUT;
        else if (state == OUT) {
            state = IN;
            ++nw;
        }
    }
    printf("%d %d %d\n", nl, nw, nc);
}

I cannot find any facility to pass data into stdin with the Run Process keyword (using the Process library). However, I managed to use echo and pipe like this:

*** Settings ***
Library Process
Test Template   Test Output

*** Variables ***
${PROGRAM}  ./a.out

*** Test Cases ***              INPUT       OUTPUT
Empty input                     ''          0 0 0
One-letter word                 'a\n'       1 1 2
Two-letter word                 'ab\n'      1 1 3
Two one-letter words            'a b\n'     1 2 4
Two lines with one-letter word  'a\nb\n'    2 2 4

*** Keywords ***
Test Output
    [Arguments] ${input}    ${output}
    Run Process echo -n ${input} | ${PROGRAM}   shell=true  alias=myproc
    ${stdout}=  Get Process Result  myproc  stdout=true
    Log ${stdout}
    Should Be Equal ${stdout}   ${output}

Neverthless, this method is not portable. Is there some more generic facility to lauch Run Process with already prepared input or am I doing it in wrong way?


Solution

  • Interacting with a process' stdin is not possible with the robotframework's Process library or with any built-in library that I know of. Have a look at https://github.com/robotframework/robotframework/issues/2166