This repository contains a simple yet functional testing framework designed for JavaScript. It provides an easy-to-use interface for defining and running unit tests, featuring hooks for test setup (before) and teardown (after) as well as a series of assertion methods to validate your test results. This can be particularly helpful when building quick tests for Google Apps Script projects.
- The
Testclass is used to execute test cases from a given test class. - Any method in your test class that starts with
test_will be treated as a test case. - The
beforeandaftermethods, if defined, are called before and after each test respectively. - Test results include a summary of passed and failed tests, along with error messages.
-
Test Runner (
Testfunction)- Runs test cases from a provided test class.
- Reports results including passed and failed tests and error messages.
-
Assertions (
Assertobject)isTrue(value): Verifies that a value istrue.match(expected, actual): Verifies thatexpectedloosely equalsactual.equals(expected, actual): Verifies thatexpectedstrictly equalsactual.deepEquals(array1, array2): Verifies deep equality of two arrays, including nested arrays.
-
Example Test Class (
ExampleTest)- Shows how to write test cases using the framework.
- Includes
beforeandafterhooks for setup and teardown processes.
Here is how you can use the test framework:
- Create a Test Class: Define a class containing your test methods. Test methods should be prefixed with
test_.
class ExampleTest {
before() {
// Setup code before every test
Logger.log('before()');
}
after() {
// Teardown code after every test
Logger.log('after()');
}
test_testName1() {
// Example failing test
Logger.log('GIVEN');
Logger.log('WHEN');
Logger.log('THEN');
Assert.equals(true, false);
}
test_testName2() {
// Example passing test
Logger.log('GIVEN');
Logger.log('WHEN');
Logger.log('THEN');
Assert.isTrue(true);
}
}- Run the Tests: Instantiate the test class and pass it to the
Testfunction.
function test_Example() {
const object = new ExampleTest();
new Test(object).run();
}- Copy the
ExampleTestclass andtest_Examplefunction to your implementation file. - Rename the class and methods to match what you are testing.
- Use
beforeandafterto set up and clean up as needed. - Prefix your test methods with
test_to ensure they are executed.
The Logger.log statements provide a detailed output of which tests are running, which ones pass or fail, and any errors encountered. The results are logged as follows:
- Number of tests passed
- Number of tests failed
- Detailed error messages for each failed test
Running test: testName1
Test failed: testName1 - Assertion failed: expected true, but got false
Running test: testName2
2 tests passed
1 tests failed
Feel free to contribute to this project by creating pull requests. You can add new features, improve existing functionality, or add more examples.
This project is licensed under the Apache 2.0 license.