Skip to content

Latest commit

 

History

History
68 lines (49 loc) · 1.98 KB

VDR302.md

File metadata and controls

68 lines (49 loc) · 1.98 KB

VDR302. Interface should not be used in given or asserted steps

Wrapping interfaces into contexts is believed to be the best practice: it increases the level of abstraction and readability and also guarantees that a scenario is atomic (only one action in the main act is tested). So using interfaces not in “when” step is considered to be a mistake that should be prevented via linter.

❌Anti-pattern

# scenarios/login_registered_user.py
from interfaces.chat_api import ChatApi


class Scenario(vedro.Scenario):
    subject = "login as registered user"

    def given_user(self):
        self.user = fake(NewUserSchema)
        ChatApi().register(self.user)

    def when_user_logs_in(self):
        self.response = ChatApi().login(self.user)

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

    def and_it_should_return_created_token(self):
        assert self.response.json() == AuthTokenSchema % {
            "username": self.user["username"]
        }

✅Best practice

# ./contexts/registered_user.py
import vedro
from interfaces.chat_api import ChatApi

@vedro.context
def registered_user(user):
    response = ChatApi().register(user)
    assert response.status_code == 200


# scenarios/login_registered_user.py
from interfaces.chat_api import ChatApi

class Scenario(vedro.Scenario):
    subject = "login as registered user"

    def given_user(self):
        self.user = fake(NewUserSchema)
        registered_user(self.user)

    def when_user_logs_in(self):
        self.response = ChatApi().login(self.user)

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

    def and_it_should_return_created_token(self):
        assert self.response.json() == AuthTokenSchema % {
            "username": self.user["username"]
        }

Additional links