This tutorial will guide you through using the testing framework included in this repository. It is designed to help you write, run, and verify JavaScript tests efficiently. This framework is especially useful for developers working on Google Apps Script projects.
The first step is to create a test class that contains the tests you want to run. This class should include the setup (before) and teardown (after) methods as well as the actual test methods.
- Setup (
before): This method runs before each test and can be used to set up any required state or dependencies. - Teardown (
after): This method runs after each test and can be used to clean up.
Each test method must start with test_ to be recognized by the test runner.
class ExampleTest {
before() {
// Setup code before every test
Logger.log('Setting up before the test');
}
after() {
// Teardown code after every test
Logger.log('Cleaning up after the test');
}
test_addition() {
// This is a sample test to verify addition functionality
Logger.log('Testing addition');
Assert.equals(2 + 2, 4);
}
test_subtraction() {
// This is a sample test to verify subtraction functionality
Logger.log('Testing subtraction');
Assert.equals(5 - 3, 2);
}
}To run your tests, you need to create an instance of the ExampleTest class and pass it to the Test class, which will execute all test methods.
function test_Example() {
const testObject = new ExampleTest();
new Test(testObject).run();
}This function creates an instance of ExampleTest and calls the run() method to execute all the test methods prefixed with test_.
The output of the tests is displayed using Logger.log statements. The framework will log the following information:
- Test Execution: Which test is being run.
- Results: Number of tests that passed, number of tests that failed, and detailed error messages for any failed tests.
Running test: addition
Test passed: addition
Running test: subtraction
Test passed: subtraction
2 tests passed
0 tests failed
In the case of a failure, the framework will log the specific error:
Running test: addition
Test failed: addition - Assertion failed: expected 5, but got 4
1 tests passed
1 tests failed
You can create more complex tests by using the available assertions. Here are some examples:
- Basic Assertions:
Assert.isTrue(value): Validates thatvalueistrue.Assert.equals(expected, actual): Checks strict equality betweenexpectedandactual.Assert.deepEquals(array1, array2): Verifies that two arrays, including nested arrays, are equal.
class ArrayTest {
test_arrayEquality() {
const array1 = [1, [2, 3], 4];
const array2 = [1, [2, 3], 4];
Assert.deepEquals(array1, array2);
}
}
function test_Array() {
const arrayTest = new ArrayTest();
new Test(arrayTest).run();
}- Use
beforeandafterhooks to minimize repetitive setup and teardown code. - Name your tests descriptively to understand their purpose easily (e.g.,
test_userAuthenticationrather thantest_auth). - Keep tests independent: Each test should be able to run on its own without depending on other tests.
- Define your test class and include
before,after, andtest_methods. - Use the
Testrunner to execute your tests. - Utilize assertions provided by the
Assertobject to verify expected outcomes.
By following this tutorial, you should be able to create effective tests for your JavaScript code using this framework. Happy testing!