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.2.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.2-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: import_volkswagen-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 786fc3936c65112f9ee9cb6eee9e08596191f986bc8b1f23f3ec42f1a5399238
MD5 b5eacbcd0dbb28c5af6212d3ace56b0b
BLAKE2b-256 4dfad0d05648705a41c6620c0d7b110a9291627b30a2709907251399703ef97a

See more details on using hashes here.

Provenance

The following attestation bundles were made for import_volkswagen-0.1.2.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.2-py3-none-any.whl.

File metadata

File hashes

Hashes for import_volkswagen-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f661d294fc85f833bbcc824b2d03b1157bc149cc20fddcac234cce59c3d7b94c
MD5 142062dbd0279b037d42dbb2e04d0b99
BLAKE2b-256 127eff072f94573e5f6e7d416406b38cde1a4b0968882a916a715953e492057a

See more details on using hashes here.

Provenance

The following attestation bundles were made for import_volkswagen-0.1.2-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