Skip to main content

TestFlows.com Open-Source Software Testing Framework Stash

Why

Allows to stash values or files that are generated during test program execution that could be reused on the next test program run.

Usage

Use stashed context manager to stash a value. If the value identified by name is found inside the stash then the code within the with block is not executed.

Example

Here is an example how to use testflows.stash module and the stashed context manager to stash a value returned by a function that takes a non trivial time to execute.

example1.py

import time
from testflows.stash import stashed

def generate_value():
    print("Generating a value that takes a long time...")
    time.sleep(10)
    return "my generated value"

with stashed("value") as stash:
   stash(generate_value())

print(stash.value)

In this example, we simulate the work done by the function using time.sleep(10). The stashed value is identified by the name argument passed when creating an instance of the stash using the stashed() context manager.

On the first run, when the value is not in a stash the code within the with block is executed where the value to be stashed is added by calling stash instance with the value to be stashed. In this case, the result of the generated_value() function.

$ python3 example1.py
Generating a value that takes a long time...
my generated value

Note that stash folder will be created in the same directory as the source file.

$ find
./stash
./stash/example1.py.stash
./example1.py

The stash folder will contain a file that stores the stashed value by using the name specified upon creation of the stash instance. The name of the stash file will have the same prefix as the original source file.

The content of the file will have the following

$ cat ./stash/example1.py.stash 
value = '"my generated value"'

On the second run, the value is found in a stash and the body of the with block is skipped and the generated_value() is not called and the stashed value is available using the stash.value where stash is the instance of the stashed() context manager.

$ python3 example1.py
my generated value

stashed()

The stashed context manager can take the following arguments.

stashed(name, id=None, output=None, path="./stash", encoder=json, use_stash=True)

where

  • name name of the stashed value inside the stash file
  • id custom stash id, default: None
  • output function to output the representation of the value, default: None
  • path custom stash folder path, default: ./stash
  • encoder custom encoder for the value, default: json
  • use_stash use stash, default: True. If False, then the stash will not be used and the with block is always executed regardless if the value is already stored in a stash

In general, stash file name format is defined as

<source file name>.<id>.stash

Metadata

Release files for testflows.stash 1.1.230317.1211113

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for testflows.stash 1.1.230317.1211113
File Size Uploaded
testflows.stash-1.1.230317.1211113.tar.gz 31.8 kB Details

Release files / testflows.stash-1.1.230317.1211113.tar.gz

Download URL testflows.stash-1.1.230317.1211113.tar.gz
Size 31.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0b7bd8606e95432347520211ea2ec781b4dda5a4d3679d1e93c3bc5ca0a1ab5e
BLAKE2b-256 checksum
How to use checksums
d61d12432568365fb6ab89292843e54504ccfa83218508c1dda235cf8b26b526
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page