Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for before/after test stage actions #114

Closed
Birne94 opened this issue May 31, 2018 · 1 comment · Fixed by #844
Closed

Add support for before/after test stage actions #114

Birne94 opened this issue May 31, 2018 · 1 comment · Fixed by #844

Comments

@Birne94
Copy link
Contributor

Birne94 commented May 31, 2018

follow up to this comment: #41 (comment)

Currently, the support for pre- and post-stage actions is rather limited. The only ones we support right now are delays and clearing cookies. I suggest adding the possibility to call arbitrary functions here. The format could be similar to the $ext function calls currently allowed for validation.

One use case we have for this would be to use freezegun to simulate changes in time for a single request (e.g. for testing session expiry).

stages:
  - name: test-datetime-endpoint

    before:
      - function: my.package.utils.freeze
        extra_kwargs:
          time: "2018-05-31 12:00:00"

    after:
      - function: my.package.utils.unfreeze

    request:
      url: "{host}/get-time"
      method: GET

    response:
      status_code: 200
      body:
        current_time: "2018-05-31 12:00:00"
# my.package.utils

def freeze(time, context):
    t = context['freezegun'] = freezegun.freeze_time(time)
    t.start()


def unfreeze(context):
    t = context.pop('freezegun')
    t.stop()

Note the context parameter I have added here which allows passing a context object (might just be a dictionary) to the function. This allows a pair of setup- and teardown methods to keep track of created objects. The context may also contain additional info, for example the session is required for clearing the cookies.

The context variable should be injected dynamically, which is controlled either through an attribute in the spec or through a decorator:

      - function: my.package.utils.freeze
        with_context: true
        extra_kwargs:
          time: "2018-05-31 12:00:00"
# sets freeze._context = True
@tavern.utils.inject_context
def freeze(time, context):
    t = context['freezegun'] = freezegun.freeze_time(time)
    t.start()

This allows us to still use functions without context which is useful when using builtins (e.g. time.sleep as a replacement for delay_after and delay_before).

What are your thoughts on this?

@michaelboulton
Copy link
Member

I have a proof of concept that will handle this kind of thing that will be going into a 2.x release #802 If anyone following this issue wants to give any feedback or examples of what they would actually use this for then I can improve it / add tests for those scenarios.

@michaelboulton michaelboulton linked a pull request Feb 8, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants