Skip to main content

Scenario Scope

What is Scope?​

In Vedro, the Scope serves as a snapshot of important variables, capturing their current states when a test scenario fails. This snapshot offers valuable insights into the execution environment, making it highly useful for debugging and understanding the cause of the test failure.

A Practical Example​

To better understand how the Scope works, consider the following real-world example.

Imagine a scenario that tests sending a chat message. The process begins with a logged-in user, sends a message via the ChatApi and then verifies that the received response meets expectations.

import vedro
from contexts import logged_in_user
from interfaces import ChatApi

class Scenario(vedro.Scenario):
subject = "send message"

def given_user_token(self):
self.token = logged_in_user()

def given_message(self):
self.message = "banana"

def when_user_sends_message(self):
self.response = ChatApi().send(self.message, self.token["token"])

def then_it_should_return_success_response(self):
assert self.response.status_code == 200

def and_it_should_return_sent_message(self):
assert self.response.json() == {
"username": self.token["username"],
"text": self.message,
}

In this scenario, the variables token, message, and response are critical to the test's success. They are stored as instance variables within the Scenario class:

  • token: Retains the user's token details
  • message: Holds the chat message to be sent
  • response: Captures the server's reply

What Happens When a Test Fails?​

When you run the command with verbose output (-v) and a test scenario fails, a snapshot of key variables at that specific moment is captured and displayed as the Scope, alongside the exception.

$ vedro run -v

Exception:

╭────────────────────── Traceback (most recent call last) ───────────────────────╮
│ ./scenarios/send_message/send_message.py:28 │
│ 26 │ │
│ 27 │ def and_it_should_return_sent_message(self): │
│ ❱ 28 │ │ assert self.response.json() == MessageSchema % { │
│ 29 │ │ │ "username": self.token["username"], │
│ 30 │ │ │ "text": self.message["text"], │
│ 31 │ │ } │
╰────────────────────────────────────────────────────────────────────────────────╯
ValidationException:
- Value <class 'str'> at _['text'] must be equal to 'banana', but '0RLyemKwG53' given

Scope:

Scope
token: {
"username": "vzwmkast",
"token": "0f8fefc25185eb869fd5acbe6b6778523d6172b7",
"created_at": 1694250002
}
message: {
"text": "0RLyemKwG53",
"chat_id": "Cuyjp"
}
response: Response:
HTTP/1.1 200 OK
content-type: application/json; charset=utf-8
content-encoding: gzip
{
│ "username": "vzwmkast",
│ "text": "banana",
│ "sent_at": 1694250002
}

Benefits of Using Scope​

  • Debugging Made Easier: With Scope, debugging becomes more straightforward. You receive an immediate snapshot of crucial variables at the moment of failure. This eliminates guesswork and accelerates the troubleshooting process.

  • Replicating Errors: Encountering an error once is challenging, but consistently reproducing it is even more so. The Scope output serves as a valuable tool, significantly simplifying the recreation of the error for more detailed testing and analysis.

  • Team Communication: Collaborative debugging can be complicated without the right information. Sharing a snapshot of the Scope when a test fails gives team members a clearer picture of the situation, streamlining both communication and resolution.