Skip to content

Commit 4b255e2

Browse files
authored
Merge pull request #6773 from diyaayay/Error-Handling/visualTests
Adds Error message for when there are no screenshots
2 parents 8a9d9f6 + d502d83 commit 4b255e2

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

contributor_docs/unit_testing.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,29 @@ If you need to add a new test file, add it to that folder, then add the filename
139139
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.
140140
141141
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.
142+
143+
144+
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.
145+
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:
146+
147+
```js
148+
visualSuite('3D Model rendering', function() {
149+
visualTest('OBJ model is displayed correctly', function(p5, screenshot) {
150+
// Return a Promise to ensure the test runner waits for the asynchronous operation to complete
151+
return new Promise(resolve => {
152+
p5.createCanvas(50, 50, p5.WEBGL);
153+
// Load the model asynchronously
154+
p5.loadModel('unit/assets/teapot.obj', model => {
155+
p5.background(200);
156+
p5.rotateX(10 * 0.01);
157+
p5.rotateY(10 * 0.01);
158+
p5.model(model);
159+
// Take a screenshot for visual comparison
160+
screenshot();
161+
// Resolve the Promise to indicate completion
162+
resolve();
163+
});
164+
});
165+
});
166+
});
167+
```

test/unit/visual/visualTest.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ window.visualTest = function(
151151
actual.push(myp5.get());
152152
});
153153

154+
155+
if (actual.length === 0) {
156+
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.');
157+
}
154158
if (expectedScreenshots && actual.length !== expectedScreenshots) {
155159
throw new Error(
156160
`Expected ${expectedScreenshots} screenshot(s) but generated ${actual.length}`

0 commit comments

Comments
 (0)