Skip to content

Adding C Assignment

Syed Owais Ali Chishti edited this page Oct 29, 2017 · 2 revisions

Adding C++ assignment are simple the only changes goes within the student test and instructor test file.

For complete detail about adding assignment, review Adding Python Assignment.

Sample Test File for C++ Assignment

import subprocess

fn = 'a99' # Name of assignment file

def teardown_module(module):
    cleanup()  # cleanup at end to remove object code file
    pass

def compile_program():
    """ Compile the code """
    args = ['g++', fn+'.cpp', '-o', fn+'.out', '-w'] # suppress warning (for windows)
    p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = p.communicate()

    print err
    # print '[', out, ']'
    return (err == '' or err == None) # no error means we compiled fine

def cleanup():
    """ Cleanup the directory """
    import os
    frem = fn + '.out'
    if os.path.exists(frem):
        os.remove(frem)

def test_compile(): 
    """ Check whether code compiled or not"""
    res = compile_program()
    assert res

##### TESTS START HERE

def test_hello():
    args = ['./' + fn + '.out'] # Execute the compiled code
    p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # Input to program are sent via args
    out, err = p.communicate()

    ground = ['Hello world!', 'Hello world from cout ...\n']

    assert out == '\n'.join(ground)

Note: Test cases can be changed accordingly.