~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/apps/tutorialservice/test/TestFramework.py

  • Committer: dilshan_a
  • Date: 2008-01-25 03:39:05 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:306
Consolidated SolutionError and AttemptError into ScriptExecutionError.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
import sys, StringIO, copy
26
26
 
27
 
# author error
28
 
class TestCreationError(Exception):
29
 
    """An error occured while creating the test suite or one of its components"""
30
 
    def __init__(self, reason):
31
 
        self._reason = reason
32
 
        
33
 
    def __str__(self):
34
 
        return self._reason
35
 
 
36
 
# author error
37
 
class SolutionError(Exception):
38
 
    """Error in the provided solution"""
39
 
    def __init__(self, exc_info):
40
 
        cla, exc, trbk = exc_info
41
 
        self.name = cla.__name__
42
 
        self._detail = str(exc)
43
 
        self._exc_info = exc_info
44
 
 
45
 
    def to_dict(self):
46
 
        return {'name': self._name,
47
 
                'detail': self._detail,
48
 
                'critical': False
49
 
                }
50
 
 
51
 
    def exc_info(self):
52
 
        return self._exc_info
53
 
 
54
 
    def __str__(self):
55
 
        return "Error running solution: %s" %str(self._detail)
56
 
 
57
 
# author error
58
 
class TestError(Exception):
59
 
    """Runtime error in the testing framework outside of the provided or student code"""
60
 
    def __init__(self, exc_info):
61
 
        cla, exc, trbk = exc_info
62
 
        self.name = cla.__name__
63
 
        self._detail = str(exc)
64
 
        self._exc_info = exc_info
65
 
 
66
 
    def exc_info(self):
67
 
        return self._exc_info
68
 
 
69
 
    def __str__(self):
70
 
        return "Error testing solution against attempt: %s" %str(self._detail)
71
 
 
72
 
# student error
73
 
class AttemptError(Exception):
74
 
    """Runtime error in the student code"""
 
27
# student error or author error
 
28
# errors in student code get handled internally
 
29
# errors in solution code get passed up
 
30
class ScriptExecutionError(Exception):
 
31
    """Runtime error in the student code or solution code"""
75
32
    def __init__(self, exc_info):
76
33
        cla, exc, trbk = exc_info
77
34
        self._name = cla.__name__
94
51
    def __str__(self):
95
52
        return self._name + " - " + str(self._detail)
96
53
 
97
 
# student error
 
54
# author error
 
55
class TestCreationError(Exception):
 
56
    """An error occured while creating the test suite or one of its components"""
 
57
    def __init__(self, reason):
 
58
        self._reason = reason
 
59
        
 
60
    def __str__(self):
 
61
        return self._reason
 
62
 
 
63
# author error
 
64
class TestError(Exception):
 
65
    """Runtime error in the testing framework outside of the provided or student code"""
 
66
    def __init__(self, exc_info):
 
67
        cla, exc, trbk = exc_info
 
68
        self._name = cla.__name__
 
69
        self._detail = str(exc)
 
70
        self._exc_info = exc_info
 
71
 
 
72
    def exc_info(self):
 
73
        return self._exc_info
 
74
 
 
75
    def __str__(self):
 
76
        return "Error testing solution against attempt: %s - %s" %(self._name, self._detail)
 
77
 
 
78
# author error
 
79
# raised when expected file not found in solution output
 
80
# Always gets caught and passed up as a TestError
 
81
class FileNotFoundError(Exception):
 
82
    def __init__(self, filename):
 
83
        self._filename = filename
 
84
 
 
85
    def __str__(self):
 
86
        return "File %s not found in output" %(self._filename)
 
87
    
 
88
 
 
89
# Error encountered when executing solution or attempt code
 
90
# Always gets caught and passed up in a ScriptExecutionError
98
91
class FunctionNotFoundError(Exception):
99
92
    """This error is returned when a function was expected in a
100
93
    test case but was not found"""
262
255
        # check files indicated by test
263
256
        for (filename, (test_type, f)) in self._file_tests.items():
264
257
            if filename not in solution_files:
265
 
                raise SolutionError('File %s not found' %filename)
 
258
                raise FileNotFoundError(filename)
266
259
            elif filename not in attempt_files:
267
260
                return filename + ' not found'
268
261
            elif not self._check_output(solution_files[filename], attempt_files[filename], test_type, f):
383
376
                solution_data = self._run_function(lambda: global_space_copy[self._function](*self._list_args, **self._keyword_args))
384
377
                
385
378
        except:
386
 
            raise SolutionError(sys.exc_info())
 
379
            raise ScriptExecutionError(sys.exc_info())
387
380
 
388
381
        # Run student attempt
389
382
        try:
396
389
                    raise FunctionNotFoundError(self._function)
397
390
                attempt_data = self._run_function(lambda: global_space_copy[self._function](*self._list_args, **self._keyword_args))
398
391
        except:
399
 
            case_dict['exception'] = AttemptError(sys.exc_info()).to_dict()
 
392
            case_dict['exception'] = ScriptExecutionError(sys.exc_info()).to_dict()
400
393
            case_dict['passed'] = False
401
394
            return case_dict
402
395
        
405
398
        
406
399
        # generate results
407
400
        for test_part in self._parts:
408
 
            result = test_part.run(solution_data, attempt_data)
 
401
            try:
 
402
                result = test_part.run(solution_data, attempt_data)
 
403
            except:
 
404
                raise TestError(sys.exc_info())
 
405
            
409
406
            result_dict = {}
410
407
            result_dict['description'] = test_part.get_description()
411
408
            result_dict['passed']  = (result == '')