Robot Framework libary for caching values across tests and test runs
Project description
Robot Framework Cache Library
Caching mechanism for Robot Framework tests. Works within a single test run and across multiple test runs. Works with Pabot.
Why
In one word: Performance.
When testing, we sometimes want to reuse work we did in a different test. Examples of this include API 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.
Keyword documentation
For full keyword documentation, see: https://lakitna.github.io/robotframework-cache
Installation
-
Install CacheLibrary with pip. Run the following command:
pip install robotframework-cache
-
Import it from your
.robotor.resourcefile. Add the following line to theSettingssection:Library CacheLibrary
-
Add the cache file to
.gitignore. If you use the default file path, add the following to your.gitignore:robotframework-cache.json
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
VAR ${cache_key} = api-sessions
${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). You can change this default when importing the library.
Changing the cache file path
When importing the library, you can provide an alternative cache file path.
Library CacheLibrary file_path=alternative-cache-file.json
Cache too big warnings
A big cache file can indicate an issue with CacheLibary or with how it's used. To help you spot these issues, CacheLibrary will warn you if the cache file is very big. By default it will warn if the file is larger than 500Kb.
You can change this threshold when importing the library. For example, changing it to 1Mb:
Library CacheLibrary file_size_warning_bytes=1000000
Resetting the cache
If you need to reset the cache for any reason, simply remove or empty the cache file
(default: robotframework-cache.json).
Alternatively, you can use the keyword Cache Reset for the same purpose.
Common questions
What can I cache?
Anything that is JSON serializable.
CacheLibrary uses a JSON file to store data. Because of this, anything you cache must be able to fit in a JSON file. In practice, this means that most Robot variables can be stored in the cache.
Supported values include (but are not limited to):
- String
- Integer
- Float
- Boolean
- Dictionary
- List
Should I commit my cache file?
No, you should not commit your cache file. The data in your cache file will become outdated quickly. It also makes no sense to review it or to keep a history on it.
It can even be a security issue. For example: If you store session information (like an API token) in your cache, you can expose yourself to session highjacking attacks.
What about Browser storage states?
In Browser Library there is a concept called 'Storage States'. At first glance, there is a lot of overlap between CacheLibrary and these storage states. Here is a small flowchart to help you choose between the two:
┌────────────────────────────┐
┌─────────────────────────────┐ │ │
│ │ Yes │ Do you only want to store: │
│ Do you use Browser Library? ├───────────►│ - Browser cookies │
│ │ │ - Browser local storage │
└──────────────┬──────────────┘ │ │
│ └───┬─────────┬──────────────┘
│ No │ │
No │ ┌─────────────────────────┘ │ Yes
│ │ │
▼ ▼ ▼
┌──────────────────┐ ┌─────────────────────┐
│ │ │ │
│ Use CacheLibrary │ │ Use Browser Library │
│ │ │ storage states │
└──────────────────┘ │ │
└─────────────────────┘
Does CacheLibrary work with Pabot?
CacheLibrary works with Pabot.
- Pabot @ <2.2.0 is not supported
- Pabot @ >=2.2.0 and <4 requires the
--pabotlibcommand line argument. - Pabot @ >=4 won't work with the
--no-pabotlibcommand 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.
Contributing
Contributions are always welcome :)
Testing CacheLibrary
-
Install the dependencies with
poetry.uv sync --with dev
-
Run the following command in the repository root.
uv run invoke test
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file robotframework_cache-1.2.0.tar.gz.
File metadata
- Download URL: robotframework_cache-1.2.0.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.8 {"installer":{"name":"uv","version":"0.10.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
86fc698e81e790831137ec355f0e14a84c9b88dc2a60e4d765380f1a27e8090f
|
|
| MD5 |
94fe2582ef01f74da32181054aeb5558
|
|
| BLAKE2b-256 |
603071d3628258e2bc98c0c97ebd5d964e8cfa59c6f2a8ab408b8d8fdee87259
|
File details
Details for the file robotframework_cache-1.2.0-py3-none-any.whl.
File metadata
- Download URL: robotframework_cache-1.2.0-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.8 {"installer":{"name":"uv","version":"0.10.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
985657b1dce0b3d18d65ead6398ed94459da97a2839376d25a1b5afc47743d11
|
|
| MD5 |
68e8601168b12e9cad92497cd9e2d22d
|
|
| BLAKE2b-256 |
2358b9b314976581110426a861d4d93b84036731dc5b162edcd7d7e313764e56
|