This guide illustrates how to add tests to the vtk.js repository and how to run and debug them.
First each VTK class can have several tests spread among several files but we also have infrastructure for global tests which live inside Sources/Testing/test*.js.
vtk.js uses tape for running tests. Please refer to tape’s documentation for more information about the test harness API.
Class tests
In order to add test to vtk.js you will need to create a test directory underneath your class directory.
That test directory should contain as many test file as you like with their associated baselines if any. By convention we expect each test file to be prefixed with test similar to testDataArray.js.
Testing plain JavaScript
Some vtkClass’es don’t necessarily involve rendering and can be tested without a WebGL environment. In which case a plain JavaScript test could be written as follows and an additional test() function could be added within the same file:
import test from 'tape'; |
Testing rendering with image comparison
import test from 'tape'; |
Registering a test
Tests are automatically picked up by Karma, our test runner.
Running all the tests
In order to run all the registered test, run the following command:
$ npm run test |
That command can also be written as npm t
or npm test
.
Test reporting
After running tests, a report file will be written to Utilities/TestResults/Test-Report.html
. Opening this file in a browser will show which tests have passed, which tests have failed, and which tests have associated images.
Running a single test for debugging
In your test file, specifying test.only(...)
will tell the test harness to only run your test. Don’t forget to remove it once your test is ready!
-test('my test description', (t) => {}) |
Then executing npm t
will only run that file, but if you want to open a browser and debug the actual code, you can do it with the following command:
$ npm run test:debug |
This will automatically open a browser and run the test. But you can manually open http://localhost:9876/debug.html
with any browser and start debugging.
Moreover, when doing some rendering that’s a great way for building your baseline image.
Creating a new baseline image
Follow the following procedure to create a new baseline or change an existing baseline for a test.
- Add an invalid baseline (any PNG file) and rename it as the required baseline.
For example, to create a baseline fortestCylinder.js
copy testCone.png to Sources/Filters/Sources/CylinderSource/test/testCylinder.png. - Run the test as per Running a single test for debugging. The test should fail because of the invalid baseline.
- The test execution creates a file Utilities/TestResults/Test-Report.html. Open this in the browser.
- The file should show the test output versus the invalid baseline image, as well as a diff.
Right-click on the test output image and save it as the valid baseline. - Re-run the test to ensure that it passes with the valid baseline.
- Commit the baseline image to the git source tree.