All Collections
Tests / Autograding
Writing tests
[Advanced] Reporting test results from a helper file
[Advanced] Reporting test results from a helper file

Want to report test results from a Python file, or some other helper file?

James avatar
Written by James
Updated over a week ago

In File Mode, it's possible to report the results of tests using the codePost TestOutput  command from a test .sh  file. But what if you want to report test results directly from a non-bash helper file, like a Python script?

Here's how to do that.

Invoke TestOutput command from a helper file

Many languages allow scripts to invoke commands. In Python, for example, you can use subprocess.popen to execute a bash command directly from Python. This means that you can report test results using codePost's TestOutput  syntax, just like you would from a .sh  file. 

from subprocess import Popen

def report_result(category, case, res, logs=''):
  reporter = ("TestOutput \"%s\" \"%s\" %s \"%s\"") % (category, case, "true" if res else "false", logs)
  Popen(['/bin/bash', '-c', reporter])

...your tests...

if passed:
    report_result("Correctness", "Test 1", True, logs="nice job!")
else:
    report_result("Correctness", "Test 1", False, logs="failed")

You might be wondering how it's possible to report results for Correctness/Test 1  if the test case hasn't been created yet. Anytime you run tests, codePost will check to see whether you reported the results of a test case that doesn't exist yet, and, if so, it will create the test case for you. So if you want to edit properties of the test (e.g. to automatically add points on pass) you'll need to run your tests first to create the corresponding test cases. 

Warning: if you plan to invoke TestOutput from a helper file in this way, you must turn off this environment setting. 

Did this answer your question?