Helpers

Molotov provides a few helpers to make it easier to write tests.

Global variables

If you need to use an object in various test fixtures or tests, you can use the set_var() and get_var() functions.

molotov.set_var(name, value)

Sets a global variable.

Options:

  • name: name of the variable
  • value: object to set
molotov.get_var(name, factory=None)

Gets a global variable given its name.

If factory is not None and the variable is not set, factory is a callable that will set the variable.

If not set, returns None.

Synchronous requests

If you need to perform synchronous requests in your setup:

molotov.request(endpoint, verb='GET', session_options=None, **options)

Performs a synchronous request.

Uses a dedicated event loop and aiohttp.ClientSession object.

Options:

  • endpoint: the endpoint to call
  • verb: the HTTP verb to use (defaults: GET)
  • session_options: a dict containing options to initialize the session (defaults: None)
  • options: extra options for the request (defaults: None)

Returns a dict object with the following keys:

  • content: the content of the response
  • status: the status
  • headers: a dict with all the response headers
molotov.json_request(endpoint, verb='GET', session_options=None, **options)

Like molotov.request() but extracts json from the response.

from molotov import global_setup, json_request, set_var


@global_setup(args)
def _setup():
    set_var('token', json_request('http://example.com')['content'])