Apparently, when you run k6 load tests and some of them fails, by default you don’t have access to the errors in the console (the only things displayed are the test name and the expected result, but NOT the actual result). In order to see the actual results that caused the failure, you will need to implement an Error Handler class in your project.
For example, in the above image you can’t know why the test failed. The status code was not expected 200, but what was the actual status? Personally, I consider this information mandatory for the debugging process.
The solution
In the project structure, I usually like to create a ‘lib’ folder with a new ‘ErrorHandler.js’ file that contains the following
The next step is to import the ‘errorHandler’ method in our test file.
Note: depending on your test file location, you might have a different “from ‘./lib‘” path. Also double check ErrorHandler ends with ‘.js‘
Last but not least, include the errorHandler.logError method in each of your tests.
Test example:
That’s it! Simple, isn’t it? Now whenever a test happens to fail, you will see in the console the actual error – e.g. below example shows an actual status code of 502 that causes test failure
You can find more information regarding this approach and also the source code in the official Grafana documentation HERE
Enjoyed this article? Make sure to subscribe to my YouTube Channel for more Test Automation tutorials, and follow me on LinkedIn and Twitter for regular insights.
Looking to improve your test automation skills?
I’ve created a personalized 1-on-1 Mentoring program refined to boost YOUR skills. Reach out at iamrv@razvanvancea.ro for more details and together will create a learning path adapted to your current skills and goals that you are aiming for, in a timely-efficient manner🚀