DEV Community

Cover image for Test context: A powerfull way to debug our tests
Will Drygla
Will Drygla

Posted on

1

Test context: A powerfull way to debug our tests

In this post I will share with you guys an upgrade that I do to my API tests!

To test our system, I use system tests (integration / API / service) where I create a seed of data on database, then, make a request to an endpoint, and later check the results on the database.

The problem: When a test fails, on general, we don´t have much context about how things happened, and in general, we re run the tests locally to collect some info.

  • Also, this tests are great to be used when devs make big refactors that doesn´t change the application rules

My solution: I create on each test, a context variable, that, during test execution, is filled with Id's, responses, data from database, and, after the test execution, if the test failed, we add this data to the XML report.

How I do this:
Before the test:

  • Create a variable named testContext
  • Fill this variable with basic data from our Seed (the principal data the test handle)

During the test:

  • Collect API responses and save to the testContext
  • Collect data from database and save to the testContext before validations

After the test:

  • If the test fail, capture necessary info (test suite and name, failed reason) and add to testContext
  • Save the testContext as a JSON file

This helps our debug, since, when a test fail, we could check this JSON, to see if the initial data is correct, if API responses was OK and how the data was saved into the database, all this inside a JSON of 50 lines hahaha.
With a JSON like this, checking the logs, would be easy find the reason why it failed

I will give you an example of this JSON:

The image shows and json, with test context

After this if the test is running on pipeline, we add the JSON inside the test XML (I will make an post only about the XML handle)

The image shows an XML generate by jest on the end of tests, where we add the test context

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed: Zero in on just the tests that failed in your previous run
  • 2:34 --only-changed: Test only the spec files you've modified in git
  • 4:27 --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • 5:15 --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • 5:51 --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

Watch Full Video 📹️

Top comments (0)

Hot sauce if you're wrong - web dev trivia for staff engineers

Hot sauce if you're wrong · web dev trivia for staff engineers (Chris vs Jeremy, Leet Heat S1.E4)

  • Shipping Fast: Test your knowledge of deployment strategies and techniques
  • Authentication: Prove you know your OAuth from your JWT
  • CSS: Demonstrate your styling expertise under pressure
  • Acronyms: Decode the alphabet soup of web development
  • Accessibility: Show your commitment to building for everyone

Contestants must answer rapid-fire questions across the full stack of modern web development. Get it right, earn points. Get it wrong? The spice level goes up!

Watch Video 🌶️🔥

👋 Kindness is contagious

If this post resonated with you, feel free to hit ❤️ or leave a quick comment to share your thoughts!

Okay