TestRunner examples


#1

Run all tests in a given file

function RunJasmineOne(filename, grid=true) {
  console.log('Running Jasmine on ' + filename)
  var files = MetadataStore.tag().files({
                category: MetadataFileCategory.JASMINE
              });
  var file = _.find(files,
                    function(f) { return f.url.indexOf(filename) > 0})
  var ts = JasmineTestRunner.testPath(file)
  if (grid) {
    c3Grid(ts)
  }
  console.log('Done')
  return ts
}

Run all tests in a collection of files, path could be ‘consumer/admin/

function RunJasmineMany(path, grid=true) {
  console.log("Running Jasmine on all in " + path)
  var files = MetadataStore.tag().files({
                category: MetadataFileCategory.JASMINE,
                path: path
              })
  var res = _.map(files,
                  function(file) {
                    console.log('Running Jasmine on ' + file)
                    var ts = JasmineTestRunner.testPath(file)
                    console.log("tests="  + ts.tests +
                                ", failures=" + ts.failures)
                    if (ts.failures > 0) {
                      console.log(Object.toJSON(ts.allFailures()))
                    }
                    return ts
                  })
  if (grid) {
    c3Grid(res)
  }
  return res
}

Run all tests inside a package

function RunJasminePackage(pkgname, grid=true) {
  var config = TestRunnerConfig.make({
                 pkgName: pkgname,
                 noDependency: true
               })
  var runner = JasmineTestRunner.testPackage(config)
  console.log("TestRunner (" + runner.id + ") starts testing " +
              config.pkgName)
  var counter = 0
  var result;
  var interval = setInterval(
                   function() {
                     counter++
                     console.log("TestRunner has run for " +
                                 (counter  / 2) + " minutes.")
                     runner = runner.get()
                     var status = runner.status().status
                     if (status == "failing" || status == "canceled") {
                       console.log("TestRunner status is " + status +
                                   ", exiting")
                       console.log(JSON.stringify(runner.status().errors))
                       clearInterval(interval)
                     } else if (status == "completed") {
                       clearInterval(interval)
                       console.log("TestRunner has finished. " +
                                   "Generating test reports...")
                       result = runner.getTestResult()
                       if (grid) {
                         c3Grid(result)
                       }
                     }
                     if (counter == 1800) {
                       clearInterval(interval)
                       console.log("TestRunner timed out after " +
                                   (counter / 2) + " minutes.")
                     }
                   },
                   30000)
  return result
}

Run tests for canonicals

function RunCanonicalPackage(package, testDataDir="test/canonical/data",
                             grid=true) {
  console.log("Running "+package+" Canonical tests")
  var ctx = TestApi.createContext(package+"CanonicalTest")
  var testPackage = MetadataStore.current().package(package, true)
  var testPath = MetadataPath.make({
                   repository: testPackage.repository,
                   package: testPackage.package,
                   category: MetadataFileCategory.PACKAGE,
                   encodedSubPath: testDataDir})
  var resStatus = CanonicalTestRunner.generateExpectedResult(
                    testPath,
                    "/tmp/"+ctx.uuid())
  var res = CanonicalTestRunner.testPath(testPath)
  if (grid) {
    c3Grid(res.testcase)
  }
  console.log("Done")
  return res
}

#2

Note that CanonicalTestRunner (at least in v7.6.1) verifies JSON files until the first difference. Once you have fixed a problem leading to that difference, CanonicalTestRunner will verify further in the next run.