Skip to main content

A cleanup plugin for pytest

Project description

pytest-cleanuptotal

This plugin for pytest and for playwright-pytest helps simplify test cleanup. It does so by providing a systematic way to mark entities for deletion immediately after creation. This is particularly useful when tests involve creating complex structures, such as a bank account with an investment plan and a deposit. Without proper cleanup, attempting to delete the account may result in errors, such as a refusal due to the account not being empty. However, with pytest-cleanuptotal, entities are deleted in the correct order, ensuring that tests clean up after themselves and do not interfere with each other. Read more here.

Installation

$ pip install pytest-cleanuptotal

Usage

To use pytest-cleanuptotal, simply add the cleanuptotal fixture to the test method. This will include the cleanup functionality in your test. No further setup is required. Here's an example:

def test_should_keep_things_tidy(cleanuptotal):
            # ...

            account_id = create_account("John Blow");
            amount = 1000000
            
            # usage of cleanuptotal with lambda function:
            cleanuptotal.add_cleanup(lambda: delete_account(account_id))

            add_investment_plan(account_id, "ModRisk");

            cleanuptotal.add_cleanup(lambda: remove_investment_plan(account_id))
            
            deposit(account_id, amount);

            def remove_deposit():
              url = f"https://api.example.com/user/{accountId}/withdraw"
              data = {"amount": amount}
              response = requests.post(url, json=data)
            
            # usage of cleanuptotal with regular function:
            cleanuptotal.add_cleanup(remove_deposit)

            # ...

        # Please note that the actual execution of the cleanup code would take palce AFTER test completion.
        # Execution order in cleanuptotal would be remove_deposit() -> remove_investment_plan(account_id) -> delete_account(account_id).

To use type hints follow this example:

from pytest_cleanuptotal.cleanup import Cleanup

def test_should_keep_things_tidy(cleanuptotal:Cleanup):
            # ... your test code here

Logging

To see all the pytest-cleanuptotal logging change the log level to 'DEBUG' (e.g., in your project's pytest.ini)

Support

For any questions or suggestions contact me at: tzur.paldi@outlook.com

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

pytest_cleanuptotal-0.3.4.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

pytest_cleanuptotal-0.3.4-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file pytest_cleanuptotal-0.3.4.tar.gz.

File metadata

  • Download URL: pytest_cleanuptotal-0.3.4.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for pytest_cleanuptotal-0.3.4.tar.gz
Algorithm Hash digest
SHA256 ce42db39f5ab497cf37a3d08aaa05372691670f176a39f85aba581a1e11f7ed2
MD5 8971ab4c1b42ff7422060e6c07d5726a
BLAKE2b-256 d62f171890b7ba79666ded1bf04c9019bac43d71c220f8ee87a3f3dce20f832c

See more details on using hashes here.

File details

Details for the file pytest_cleanuptotal-0.3.4-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_cleanuptotal-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a7ed9e316c3f3f249a33e8ee10018d1e99338e9e15e06c78d471150251eda2e0
MD5 2a126b57f170ce2bca0561ed2da07cd2
BLAKE2b-256 8519e12b49dfe0f20f367a51214db088e77b03f87e5a54a9654c395359842a6f

See more details on using hashes here.

Supported by

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