Neste artigo
The Banana.Test class is used to run unit tests of Banana Extensions.
See also working with the Banana Extensions Test Framework.
Banana Extensiong TestFramework
The Banana Extensions Test Framework is like an usual Unit Test Framework.
Two methodologies are available:
- Verify results
Through assert methods the user can verify some conditions, in case that a condition didn't meet the test fail and it is interrupted.
For example you verify that a method returns a determined value.
Ex.: Test.assertIsEqual(totalVatAmount(), "5000.00");
- Log results and compare them with previous results (expected results)
Through the Banana.Test.Logger methods, you can log test results. Test results are stored under the test/testresults folder. They will be compared at the end of the test with the expected results stored under the folder test/testexpected (results form previous tests), if any difference is found, the test is marked as failed and the differences showed to the user.
In this case you don't care about the exact value returned by a method, but you verify that the method returns the same value across different versions of the BananaApp or Banana Accounting.
Ex.: Test.logger.addKeyValue("Total VAT amount", totalVatAmount());
Create a test case
To create a test case look at the example SampleExtensions/TestFramework.
Run a test case
You can run a test case in two ways (both available starting at Banana Accounting 9.0.4):
- Through the Manage Apps dialog
- Open the Manage Apps dialog
- Select the Banana Extension to test
- Click over 'Show details'
- Click on the button 'Run test case'
- Through the Command line
- banana90.exe -cmd=runtestsapps -p1=path_to_testcase.js|folder
- As parameter p1 you can specify the path to a test case file, or the path of a folder
- In case of a folder all files in the folder and subfolders ending with test.js are run
Test case folder structure
This is the default test structure of a test case. All the files used for the test case are stored in a folder named test.
In the dialog Manage apps the button 'Run test case' button is showed only if the application find a file named test/<same_name_bananaextemsopm>.test.js.
ch.banana.script.bananaapp.js # BananaApps
ch.banana.script.bananaapp2.js
...
test/
ch.banana.script.bananaapp.test.js # BananaApps Test Cases
ch.banana.script.bananaapp2.test.js
...
testcases/
*.ac2 # ac2 files for the test cases
...
testexpected/ # Expected test results used for verifying the current results
ch.banana.script.bananaapp.test/
*.txt
ch.banana.script.bananaapp2.test/
*.txt
...
testresults/ # Current test results
ch.banana.script.bananaapp.test/
*.txt
ch.banana.script.bananaapp2.test/
*.txt
...
Logger output format
The results are saved in .txt with the Latex format. Yes, it means that you can convert the output files in pdf, and look at the results without the log structure commands.
Short example
For a complete example look a SampleExtensions/TestFramework.
// @id = ch.banana.script.bananaapp.test
// @api = 1.0
// @pubdate = 2018-03-30
// @publisher = Banana.ch SA
// @description = Simple test case
// @task = app.command
// @doctype = *.*
// @docproperties =
// @outputformat = none
// @inputdataform = none
// @timeout = -1
// Register test case to be executed
Test.registerTestCase(new TestLoggerSimpleExample());
// Here we define the class, the name of the class is not important
function TestLoggerSimpleExample() {
}
// Test method, every method starting with 'test' will be automatically executed
TestLoggerSimpleExample.prototype.testOk = function() {
Test.logger.addText("This test will pass :-)");
Test.assert(true);
}
The Test object
When a script is run as a test case, a global object named Test is exposed to the script. This object defines properties and methods for executing the test case.
Test.logger.addKeyValue("count", 4);
Test.assert(true);
Properties
logger
The property logger returns an object of type Banana.Test.Logger that permits to log test results. If the script is not run though the Banana Apps functionality this object is null.
var testLogger = Test.logger;
testLogger.addKeyValue("count", 4);
Methods
assert(condition, message)
This method verifies if the condition is true. If the condition is true the test continues, else an exception is thrown and the message message is inserted in the test results.
Test.assert(true, "This test will pass");
assertEndsWidth(string, endString)
This method verifies if text string ends with the text endString. If the condition is true the test continues, else an exception is thrown and a fatal error is inserted in the test results.
Test.assertEndsWith("This string ends with the text", "the text");
assertIsEqual(actual, expected)
This method verifies if actual equal to expected. If the condition is true the test continues, else an exception is thrown and a fatal error is inserted in the test results.
Test.assertIsEqual("Those strings are equal", "Those strings are equal");
assertGreaterThan(actual, expected)
This method verifies if actual is greather than expected. If the condition is true the test continues, else an exception is thrown and a fatal error is inserted in the test results.
Test.assertGreaterThan(10, 8);
assertGreaterThanOrEqual(actual, expected)
This method verifies if actual is greather than or equal to expected. If the condition is true the test continues, else an exception is thrown and a fatal error is inserted in the test results.
Test.assertGreaterThanOrEqual(8, 8);
assertLessThan(actual, expected)
This method verifies if actual less than expected. If the condition is true the test continues, else an exception is thrown and a fatal error is inserted in the test results.
Test.assertLessThan(8, 10);
assertLessThanOrEqual(actual, expected)
This method verifies if actual less than or equal to expected. If the condition is true the test continues, else an exception is thrown and a fatal error is inserted in the test results.
Test.assertLessThanOrEqual(10, 10);
assertMatchRegExp(string, pattern)
This method verifies if string math the regula expression defined by pattern. If the condition is true the test continues, else an exception is thrown and a fatal error is inserted in the test results.
Test.assertMatchRegExp("This string match the regual expression", /match/);
assertStartsWith(string, startString)
This method verifies if text string starts with the text startString. If the condition is true the test continues, else an exception is thrown and a fatal error is inserted in the test results.
Test.assertStartsWith("This string start with the text", "This string");
closeDocument(document)
Close a document previously opened for testing. Only documents opened via Banana.application.openDocument can be closed. Document opened during tests have to be closed before the test ends.
let testDoc = Banana.application.openDocument("testfile.ac2");
// Do some tests on testDoc
Test.closeDocument(testDoc);
registerTestCase(testCase)
This method register a testCase (an object) to be run as test.
// Register test case to be executed
Test.registerTestCase(new TestLoggerSimpleExample());
setDocument(document)
Set the current document returned by Banana.document.
let testDoc = Banana.application.openDocument("testfile.ac2");
// Do some tests on testDoc
Test.setDocument(testDoc);
Test.closeDocument(testDoc);
setTableValue(tableName, rowNr, columnName, value)
Set the value in table tableName, row rowNr and column columnName. The value is a string, and follow the internal format for numbers, dates, times and boolean.
let testDoc = Banana.application.openDocument("testfile.ac2");
Test.setDocument(testDoc);
Test.setTableValue("Invoices", 3, "Description", "Test Invoice 3");
Test.setTableValue("Invoices", 3, "Date", "2023-09-26");
Test.setTableValue("Items", 12, "UnitPrice", "1200.50");
Test.closeDocument(testDoc);