From e5e0a7d375eff6b22b7799a76e71b133906bdbd6 Mon Sep 17 00:00:00 2001 From: Diya Date: Sat, 27 Jan 2024 11:33:17 +0530 Subject: [PATCH 1/3] Adds Error message for when there are no screenshots and the developer might need to wait for a promise --- test/unit/visual/visualTest.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/unit/visual/visualTest.js b/test/unit/visual/visualTest.js index 24dbec2f53..a0bc033b7c 100644 --- a/test/unit/visual/visualTest.js +++ b/test/unit/visual/visualTest.js @@ -151,6 +151,12 @@ window.visualTest = function( actual.push(myp5.get()); }); + + if (actual.length === 0) { + throw new Error('No screenshots were generated. Check if your test generates screenshots correctly.If the test includes asynchronous operations, ensure they complete before the test ends.'); + } + + if (expectedScreenshots && actual.length !== expectedScreenshots) { throw new Error( `Expected ${expectedScreenshots} screenshot(s) but generated ${actual.length}` From 9947e225722b3765f9a8dc1d08b05c52687b1a0a Mon Sep 17 00:00:00 2001 From: Diya Date: Sat, 27 Jan 2024 14:42:27 +0530 Subject: [PATCH 2/3] updated visual test documentation --- contributor_docs/unit_testing.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/contributor_docs/unit_testing.md b/contributor_docs/unit_testing.md index 64ef0f73b9..35efe785eb 100644 --- a/contributor_docs/unit_testing.md +++ b/contributor_docs/unit_testing.md @@ -139,3 +139,31 @@ If you need to add a new test file, add it to that folder, then add the filename When you add a new test, running `npm test` will generate new screenshots for any visual tests that do not yet have them. Those screenshots will then be used as a reference the next time tests run to make sure the sketch looks the same. If a test intentionally needs to look different, you can delete the folder matching the test name in the `test/unit/visual/screenshots` folder, and then re-run `npm test` to generate a new one. To manually inspect all visual tests, run `grunt yui:dev` to launch a local server, then go to http://127.0.0.1:9001/test/visual.html to see a list of all test cases. + + +The visual test environment is set up to execute your commands sequentially rather than running a preload or draw function that you provide. +In continuous integration (CI) environments, it's crucial to keep tests running as quickly as possible. Running a full `preload/draw` cycle as in a regular p5.js sketch can significantly slow down the testing process. +When testing features like 3D model rendering, you might encounter scenarios where you need to load a model before performing assertions. Here's an example of how you can handle Sequential Command Execution and Asynchronous Operations in your visual tests: + + +```js +visualSuite('3D Model rendering', function() { + visualTest('OBJ model is displayed correctly', function(p5, screenshot) { + // Return a Promise to ensure the test runner waits for the asynchronous operation to complete + return new Promise(resolve => { + p5.createCanvas(50, 50, p5.WEBGL); + // Load the model asynchronously + p5.loadModel('unit/assets/teapot.obj', model => { + p5.background(200); + p5.rotateX(10 * 0.01); + p5.rotateY(10 * 0.01); + p5.model(model); + // Take a screenshot for visual comparison + screenshot(); + // Resolve the Promise to indicate completion + resolve(); + }); + }); + }); +}); +``` From d502d83d3a56d71c10410d7aacd8a488727fb30d Mon Sep 17 00:00:00 2001 From: Diya Date: Wed, 31 Jan 2024 18:24:50 +0530 Subject: [PATCH 3/3] Fixes error messages and documentation --- contributor_docs/unit_testing.md | 6 ++---- test/unit/visual/visualTest.js | 4 +--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/contributor_docs/unit_testing.md b/contributor_docs/unit_testing.md index 35efe785eb..072ba65705 100644 --- a/contributor_docs/unit_testing.md +++ b/contributor_docs/unit_testing.md @@ -141,10 +141,8 @@ When you add a new test, running `npm test` will generate new screenshots for an To manually inspect all visual tests, run `grunt yui:dev` to launch a local server, then go to http://127.0.0.1:9001/test/visual.html to see a list of all test cases. -The visual test environment is set up to execute your commands sequentially rather than running a preload or draw function that you provide. -In continuous integration (CI) environments, it's crucial to keep tests running as quickly as possible. Running a full `preload/draw` cycle as in a regular p5.js sketch can significantly slow down the testing process. -When testing features like 3D model rendering, you might encounter scenarios where you need to load a model before performing assertions. Here's an example of how you can handle Sequential Command Execution and Asynchronous Operations in your visual tests: - +In a continuous integration (CI) environment, optimizing test speed is essential. It is advantageous to keep the code concise, avoid unnecessary frames, minimize canvas size, and load assets only when essential for the specific functionality under test. +To address scenarios involving operations like asynchronous 3D model rendering, consider returning a promise that resolves upon completing all the necessary tests, ensuring efficiency in your visual testing approach. Here's an example of how you can asynchronous 3D model rendering in your visual tests: ```js visualSuite('3D Model rendering', function() { diff --git a/test/unit/visual/visualTest.js b/test/unit/visual/visualTest.js index a0bc033b7c..ec66fc3e07 100644 --- a/test/unit/visual/visualTest.js +++ b/test/unit/visual/visualTest.js @@ -153,10 +153,8 @@ window.visualTest = function( if (actual.length === 0) { - throw new Error('No screenshots were generated. Check if your test generates screenshots correctly.If the test includes asynchronous operations, ensure they complete before the test ends.'); + throw new Error('No screenshots were generated. Check if your test generates screenshots correctly. If the test includes asynchronous operations, ensure they complete before the test ends.'); } - - if (expectedScreenshots && actual.length !== expectedScreenshots) { throw new Error( `Expected ${expectedScreenshots} screenshot(s) but generated ${actual.length}`