44'''
55import os
66import lxml
7- import nose
87from docx import *
98
10- TEST_FILE = 'Short python-docx test.docx'
9+ TEST_FILE = 'ShortTest.docx'
10+ IMAGE1_FILE = 'image1.png'
1111
1212# --- Setup & Support Functions ---
1313def setup_module ():
1414 '''Set up test fixtures'''
15+ import shutil
16+ if IMAGE1_FILE not in os .listdir ('.' ):
17+ shutil .copyfile (os .path .join (os .path .pardir ,IMAGE1_FILE ), IMAGE1_FILE )
1518 testnewdocument ()
1619
1720def teardown_module ():
@@ -33,7 +36,7 @@ def simpledoc():
3336 docbody .append (paragraph ('Paragraph 2' ))
3437 docbody .append (table ([['A1' ,'A2' ,'A3' ],['B1' ,'B2' ,'B3' ],['C1' ,'C2' ,'C3' ]]))
3538 docbody .append (pagebreak (type = 'section' , orient = 'portrait' ))
36- relationships ,picpara = picture (relationships ,'image1.png' ,'This is a test description' )
39+ relationships ,picpara = picture (relationships ,IMAGE1_FILE ,'This is a test description' )
3740 docbody .append (picpara )
3841 docbody .append (pagebreak (type = 'section' , orient = 'landscape' ))
3942 docbody .append (paragraph ('Paragraph 3' ))
@@ -45,10 +48,14 @@ def testsearchandreplace():
4548 '''Ensure search and replace functions work'''
4649 document , docbody , relationships = simpledoc ()
4750 docbody = document .xpath ('/w:document/w:body' , namespaces = nsprefixes )[0 ]
51+ assert search (docbody , 'ing 1' )
52+ assert search (docbody , 'ing 2' )
53+ assert search (docbody , 'graph 3' )
54+ assert search (docbody , 'ist Item' )
55+ assert search (docbody , 'A1' )
4856 if search (docbody , 'Paragraph 2' ):
4957 docbody = replace (docbody ,'Paragraph 2' ,'Whacko 55' )
5058 assert search (docbody , 'Whacko 55' )
51- assert False # replace works, search fails for some cases
5259
5360def testtextextraction ():
5461 '''Ensure text can be pulled out of a document'''
@@ -99,4 +106,5 @@ def testtable():
99106 assert testtable .xpath ('/ns0:tbl/ns0:tr[2]/ns0:tc[2]/ns0:p/ns0:r/ns0:t' ,namespaces = {'ns0' :'http://schemas.openxmlformats.org/wordprocessingml/2006/main' })[0 ].text == 'B2'
100107
101108if __name__ == '__main__' :
109+ import nose
102110 nose .main ()
0 commit comments