Skip to content

Latest commit

 

History

History
73 lines (53 loc) · 2.46 KB

VDR311.md

File metadata and controls

73 lines (53 loc) · 2.46 KB

VDR311. Scope variables should not be redefined

In the Vedro Framework, utilizing data stored in 'self' allows for better understanding in case of a failed test. If scope variables undergo changes, it becomes more challenging (or potentially impossible) to determine the original data used prior to the mutation.

❌ Anti-pattern

class Scenario(vedro.Scenario):
    subject = "get user info after update"

    def given_registered_user(self):
        self.user = registered_user()
   
   def given_updated_username(self):
        self.user = updated_username(self.user) # <- mutation

    def when_user_gets_info(self):
        self.response = httpx.get(f"{API_URL}/user/info/{self.user['id']}"

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

    def and_then_it_should_return_user_info(self):
        assert self.response.json() == NewUserSchema % {
            "username": self.user["username"],
        }

✅ Best practice

class Scenario(vedro.Scenario):
    subject = "get user info after update"

    def given_registered_user(self):
        self.user = registered_user()
   
   def given_updated_username(self):
        self.updated_user = updated_username(self.user)

    def when_user_gets_info(self):
        self.response = httpx.get(f"{API_URL}/user/info/{self.user['id']}"

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

    def and_then_it_should_return_user_info(self):
        assert self.response.json() == NewUserSchema % {
            "username": self.updated_user["username"],
        }

⚠️ Exception

At times, scope variables serve as a representation of a page or application state, particularly in front-end tests. In such instances, it is permissible to redefine them, and this can be configured in the setup.cfg file using the parameter allowed_to_redefine_list=page,page1.

class Scenario(vedro.Scenario):
    subject = "submit confirmation code"

   def given_code(self):
      self.code = fake(ConfirmationCodeSchema)

   def given_opened_main_page(self):
      self.page = opened_main_page()

   def given_opened_popup(self):
      opened_confirm_popup_for_sign_up()
    
   def when_user_submits_form(self):
      self.page.confirm_popup.code.fill()

   def then_profile_should_be_visible(self):
      self.page = self.page.as_page(AuthorizedProfilePage)
      assert self.page.profile_frame.is_visible()