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

« back to all changes in this revision

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

  • Committer: dilshan_a
  • Date: 2008-01-24 22:14:20 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:288
Jsonified test framework output.

Show diffs side-by-side

added added

removed removed

Lines of Context:
81
81
        else:
82
82
            return False
83
83
 
 
84
    def to_dict(self):
 
85
        return {'name': self._name,
 
86
                'detail': self._detail,
 
87
                'critical': self.is_critical()
 
88
                }
 
89
 
84
90
    def __str__(self):
85
91
        return self._name + " - " + str(self._detail)
86
92
 
332
338
        """ Run the solution and the attempt with the inputs specified for this test case.
333
339
        Then pass the outputs to each test part and collate the results.
334
340
        """
 
341
        case_dict = {}
 
342
        case_dict['name'] = self._name
 
343
        
335
344
        # Run solution
336
345
        try:
337
346
            global_space_copy = copy.deepcopy(self._global_space)
357
366
                    raise FunctionNotFoundError(self._function)
358
367
                attempt_data = self._run_function(lambda: global_space_copy[self._function](*self._list_args, **self._keyword_args))
359
368
        except:
360
 
            raise AttemptError(sys.exc_info())
 
369
            case_dict['exception'] = AttemptError(sys.exc_info()).to_dict()
 
370
            return case_dict
361
371
        
362
372
        results = []
363
373
 
364
374
        # generate results
365
375
        for test_part in self._parts:
366
376
            result = test_part.run(solution_data, attempt_data)
367
 
 
368
 
            results.append((test_part.get_description(), result))
369
 
 
370
 
        return results
 
377
            result_dict = {}
 
378
            result_dict['description'] = test_part.get_description()
 
379
            result_dict['passed']  = (result == '')
 
380
            if result_dict['passed'] == False:
 
381
                result_dict['error_message'] = result
 
382
                
 
383
            results.append(result_dict)
 
384
 
 
385
        case_dict['parts'] = results
 
386
 
 
387
        return case_dict
371
388
                
372
389
    def _execfile(self, filename, global_space):
373
390
        """ Execute the file given by 'filename' in global_space, and return the outputs. """
473
490
    def run_tests(self, attempt_file):
474
491
        " Run all test cases and collate the results "
475
492
        
476
 
        results = []
 
493
        problem_dict = {}
 
494
        problem_dict['name'] = self._name
477
495
        
 
496
        test_case_results = []
478
497
        for test in self._tests:
479
 
            test_results = []
480
 
            try:
481
 
                for (name, result) in test.run(self._solution, attempt_file):
482
 
                    if result == '':
483
 
                        disp = "Passed: %s" %name
484
 
                    else:
485
 
                        disp = "Failed: %s, %s" %(name,result)
486
 
                    test_results.append(disp)
487
 
            except AttemptError, e:
488
 
                test_results.append("Error running submitted script: %s" %str(e))
489
 
            results.append((test.get_name(), test_results))
490
 
        return (self._name, results)
 
498
            result_dict = test.run(self._solution, attempt_file)
 
499
            if 'exception' in result_dict and result_dict['exception']['critical']:
 
500
                # critical error occured, running more cases is useless
 
501
                # FunctionNotFound, Syntax, Indentation
 
502
                problem_dict['critical_error'] = result_dict['exception']
 
503
                return problem_dict
 
504
            
 
505
            test_case_results.append(result_dict)
 
506
 
 
507
        problem_dict['cases'] = test_case_results
 
508
        return problem_dict
 
509
 
 
510
    def get_name():
 
511
        return self._name
491
512
 
492
513
class TestFilespace:
493
514
    """
688
709
##      for node in mod.node.nodes:
689
710
##              if isinstance(node, compiler.ast.Function) and node.name == function_name:
690
711
##                      return node
691
 
##    
 
712
##