Skip to content
Learn with RV – Tech Blog
Learn with RV – Tech Blog

#testautomation #qa #programming #linux #devops

  • Home
  • Who am I?
  • 1-on-1 Mentoring
  • Test Automation Incubator
  • 24 Testimonials
  • YouTube
  • LinkedIn
  • Contact
Learn with RV – Tech Blog

#testautomation #qa #programming #linux #devops

expect-soft-thumbnail-k6
May 24, 2026May 24, 2026

Cleaner asserts in Grafana k6 load tests using expect

Grafana k6 provides an expect() function through the k6-testing library. It lets you write readable assertions such as toBe(), toContain(), toHaveLength(), and toBeTruthy(). According to the Grafana documentation, expect() wraps a value and returns an expectation object with assertion methods. It also supports both regular assertions and soft assertions.

Docs HERE

expect() vs expect.soft()

A regular expect() assertion fails immediately when the condition is not met. That can be useful when the current iteration should stop as soon as something is wrong.

For load testing, though, I often prefer expect.soft().

Soft assertions do not stop the test execution immediately. Instead, they mark the test as failed while allowing the script to continue running. That failed soft assertions can make k6 eventually exit with code 110, while still letting the test reach its normal end condition.

k6-expect

Why this style is useful

The nice thing about this approach is readability. These assertions almost document the API contract by themselves:

k6-expect-status

That line tells us three things:

The actual value is res.status.

The failure message is status should be 200.

The expected value is 200.

This is easier to scan than a large check() block when you have several response validations.

Conclusion

expect makes k6 scripts feel closer to modern JavaScript test code. For API load testing, expect.soft() is usually the more forgiving and informative option because it lets the test continue while still reporting assertion failures.

In short: use hard expect() when the iteration should stop immediately, and use expect.soft() when you want better failure visibility during the full load test run.

Enjoyed this article?
I share more practical automation tips on YouTube and LinkedIn.

Need structured guidance instead of learning alone?
I offer 1-on-1 mentoring – learn more → HERE

Or email me at iamqarv [at] gmail [dot] com

Post Views: 11

Related

Share this article:
k6 Tools

Post navigation

Previous post

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Cleaner asserts in Grafana k6 load tests using expect
  • AI: What Happens When an AI’s Context Window Gets Full?
  • Upgrading Playwright package to latest version in simple steps
  • Oxfmt tutorial – the Prettier code formatter alternative, 30x faster
  • Configuring testIdAttribute in Playwright

Recent Comments

  1. Paul on Web Accessibility: A step-by-step guide to Testing with pa11y
  2. Automated Tests for website Accessibility with Axe and TestCafe - Learn with RV - Tech Blog on How to generate E2E TestCafe Framework in seconds
  3. RV on Exploring Faker.js: A Powerful Tool for Generating Realistic Random Test Data
  4. Adrian Maciuc on Exploring Faker.js: A Powerful Tool for Generating Realistic Random Test Data
  5. Nick on Cypress vs Playwright vs Testcafe – which framework is faster?

Archives

  • May 2026
  • April 2026
  • March 2026
  • February 2026
  • November 2025
  • October 2025
  • September 2025
  • August 2025
  • July 2025
  • June 2025
  • March 2025
  • February 2025
  • January 2025
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023

Categories

  • AI
  • k6
  • Linux
  • Programming
  • QA
  • Tools
  • Uncategorized
©2026 Learn with RV – Tech Blog | WordPress Theme by SuperbThemes