TestGAS helps you write better Google Apps Script
The TestGAS library makes it easy to write small tests on Google Apps Script, yet scales to support complex functional testing for applications and libraries.
Usage
Import
You can import this library by this script ID.
1CRjWWWYfjD7WzPl43RB1BiD7XDLJmR03eEpXr2LMh75yAq5qMlczOIfm
If you cannot use...
Maybe, your files are older than this library.
So, you should remake .gs script files to use this library.
Getting started
An example of a simple test:
// content of test_sample.gs
let tester = TestGAS.createExecutor();
function sample(x){
return x + 1;
}
class Test_sample{
test_sample_1(){
tester.assertEquals(sample(2), 4);
}
test_sample_2(){
tester.assertNotEquals(sample(2), 4);
}
}
function execute_Test_sample(){
let failureFuncs = tester.executeTestGas(Test_sample);
}Result of executing execute_Test_sample:
[ 'test_sample_1', 'test_sample_2' ]
TestGAS starts: "Test_sample"
...... TestGAS terminated: "Test_sample".
====================================================== FAILURES ======================================================
______________________________________________ test_sample_1 ______________________________________________
AssertionError: Actual value is not equal to Expected value.
actual : 3
expected: 4
=================== 1 failed, 1 passed of all 2 tests in 0.01 seconds ===================
Features
Methods
Reference
createExecutor ()
Creates Executor object.
Executor runs test and display result of test.
Arguments
| name | type | description |
|---|---|---|
| (nothing) | (nothing) | (nothing) |
Example
let tester = createExecutor();
class Test_TestGas{
test_plus_1_1(){
const actual = 1 + 2;
const expected = 3;
tester.assertEquals(actual, expected);
}
test_plus_1_2(){
const actual = 2 + 3;
const expected = 5;
tester.assertEquals(actual, expected);
}
}
function execute_Test_utilForTestGas(){
let failureFuncs = tester.executeTestGas(Test_TestGas);
}Executor.executeTestGas (executingTestClass, arraySkippingTest=[])
Runs test and display result of test.
Arguments
| name | type | description |
|---|---|---|
| executingTestClass | Object | Executor of TestGAS. |
| arraySkippingTest | string[] | array of functions that test is being skipped. |
Example
class Test_TestGas{
test_plus_1_1(){
const actual = 1 + 2;
const expected = 3;
tester.assertEquals(actual, expected);
}
test_plus_1_2(){
const actual = 2 + 3;
const expected = 5;
tester.assertEquals(actual, expected);
}
test_plus_1_3(){
const actual = 3 + 4;
const expected = 7;
tester.assertEquals(actual, expected);
}
}
function execute_Test_utilForTestGas(){
const arraySkippingTest = ["test_plus_1_3"];
let failureFuncs = tester.executeTestGas(Test_TestGas, arraySkippingTest);
}tester.executeTestGas(Test_TestGas) returns array of functions that test failed from Test_TestGas object.
Furthermore, you can specify testcases that you don't wanna test.
In the code above, test_plus_1_3 is skipped in the test.
Executor.assertEquals (actual, expected, willOutputErrorToReport=true)
Asserts that actual value and expected value are same or not.
Arguments
| name | type | description |
|---|---|---|
| actual | any | Actual value. |
| expected | any | Expected value. |
| willOutputErrorToReport | boolean | Set true if you wanna display result of a testcase. Set false otherwise. |
let tester = createExecutor();
class Test_TestGas{
test_plus_1_1(){
const actual = 1 + 2;
const expected = 3;
tester.assertEquals(actual, expected);
}
test_plus_1_2(){
const actual = 2 + 3;
const expected = 6;
tester.assertEquals(actual, expected);
}
}
function execute_Test_utilForTestGas(){
let failureFuncs = tester.executeTestGas(Test_TestGas);
}test_arrayLength_1_1's test is passing. test_arrayLength_1_2's test is failing.
Executor.assertEqualsArrayLength (actual, expected, willOutputErrorToReport=true)
Asserts that actual array and expected array are same or not in the point of view of length.
Arguments
| name | type | description |
|---|---|---|
| actual | any | Actual array. |
| expected | any | Expected array. |
| willOutputErrorToReport | boolean | Set true if you wanna display result of a testcase. Set false otherwise. |
Example
let tester = createExecutor();
class Test_TestGas{
test_arrayLength_1_1(){
const actual = ["t", "e", "s", "t"];
const expected = ["g", "a", "p", "s"];
tester.assertEqualsArrayLength(actual, expected);
}
test_arrayLength_1_2(){
const actual = ["t", "e", "s", "t"];
const expected = ["t", "e", "s", "t", "g"];
tester.assertEqualsArrayLength(actual, expected);
}
}
function execute_Test_utilForTestGas(){
let failureFuncs = tester.executeTestGas(Test_TestGas);
}test_arrayLength_1_1's test is passing. test_arrayLength_1_2's test is failing.
Executor.assertEqualsArrayItems (actual, expected, willOutputErrorToReport=true)
Asserts that actual array and expected array are same or not.
Arguments
| name | type | description |
|---|---|---|
| actual | any | Actual array. |
| expected | any | Expected array. |
| willOutputErrorToReport | boolean | Set true if you wanna display result of a testcase. Set false otherwise. |
Example
let tester = createExecutor();
class Test_TestGas{
test_arrayLength_1_1(){
const actual = ["t", "e", "s", "t"];
const expected = ["t", "e", "s", "t"];
tester.assertEqualsArrayItems(actual, expected);
}
test_arrayLength_1_2(){
const actual = ["t", "e", "s", "t"];
const expected = ["t", "e", "s", "t", "g"];
tester.assertEqualsArrayItems(actual, expected);
}
test_arrayLength_1_3(){
const actual = ["t", "e", "s", "t"];
const expected = ["t", "s", "e", "t"];
tester.assertEqualsArrayItems(actual, expected);
}
}
function execute_Test_utilForTestGas(){
let failureFuncs = tester.executeTestGas(Test_TestGas);
}test_arrayLength_1_1's test is passing.
Tests of test_arrayLength_1_2 and test_arrayLength_1_3 are failing.
Executor.assertNotEquals (actual, expected)
Asserts that actual value and expected value are NOT same or same.
Arguments
| name | type | description |
|---|---|---|
| actual | any | Actual value. |
| expected | any | Expected value. |
Example
let tester = createExecutor();
class Test_TestGas{
test_plus_1_1(){
const actual = 1 + 2;
const expected = 4;
tester.assertNotEquals(actual, expected);
}
test_plus_1_2(){
const actual = 2 + 3;
const expected = 5;
tester.assertNotEquals(actual, expected);
}
}
function execute_Test_utilForTestGas(){
let failureFuncs = tester.executeTestGas(Test_TestGas);
}test_arrayLength_1_1's test is passing. test_arrayLength_1_2's test is failing.
Executor.assertError (func, funcArgs, expectedErrorName, willOutputErrorToReport=true)
Asserts that a function call raises expected_exception or raise a failure exception otherwise.
Arguments
| name | type | description |
|---|---|---|
| func | Function | A function targeted for test. |
| funcArgs | any[] | Arguments of function. |
| expectedErrorName | Error | Error name you expect. |
| willOutputErrorToReport | boolean | Set true if you wanna display result of a testcase. Set false otherwise. |
Example
let tester = createExecutor();
class Test_TestGas{
test_arrayLengthIsOneToItem_2_3(){
const arrays = ["aaa", "bbb", "ccc", "vvv", "wwww", "xxxxx"];
tester.assertError(arrayLengthIsOneToItem, [arrays], RangeError);
}
}
function execute_Test_utilForTestGas(){
let failureFuncs = tester.executeTestGas(Test_TestGas);
}If that function arrayLengthIsOneToItem(arrays) throws RangeError, that assertion is passing.
Executor.assertNotError (func, funcArgs, expectedErrorName, willOutputErrorToReport=true)
Asserts that a function call NOT raises expected_exception or raise a failure exception otherwise.
Arguments
| name | type | description |
|---|---|---|
| func | Function | A function targeted for test. |
| funcArgs | any[] | Arguments of function. |
| expectedErrorName | Error | Error name you don't expect that it may thrown. |
| willOutputErrorToReport | boolean | Set true if you wanna display result of a testcase. Set false otherwise. |
Example
let tester = createExecutor();
class Test_TestGas{
test_arrayLengthIsOneToItem_2_4(){
const arrays = ["aaa", "bbb", "ccc", "vvv", "wwww", "xxxxx"];
tester.assertNotError(arrayLengthIsOneToItem, [arrays], RangeError);
}
}
function execute_Test_utilForTestGas(){
let failureFuncs = tester.executeTestGas(Test_TestGas);
}If that function arrayLengthIsOneToItem(arrays) doesn't throw RangeError, that assertion is passing.