1

Say I wanna test my c++ code but I don't want to do it by hand. I realize I can write a python script that can test my code for me. So I want to test this c++ code for example:

#include <iostream>
#include <string>
using namespace std;
int main() {
 string line;
 cin >> line;
 cout << line << endl;
}

Here is the python script that I tried to test this c++ code:

import subprocess as sb
sb.call("g++ main.cpp", shell=True)
sb.call("./a.out", shell=True)
sb.call("chocolate", shell=True)

This create the a.out executable file but it doesn't allow me to run my program. How can I make this work? Or is there something better that I can do?

Mats Petersson
130k15 gold badges147 silver badges233 bronze badges
asked Mar 4, 2016 at 23:18
6
  • Removing "C++" tag, as your question is not about how to do something in C++, but how to do something in Python - even if you put some C++ code in the question, that is not the code you are asking about [directly]. Commented Mar 4, 2016 at 23:23
  • You could use subprocess.pipe and Popen.communicate, perhaps? Commented Mar 4, 2016 at 23:24
  • 1
    Possible duplicate of Python executing shell command doesn't terminate Commented Mar 4, 2016 at 23:25
  • Why do you want to test your C++ code with python script? Is there any special reason for that? I think it is more natural and easier to use C++ unit testing libraries like boots.test. Commented Mar 4, 2016 at 23:31
  • 1
    @BorisSerebrov We have an entire test team that writes tests for c++ programs in python. These tests drive the C++ at the command line level and by using a foreign function interface when appropriate. This is not C++ unit testing of course (we do that too) but it is a major component of testing. Putting "boost" and "easy" in the same sentence may also be violating natural law. Commented Mar 4, 2016 at 23:35

1 Answer 1

1

Testing can get complicated but as a minimum you can use the subprocess.Popen object to manage the input and output of the program. Here is a minimalist test suite

import subprocess as sb
import threading
def timeout():
 print('timeout')
 exit(3)
sb.check_call("g++ main.cpp", shell=True)
t = threading.Timer(10, timeout)
proc = sb.Popen("./a.out", shell=True, stdin=sb.PIPE, stdout=sb.PIPE,
 stderr=sb.PIPE)
out, err = proc.communicate('hello\n')
t.cancel()
assert proc.returncode == 0
assert out == 'hello\n'
assert err == ''
sb.check_call("chocolate", shell=True)
answered Mar 4, 2016 at 23:45
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.