6.19.10.2.18. Python Test

Start python section to interscript/frames/inputf.py[24 /42 ] Next Prev First Last
   760: #line 1020 "input_frame.ipk"
   761:   def test_python(self,
   762:     hlevel=None,
   763:     descr=None,
   764:     source_filename=None,
   765:     source_terminator=None,
   766:     expect_filename=None,
   767:     expect_terminator=None,
   768:     diff_context=0):
   769:     """Test a python script. The script may be an external file, or
   770:     embedded, expected output may be an external file,
   771:     embedded, or omitted, if provided a context diff will be generated
   772:     with the nominated number of lines of context.
   773:     """
   774: 
   775:     testno = self.pass_frame.get_new_test_number()
   776:     testlabel = 'test_'+str(testno)
   777:     test_record = self.pass_frame.tests[testno]=\
   778:       [descr,testlabel,'python','Aborted']
   779:     expect = expect_filename or expect_terminator
   780: 
   781:     # print heading
   782:     if hlevel: our_hlevel = hlevel
   783:     else: our_hlevel = self.last_head+1
   784:     if descr == None: descr = 'Test'
   785:     self.head(our_hlevel,'Test '+str(testno)+': '+descr)
   786:     self.set_anchor(testlabel)
   787: 
   788:     our_weaver = self.get_weaver()
   789: 
   790:     if source_terminator:
   791:       our_weaver.writeline('On-the-fly python test script follows.')
   792:       source_origin_line = self.original_count
   793:       source_origin_file = self.original_filename
   794:       test_code = self.collect_lines_upto(source_terminator)
   795:       our_weaver.script_head('python',source_origin_file)
   796:       for i in range(len(test_code)):
   797:         our_weaver.echotangle(source_origin_line+i+1,test_code[i])
   798:       our_weaver.script_foot('python',source_origin_file)
   799:     elif source_filename:
   800:       our_weaver.writeline('Python test script from file '+source_filename+'.')
   801: 
   802:     if expect_terminator:
   803:       expected_origin_line = self.original_count
   804:       expected_origin_file = self.original_filename
   805:       expected_output = self.collect_lines_upto(expect_terminator)
   806:     elif expect_filename:
   807:       # FIX to make document relative
   808:       our_weaver.writeline('Expected output from file '+expected_filename+'.')
   809:       expected_lines = loadfile(expect_filename)
   810: 
   811:     # execute the test code
   812: 
   813:     if source_filename:
   814:       our_source_filename = source_filename
   815:       description = None
   816:     else:
   817:       our_source_filebase = tempfile.mktemp()
   818:       our_source_filename = our_source_filebase + '_test.py'
   819:       f = open(our_source_filename,'w')
   820:       f.write(string.join(test_code,'\n')+'\n')
   821:       f.close()
   822:       description = 'python <<temporary>>'
   823:     our_weaver.writeline('Actual output follows.')
   824: 
   825:     status, actual_output = self.print_python_output(
   826:       our_source_filename,
   827:       description)
   828:     cmd_ok = status == 0
   829: 
   830:     # delete the file if it was created anonymously
   831:     if not source_filename:
   832:       os.remove(our_source_filename)
   833: 
   834:     if expect:
   835:       try:
   836:         diff_lines = interscript.utilities.diff.diff_lines
   837:         comparison = diff_lines(actual_output, expected_output, context=diff_context)
   838:         equal = len(comparison)==0
   839:         self.pass_frame.tests[testno][2]= 'diff'
   840:         self.pass_frame.tests[testno][3]= ('Fail','Ok')[equal]
   841:         if not equal:
   842:           our_weaver.writeline('On-the-fly expected output follows.')
   843:           our_weaver.expected_head(expected_origin_file)
   844:           for i in range(len(expected_output)):
   845:             our_weaver.echotangle(expected_origin_line+i+1,expected_output[i])
   846:           our_weaver.expected_foot(expected_origin_file)
   847:           self.print_diff_table(comparison)
   848:       except ImportError:
   849:         our_weaver.writeline('Unable to import diff to perform comparison.')
   850:       except KeyboardInterrupt: raise
   851:       except SystemExit: raise
   852:       except:
   853:         traceback.print_exc()
   854:     else:
   855:       self.pass_frame.tests[testno][3]='Inspect'
   856: 
End python section to interscript/frames/inputf.py[24]


6.19.10.2.18.1. Test 17: Python test test
6.19.10.2.18.2. Test 18: Python diff test