Skipping Through Tests
When developing something that should withstand public use, a good set of tests is always beneficial; however, sometimes you may want to skip a few without changing the code.
When developing something that should withstand public use, a good set of tests is always beneficial; however, sometimes you may want to skip a few without changing the code.
One should design test cases with automation in mind. It means that the ideal test suite runs against the target environment and, on the happy path, leaves nothing behind but a report. For my specific example, it's a system adapter for the OCI Buckets. The set of tests that I developed:
- Test Basic Functionality
- Test all parameters present
- Test basic methods
- Tests the OCI SDK functionality
- Test OCI connectivity and access
- Test bucket access
- Perform PUT/GET tasks
- Test FETCH and DELETE
Means that at the end of the case, the target bucket will contain no artefacts, except log entries. However, for some cases, it would be useful to see what objects are in the bucket. My tests were built with Mocha.js. The framework offers excellent flexibility, allowing you to select and test suites and cases, including filtering by description.
# Runs All Tests in Folder
$ _mocha -- tests/*.test.js
# Run Specific Test File
$ _mocha -- tests/BasicFunctions.test.js
# Run only tests, having matching description
$ _mocha --grep "Storage Adapter" -- tests/*.test.jsSelect Tests and Cases
But if you want to skip a specific test, you'll need to make some logic changes. I checked a few options and settled on the most universal approach: environment variables. Here is an example of skipping test file deletion.
it('should delete the file from the bucket', async function () {
if (process.env.KEEP_FILES)
this.skip()
else
return adapter.delete(pth.basename(testImage.name),
'save-test').then((result) => {
assert.ok(result, 'File deletion failed');
}).catch(err => {
assert.fail(`File deletion failed with error:
${err.message}`);
});
});Delete File Test
Now, this step checks whether the KEEP_FILES variable is declared, and if so skips the deleteion step.
# Runs All Tests but keeps test files
$ KEEP_FILES=yes _mocha -- tests/*.test.js
Skip the deletion step.