Package: robotframework-cache
Robot Framework Cache LibraryCaching mechanism for Robot Framework tests. Works within a single test run and across multiple test
runs. Works with Pabot, though it requires--pabotlib.WhyIn one word: Performance.When testing, we sometimes want to reuse 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.PabotCacheLibrary works with Pabot, but requires the--pabotlibcommand line argument.Supporting Pabot is achieved by a combination oflocksandparallel 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.InstallationInstall CacheLibrary with pip. Run the following command:pipinstallrobotframework-cacheImport it from your.robotor.resourcefile. Add the following line to theSettingssection:LibraryCacheLibraryAdd the cache file to.gitignore. If you use the default file path, add the following to your.gitignore:robotframework-cache.jsonExamplesBasic usageStore a value, and retrieve it laterCache Store ValuefooHello, world# Do something interesting${value} =Cache Retrieve ValuefooShould Be Equal${value}Hello, worldCaching output of a keywordThe 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 Variableapi-session${cached_token} =Cache Retrieve value${cache_key}IF$cached_token is not $NoneRETURN${cached_token}END# Request a new session tokenCache Store Value${cache_key}${new_token}RETURN${new_token}Alternatively, you can use the convenience keywordRun Keyword And Cache Outputto do the same
thing:Get Api Session Token${token} =Run Keyword And Cache OutputGet Api Session Token UncachedRETURN${token}Get Api Session Token Uncached[Tags]robot:private# Request a new session tokenRETURN${new_token}Retention of cached valuesWhen storing a value in the cache, you also define how long it should remain valid.Cache Store Valuekey=amazingvalue=beautifulexpire_in_seconds=10The valuebeautifulwill expire 10 seconds from the moment it's stored.If you try to retrieve an expired value withCache Retrieve Valueit will returnNonelike it
would if it was never stored.The default retention is 3600 seconds (1 hour).Changing the cache file pathWhen importing the library, you can provide an alternative cache file path.LibraryCacheLibraryfile_path=alternative-cache-file.jsonCache too big warningsA 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:LibraryCacheLibraryfile_size_warning_bytes=1000000Resetting the cacheIf 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 keywordCache Resetfor the same purpose.ContributingContributions are always welcome :)Testing CacheLibraryInstall the dependencies withpoetry.poetryinstall--withtestRun the following commands in the repository root.robottest/integration
robottest/acceptance/run.robot

pabot--pabotlibtest/integration
pabot--pabotlibtest/acceptance/run.robot

pabot--testlevelsplit--pabotlibtest/integration
pabot--testlevelsplit--pabotlibtest/acceptance/run.robot