Skip to main content

A package for managing stability tests

Project description

Stability

This package provides the @stability_test decorator, which can be used to easily manage stability tests.

Background

We often want to wrap our code in what I would call "stability tests".

These tests ensure that nothing has changed about the function(s) being tested, and often look something like this:

def test_some_func_stability():
    out = some_func()
    expected = pd.DataFrame([[1, 2.2], [3, 4.4]])
    pd.testing.assert_frame_equal(out, expected)

But updating tests like this can be time consuming - if the output of a function has changed, we will have to

  • add a print statement to our test
  • copy the new output values
  • paste them into our test as the new expected values

This can be a pain, even more so if multiple tests need updating.

A better way

The @stability_test decorator aims to take all the effort out of managing stability tests, and also makes their implementation quicker and neater. The example test shown above would simply become:

@stability_test
def test_some_func_stability():
    return some_func()

A few notes:

  • the test now simply returns the output to be checked
  • the @stability_test decorator takes care of checking the output vs the expected values

How does the library know what the expected values are?

The first time you create a stability test with @stability_test you simply pass in write=True.

@stability_test(write=True)
def test_some_func_stability():
    return some_func()

When you run the test with write=True, the output will be saved to a CSV file, in a resources sub-folder in the same folder as the test file.

Once this CSV is written, it becomes the expected output of the test, and whenever the test runs in future the decorator will automatically compare the test's output (return value) against the contents of the CSV

You can add the CSV file to your repository, and the expected output of the test is then stored in a clean human-readable format. Any future changes in the output of the function will be clearly visible in the diff on the CSV file. And it only takes adding write=True and re-running the test to upate the values in the CSV - no adding print statements, copying and pasting.

NOTE: Remember to remove write=True once the CSV file has been written or updated - otherwise your unit tests are not running as intended - they will be constantly re-saving new data when they run rather than comparing against the intended expected values.

Checking multiple outputs in the same test

The decorator can also be used to check values which are yielded from a test set up as a generator function. For example:

@stability_test
def test_some_func_stability_two_cases():
    yield some_func(1, 'a')
    yield some_func(2, 'b')

This test will compare the outputs of each function call to the contents of an associated CSV file. Both files can be updated at once by running the test with write=True, as discussed above.

Updating outputs for multiple tests at once

The decorator is most useful when updating multiple expected values (across multiple tests). Rather than repeating the work of adding write=True to each test individually, you can set the environment variable

STABILITY_WRITE_DATA=1

Then run any tests for which you want to update expected values. The decorator will take care of the rest - all expected output CSVs will be updated. You just need to add/commit the changed CSV files to git or whichever VCS you are using.

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

stability-0.0.4.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stability-0.0.4-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file stability-0.0.4.tar.gz.

File metadata

  • Download URL: stability-0.0.4.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.6

File hashes

Hashes for stability-0.0.4.tar.gz
Algorithm Hash digest
SHA256 93bce130533ef1d9dce1ecda612b410762adfdb11829e583698939f84b62e4c6
MD5 ecf17bf8459ce41a098eb530ee38f37a
BLAKE2b-256 4f62c1fc3d71ab787ecb9c8019994d796dc3a920b9d079c1c5204ee16c041af4

See more details on using hashes here.

File details

Details for the file stability-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: stability-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.6

File hashes

Hashes for stability-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3135e006a2ef6b6aa374e6eadfd1de3188224b9862513c7ef612321486782fa1
MD5 b7cafb51a69e8d2d6a8781dd89b2a4b1
BLAKE2b-256 cc88e1b8104ad8dcf9c437afe73fe2f269c1bd32445e6651e86b034677cda748

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page