Skip to content

Example Tests

Below are example test cases demonstrating BDD-style usage with this framework:

Swagger API add pet test

Uses a dataprovider with 4 random pets.

Requires live Swagger API at https://petstore.swagger.io/v2/

    @pytest.mark.parametrize("pet", SwaggerPetstorePet.random(range(4)))
    def should_add(self, pet: SwaggerPetstorePet):
        (self.steps
            .given.swagger_petstore(self.rest_session)
            .when.adding(pet)
            .then.the_available_pets(yields_item(tracing(is_(pet)))))

Mocked Swagger API add pet test

Defines expected requests and then same test flow as above.

    @pytest.mark.parametrize("pet", SwaggerPetstorePet.random(range(10)))
    @responses.activate
    def should_add(self, pet: SwaggerPetstorePet):
        responses.add(
            responses.POST,
            self.configuration.resource_uri(path="pet"),
            json=EMPTY_STRING,
            status=200)
        responses.add(
            responses.GET,
            self.configuration.resource_uri(path="pet/findByStatus"),
            json=[asdict(pet)],
            status=200)
        (self.steps
            .given.swagger_petstore(self.rest_session)
            .when.adding(pet)
            .then.the_available_pets(yields_item(tracing(is_(pet)))))

TerminalX (UI) search test

Requires live https://www.terminalx.com

    # NOTE sections may be further collected in superclasses and reused across tests
    def login_section(
            self, user: TerminalXUser) -> TerminalXSteps[TerminalXConfiguration]:
        return (self.steps
                .given.terminalx(self.web_driver)
                .when.logging_in_with(user.credentials)
                .then.the_user_logged_in(is_(user.name)))

    def should_login(self):
        self.login_section(self.configuration.random_user)

    def should_find(self):
        (self.login_section(self.configuration.random_user)
            .when.clicking_search())

        for word in ["hello", "kitty"]:
            (self.steps
             .when.searching_for(word)
             .then.the_search_hints(yields_item(tracing(
                 contains_string_ignoring_case(word)))))