Skip to main content

Utility Functions

In the Baz environment, utility functions are present to aid in writing tests and control how tests are run. These functions are broadly grouped under the following namespaces:

global

The global namespace exports functions that aid in broad actions like getting user preferences, easy behavioral assertions, etc.

verify

The verify function is a wrapper on the expect interface from the Chai assertion library. The method adds improved error handling over the expect, interface. In addition, it supports all language chains supported by the Chai interface.

Example

function testVerify() {
let input = 10;
verify(input).to.be.eql(10);
}

verifyEach

The verifyEach function is similar to the verify function but adds the capability to iterate a list of objects for verification. Similar to verify function, it also supports all language chains supported by the Chai interface.

Example

function testVerifyEach() {
let input = [10, 15, 20, 20, 25];
verifyEach(input).to.be.eql(20);
}

In the above example, each input element would be compared with the value 20, and the result of each comparison would be recorded in the report.

Language Chain - do & result

The verifyEach function adds the language chain of do & result. The do & result combination can be used to define custom comparison operators for assertions. The do method takes a callback function as input. The return value expected from the callback is a dictionary with the below keys.

NameTypeDescription
continueBooleanThe parameter indicates if the test should continue.
paramObjectThe value to be used for the comparison in the assertion.
note

The chain can only be used with verifyEach & must be combined.

Example

function testVerifyEachCheck() {
let input = ["z", 66, 42, "c", "d"];
verifyEach(input).do(printVal).result.to.equal(42);
}

function printVal(x) {
let bcontinue = false;
if (x % 2 == 0) {
bcontinue = true;
}
let val = { continue: bcontinue, param: x };
logger.Debug()?.Msg("returning " + JSON.stringify(val));
return val;
}

actor

The actor namespace exports functions that can be used to capture end-user(administrator) intentions.

getConfParam(name)

The function gets configuration parameters configured for the actor in the actor.toml file. The function returns an Object if the configuration is defined or undefined if it's absent.

NameTypeDescription
nameStringThe name of the configuration parameter to fetch from the config section of actor config.

Example

[win.confparams]
filter_list = ['f1', 'f2']
let filterList = actor.getConfParam("filter_list");

The above call to getConfParam would return the list ['f1', 'f2'].

baz

The Baz namespace exports functions that can control the Baz framework from within the test code. The list of functions and their usage is as follows:

setParam(name, value)

This function sets a parameter with the name as name to any ES6 value denoted by value. When setParam is used, and an assertion with the parameters of the same name is to be run, the value will be passed to the assertion.

NameTypeDescription
nameStringThe name of the variable to be set in the namespace.
valueObjectThe value to be set for the variable name.
function setup() {
baz.setParam(life, 42);
}

/*
* testLife takes life as a parameter
* Since baz.setParam() was called in setup()
* This invocation will now get the value as a parameter
*/
function testLife(life) {
verify(life).to.be.eql(42);
}

getParam(name)

This function returns the value associated with the parameter name explicitly.

NameTypeDescription
nameStringThe name of the variable to get from the namespace.

setGlobalParam(name, value)

setGlobalParam() is similar to setParam, with the parameter's scope expanding beyond a single directory when multiple directories are passed in as input to baz.

NameTypeDescription
nameStringThe name of the variable to be set in the namespace.
valueObjectThe value to be set for the variable name.
note

setGlobalParam does not support the setting of ES6 Objects as parameters. Doing so will result in only the data being received by the assertion or in a getParam call.


setReport(field, value)

This function adds a custom field and value to the current run's report.

NameTypeDescription
fieldStringThe name of the field in the report.
valueStringThe value to be set for the field in the report.

Report Static Fields

You can also add static data to reports for test cases. The Baz system parser parses a comment block preceding the test case for descriptors, which are added to the report for that specific test case.

Example

/**
* @description In the end it doesn't even matter
* @author Mike Shinoda
*/
function testLifeLater(life) {
verify(life).to.be.eql(43);
}

In the above example, description and author fields would be added to the report for the test case testLifeLater record.


runTests(arrayOfTests)

This function runs a list of tests immediately as specified in the arrayOfTests parameter. This gives control to run the same set of tests, say for different parameters, via the setup() function. When a test has been run via this or other runTest* functions, it will not be run in the normal sequence of setup()-> assertions -> tear_down().

NameTypeDescription
arrayOfTestsArrayThe array of test cases to be run.

runTestsExcept(arrayOfTests)

This function is the inverse of the previous function. All tests except those specified by arrayofTests are run.

NameTypeDescription
arrayOfTestsArrayThe array of test cases to be excluded from run.

runAllTests()

This function runs all tests specified present in the input.

getTestPath()

This helper function can get the directory path where test files are present in the current run.

bcsv

This module provides helpers for working with CSV (Comma Separated Value) files.

createFile(name)

This function creates a file with name name. An ES6 object is returned from the call. Method append(item)can be used to add rows to the CSV file. Method close() should be called on the object to flush and close the file handle.

NameTypeDescription
nameStringThe name of the file to be created.

openFile(name)

This function is similar to createFile but opens an existing file instead of creating a new one. The object returned is similar. This object implements the ES6 iterator. It can be iterated, and records read. A readAll() method is also available to read all records simultaneously.

NameTypeDescription
nameStringThe name of the file to be opened.

butils

This module has more helper functions for files, JSON, and TOML data.

openFile(name)

This function can be used to open a file. It returns a file object. The file object implements the ES6 iterator. It also has a readLine() function that can read one line at a time via a loop construct. readAll() is another function to read all data in one go. close() is to flush and close the underlying resources.

NameTypeDescription
nameStringThe name of the file to be opened.

createFile(name)

This is similar to the openFile function. However, this method creates a new file instead of opening a file. Method append(item)can add data to the file. Method close() should be called on the object to flush and close the file handle. The read functions are not present in the file object returned.

NameTypeDescription
nameStringThe name of the file to be created.

readJSON(name)

This function reads the file's contents specified by name, parses the JSON contents, and returns an ES6 object.

NameTypeDescription
nameStringThe name of the JSON file to be read.

writeJSON(name, object)

This function writes the object into the file specified by name as JSON.

NameTypeDescription
nameStringThe name of the file where the JSON object is written.
objectObjectThe object to be written to the file name.

mergeJSON(target, definition)

This function exposes a JSON Patch operation. The definition is applied to the target as a Merge Patch operation, and the resultant JSON is returned.

NameTypeDescription
targetObjectJSON target object.
definitionObjectJSON definition object.

readTOML(name)

This function reads the file's contents specified by name, interprets the content as TOML data, and returns an ES6 object.

NameTypeDescription
nameStringThe name of the TOML file to be read.

removeAll(directory)

This function deletes the directory. This also works on files.

NameTypeDescription
directoryStringThe name of the directory or file to be deleted.