Starten Unit Test Module in XQuery

Wednesday, 19 August 2020 · 3 messages · last reply 20 Aug 2020
MS
Markus Schmid19 Aug 2020 07:32 #
Hi Ist es möglich, ein Unit Test Module (https://docs.basex.org/wiki/Unit_Module#Query) über ein XQuery Script zu starten und die Testausgabe in ein File umzuleiten? (Ich kenne nur die Möglichkeiten, das Module über das TEST Kommando und die Command line unter Angabe der -t Option zu starten). Danke für die Antwort. Markus Schmid
CG
Christian Grün20 Aug 2020 09:10 #
Hi Markus,
Quoted text · Markus Schmid · 1 line
> Ist es möglich, ein Unit Test Module (https://docs.basex.org/wiki/Unit_Module#Query) über ein XQuery Script zu starten und die Testausgabe in ein File umzuleiten?
As you’ve already noted, unit tests must always be started outside an XQuery expression. The reason is that sequentially executed unit tests may have come with all kinds of side effects which go beyond the scope of a single query. The following solution doesn’t necessarily distinguish itself through elegance, but you could launch a second BaseX instance with the Process Module [1] and convert the string output to an XML document via fn:parse-xml:
let $args := ('-cp', '/path/to/basex.jar', 'org.basex.BaseX', '-t',
'/path/to/tests')
let $result := proc:system('java', $args)
return parse-xml($result)
This way, your tests will be run in an independent BaseX context. If you invoke the function from the directory in which your BaseX instance and the tests reside, you can omit the absolute paths (/path/to/). Hoppe this helps, Christian [1] https://docs.basex.org/wiki/Process_Module#proc:system
MS
Markus Schmid20 Aug 2020 12:20 #
Hi Christian thank you for the response. The solution is good (far better than code generating .bat files ..). I slightly modified the solution in order to run the same testsuites with different testdata in one script:
declare function test:runtests() {
  for-each(
    map:keys($test:dbset),
    function($k) {
      test:runtest($test:dbset($k))
    }
  )
};

declare function test:runtest($databases) {
  let $testdata := $databases/@data
  let $name := $databases/@name
  let $dummy := trace(concat('runtest for testdata : ‘,$name))
  let $args := ('-cp','C:\Arbeit\Tools\BaseX932\basex\BaseX.jar','org.basex.BaseX','-b',concat('{mytests}data=',$testdata),'-o',concat('C:\Arbeit\Tasks\UnitTests\TestResult',$name,'.xml'),'-t','C:\Arbeit\Tasks\UnitTests\TestModules\')

  return (
    try {proc:system('java',$args)} catch * { }
  )
};

let $result := test:runtests()
return() Using the try / catch block is important here else the script stops when the first test failure occurs. Markus
Quoted text · Christian Grün · 30 lines
> Am 20.08.2020 um 09:10 schrieb Christian Grün <christian.gruen at gmail.com>: > > Hi Markus, > >> Ist es möglich, ein Unit Test Module (https://docs.basex.org/wiki/Unit_Module#Query) über ein XQuery Script zu starten und die Testausgabe in ein File umzuleiten? > > As you’ve already noted, unit tests must always be started outside an > XQuery expression. The reason is that sequentially executed unit tests > may have come with all kinds of side effects which go beyond the scope > of a single query. > > The following solution doesn’t necessarily distinguish itself through > elegance, but you could launch a second BaseX instance with the > Process Module [1] and convert the string output to an XML document > via fn:parse-xml: > > let $args := ('-cp', '/path/to/basex.jar', 'org.basex.BaseX', '-t', > '/path/to/tests') > let $result := proc:system('java', $args) > return parse-xml($result) > > This way, your tests will be run in an independent BaseX context. If > you invoke the function from the directory in which your BaseX > instance and the tests reside, you can omit the absolute paths > (/path/to/). > > Hoppe this helps, > Christian > > [1] https://docs.basex.org/wiki/Process_Module#proc:system

⚡Generated with XQuery