Skip to main content

Detect if running in a testing environment and control if methods act (either no-op in a test, or only act in a test)

Project description

VW

This module provides decorators that turn on or off the decorated method depending on whether it is running in a testing environment or not.

NOTE: The name vw was chosen for convenience and is not intended as commentary on any political topic real or imagined.

Why?

Have you ever found yourself wanting an easy way to turn off the logging when running in tests?

What about database access?

Mocking works well for your own code, but what about when a user imports your library?

This is what this tool is meant to solve.

Controlling

By using python-decouple, the following variables can be set in a settings.ini file, or a .env file, or as variables in the environment.

  • VW_IGNORE -> causes VW to ignore if it is in a test environment (no_op still acts, and tests_only will not)
  • VW_ALWAYS -> (NOTE: VW_IGNORE takes precedence over this var) causes VW to behave like it is always in a test environment (no_op is always a no-op)

Examples

  1. Using an in-memory db for tests:

    import sqlite3
    
    import vw
    
    _con = sqlite3.connect(":memory:" if vw.is_test_env() else "database.sqlite3")
    
  2. Logging to a local file for tests

    import logging
    
    import vw
    
    _root_logger = logging.getLogger(None)
    _root_logger.addHandler(logging.FileHandler("test.log" if vw.is_test_env() else "//path/to/server/log.txt"))
    
  3. Avoid doing things in unit/component/integration tests that should only be done in production

    import requests
    import vw
    
    @vw.no_op
    def send_tornado_warning():
        requests.post(...)
    

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

import_volkswagen-0.1.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

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

import_volkswagen-0.1.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file import_volkswagen-0.1.1.tar.gz.

File metadata

  • Download URL: import_volkswagen-0.1.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for import_volkswagen-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7aaf4daba43efb92b6c1472b55d9a77458655bd1c0c9ea00a812fd7c3591a613
MD5 892570e677dd849e3fe5796c9a5ffc50
BLAKE2b-256 e1a880fe20d672544258cbd7c7d120f01f32cddfdb9453e9635503c3f799b285

See more details on using hashes here.

Provenance

The following attestation bundles were made for import_volkswagen-0.1.1.tar.gz:

Publisher: Release.yml on mshafer1/py-vw

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file import_volkswagen-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for import_volkswagen-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 13c3af47049b0451aa7b8f8bffcc8ebf904efd273d02d93386ec86958d5f21b2
MD5 8be6a9105f602a1a42e96fb2981cd0ca
BLAKE2b-256 d093993ffa85e5462c9f5455470ed6329b79b88a77ea6133a702613ed14d163e

See more details on using hashes here.

Provenance

The following attestation bundles were made for import_volkswagen-0.1.1-py3-none-any.whl:

Publisher: Release.yml on mshafer1/py-vw

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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