Announcing Robot Framework Cache Library

Caching mechanism for Robot Framework tests. Works within a single test run and across multiple test runs. Works with Pabot, though it requires --pabotlib.

Why

In one word: Performance.

When testing, we sometimes want to reuse the work we did in a different test. Examples of this include API gateway sessions, user sessions, and expensive calculations. If we can reuse this work, we don’t need to spend time redoing it every test.

Sometimes we even want to reuse the work we did in a previous test run. This allows us to speed up our workflow when writing tests.

CacheLibrary solves these problems by providing a caching mechanism that’s stored both in memory and in a file.

Pabot

CacheLibrary works with Pabot, but requires the --pabotlib command line argument.

Supporting Pabot is achieved by a combination of locks and parallel variables. This makes CacheLibrary stable when run in parallel without losing stored values.

All CacheLibrary tests are run with Pabot to ensure that the above statements are true.

Examples

Basic usage

Store a value, and retrieve it later

Cache Store Value    foo    Hello, world

# Do something interesting

${value} =    Cache Retrieve Value    foo
Should Be Equal    ${value}    Hello, world

Caching output of a keyword

The keyword below fetches or reuses a session token.

Everwhere you need the session token, you can use this keyword. CacheLibrary will ensure that
you’re only requesting a new session token once. After which it will reuse the session token.

CacheLibrary will even do this across test runs. This way, you don’t have to request the session
token every time you run a test. This can be a great help while making or debugging a test.

Get Api Session Token
    ${cache_key} =    Set Variable    api-session
    ${cached_token} =    Cache Retrieve value    ${cache_key}
    IF    $cached_token is not $None
        RETURN    ${cached_token}
    END

    # Request a new session token

    Cache Store Value    ${cache_key}    ${new_token}
    RETURN    ${new_token}

Alternatively, you can use the convenience keyword Run Keyword And Cache Output to do the same
thing:

Get Api Session Token
    ${token} =    Run Keyword And Cache Output    Get Api Session Token Uncached
    RETURN    ${token}

Get Api Session Token Uncached
    [Tags]    robot:private

    # Request a new session token

    RETURN    ${new_token}

Retention of cached values

When storing a value in the cache, you also define how long it should remain valid.

Cache Store Value    key=amazing    value=beautiful    expire_in_seconds=10

The value beautiful will expire 10 seconds from the moment it’s stored.

If you try to retrieve an expired value with Cache Retrieve Value it will return None like it
would if it was never stored.

The default retention is 3600 seconds (1 hour).

More information

For more information see the repository: GitHub - Lakitna/robotframework-cache: Robot Framework libary for caching values across tests and test runs

5 Likes