import os, sys
try:
    import sybprocess
except ImportError:
    # user private copy for old Python versions
    import MonetDBtesting.subprocess26 as subprocess

def client(cmd, input = None):
    clt = subprocess.Popen(cmd,
                           shell = True,
                           stdin = subprocess.PIPE,
                           stdout = subprocess.PIPE,
                           stderr = subprocess.PIPE,
                           universal_newlines = True)
    out, err = clt.communicate(input)
    sys.stdout.write(out)
    sys.stderr.write(err)

def main():
    xq_client = os.getenv('XQUERY_CLIENT')
    client('%s --input=my-document --collection=my-collection' % xq_client,
           '<document>test document</document>')
    client('%s -s "pf:documents()"' % xq_client)
    client('%s -s "pf:del-doc(\'my-document\')"' % xq_client)

main()

@@ -224,49 +224,8 @@
 md,%0A
-                           shell = True,%0A
     
@@ -577,16 +577,24 @@
 CLIENT')
+.split()
 %0A    cli
@@ -597,20 +597,30 @@
  client(
-'%25s 
+xq_client + %5B'
 --input=
@@ -630,17 +630,20 @@
 document
- 
+', '
 --collec
@@ -661,28 +661,17 @@
 lection'
- %25 xq_client
+%5D
 ,%0A      
@@ -728,65 +728,133 @@
 ent(
-'%25s -s %22pf:documents()%22' %25 xq_client)%0A    client('%25s -s %22
+xq_client + %5B'-s', 'for $doc in pf:documents() where $doc/@url = %22my-document%22 return $doc'%5D)%0A    client(xq_client + %5B'-s', '
 pf:d
@@ -860,18 +860,17 @@
 del-doc(
-%5C'
+%22
 my-docum
@@ -876,25 +876,12 @@
 ment
-%5C')%22' %25 xq_client
+%22)'%5D
 )%0A%0Am

Make test independent of whatever else is in the database. Also, use a different way of calling subprocess.Popen so that we can use quotes and dollars without having to do difficult cross-architectural escaping.