Skip to content

Latest commit

 

History

History
55 lines (40 loc) · 1.65 KB

VDR312.md

File metadata and controls

55 lines (40 loc) · 1.65 KB

VDR312. Scope variables should not be partially redefined

Extention of VDR311 rule.

Sometimes even partial mutation of scope variables could challenge understanding of the original data that was used in some specific test/

❌ Anti-pattern

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

    def given_registered_user(self):
        self.user = registered_user()

    def given_banned_user(self):
        banned_user(self.user)
        # there is no guarantee that 
        # user status was not banned after registration
        self.user["status"] = "banned"  # <--- partial 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 % {
            "status": self.user["status"],
        }

✅Best practice

class Scenario(vedro.Scenario):
    subject = "get user info with empty username"

    def given_registered_user(self):
        self.user = registered_user()
    
    def given_banned_user(self):
        self.banned_user = banned_user(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 % {
            "status": self.banned_user["status"],
        }