[PythonReports-checkins] PythonReports/test/tester run_tests.py, NONE, 1.1
Brought to you by:
a1s
|
From: KACAH <ka...@us...> - 2012-03-09 10:42:54
|
Update of /cvsroot/pythonreports/PythonReports/test/tester
In directory vz-cvs-3.sog:/tmp/cvs-serv30139/test/tester
Added Files:
run_tests.py
Log Message:
Added automatic tester template+data -> printout
--- NEW FILE: run_tests.py ---
"""PythonReports automatic unit tester
Read Tests/tests.cfg and test all tests from all categories
"""
"""
28-feb-2012 [kacah] created
"""
import os
import PythonReports.builder
import PythonReports.template
import yaml
def build_printout(template_file, data):
"""Build printout using PythonReports"""
_template = PythonReports.template.load(template_file)
_builder = PythonReports.builder.Builder(_template)
return _builder.run(data)
def get_differences_list(file1, file2):
"""Get list of differences in files"""
import difflib
_differences = difflib.unified_diff(
open(file1).readlines(), open(file2).readlines())
return list(_differences)
def load_data(test):
"""Load data from data.in file in yaml format"""
_data = []
try:
with open(os.path.join("tests", test["folder"], "data.in")) as _f:
_data = yaml.load(_f)
if not _data:
_data = []
except IOError, _err:
print "Warning, data.in file not found"
except Exception, _err:
print "data.in file has invalid yaml syntax", _err
return _data
def write_result(test, defferences, error=None):
"""Write result of test to diff.txt file"""
_diff_file = os.path.join("tests", test["folder"], "diff.txt")
with open(_diff_file, "w") as _f:
if defferences is None:
print >> _f, error
else:
for _str in defferences:
_f.write(_str)
return _diff_file
def print_result_message(test, differences, diff_file):
"""Print result of test to screen"""
if differences is None:
print test.get("fail_message")
print "Error! Please check", diff_file
elif len(differences):
print test.get("fail_message")
print "There are some differences, please check", diff_file
else:
print "Successful"
def run_test(test):
"""Run single unit test"""
print
print "Starting test %s from %s" % (test["name"], test["folder"])
if test.get("description"):
print " ", test["description"]
_template_file = os.path.join("tests", test["folder"], "template.prt")
_data = load_data(test)
_error = None
_differences = None
try:
_printout = build_printout(_template_file, _data)
_output_file = os.path.join("tests", test["folder"], "last_printout.prp")
_printout.write(_output_file)
_valid_file = os.path.join("tests", test["folder"], "valid_printout.prp")
_differences = get_differences_list(_output_file, _valid_file)
except Exception, _err:
_error = _err
_diff_file = write_result(test, _differences, _error)
print_result_message(test, _differences, _diff_file)
def test_groups(groups):
"""Test all categories in tests.cfg"""
for _group_name, _group in groups.iteritems():
print "----------------------------------"
print "Testing", _group_name
for _test in _group:
run_test(_test)
def main():
with open("Tests/tests.cfg", "r") as _f:
_tests = yaml.load(_f)
test_groups(_tests)
print "----------------------------------"
if __name__ == "__main__":
main()
|