Skip to main content

PyPI version Python Development Status Maintenance PyPI License


💡 pytest-inject-config

A pytest plugin that loads a config file given by --config-path and injects it into every test class as self.config. The format is chosen by the file extension: JSON (.json) and YAML (.yaml, .yml). Run the same test suite against different environments by switching a single flag.


📦 Installation

uv add pytest-inject-config

🚀 Features

  • ✅ One flag, any environment — point --config-path at a different JSON file per environment
  • ✅ Isolated per class — each test class gets its own copy, so a test that mutates its config can't affect others
  • ✅ Fails fast — a missing file, an unsupported extension, or invalid content stops the run at startup with a clear usage error
  • ✅ JSON and YAML — chosen by extension; only known extensions are accepted, so adding formats later never changes how an existing file is read
  • ✅ Safe YAML — YAML is loaded with safe_load, so a config file can never construct Python objects or run code
  • ✅ Read once — the file is loaded at startup, not once per class
  • ✅ Zero footprint without the flag — nothing is registered and no fixture runs

🛠️ How to Use

  1. Create a config file per environment, in JSON:
    {
        "host": "10.0.0.1",
        "port": 8080
    }
    
    or in YAML:
    host: 10.0.0.1
    port: 8080
    
  2. Read it from your test classes through self.config:
    class TestDevice:
        def test_connect(self):
            assert connect(self.config["host"], self.config["port"])
    
  3. Run with the file for the environment you want:
    pytest --config-path=config/staging.json
    

⚙️ Behavior

Situation Result
Test inside a class self.config holds the file's content as a dict
Top level is not a mapping (a list, a number, a string) UsageError at startup
Module-level test function Unaffected
Relative path Resolved from the directory pytest is run in
Extension Decides the format, case-insensitive. Supported: .json, .yaml, .yml
Unsupported or missing extension UsageError at startup (exit code 4), even if the content would parse
Empty file, or a top-level null UsageError at startup
YAML with several documents (---) or Python tags (!!python/...) UsageError at startup
Missing file, directory, non-UTF-8 or invalid content UsageError at startup (exit code 4), no tests run
No --config-path Nothing is injected; no fixture runs

▶️ Usage Examples

Select the environment in CI

pytest --config-path="config/${TARGET_ENV}.yaml"

Set a default in pytest.ini

[pytest]
addopts =
    --config-path=config/local.json

🔁 Migrating from pytest-plugins

This plugin was previously add_config_parameters in pytest-plugins, where it never worked. Both register the --config-path option, so don't install them together until you're on a pytest-plugins release without it.


🤝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/my-addition
  3. Make your changes
  4. Commit your changes: git commit -m 'Add my addition'
  5. Push to the branch: git push origin feature/my-addition
  6. Open a Pull Request

Additions that promote clean, productive, and maintainable development are always welcome.


📄 License

MIT License — see LICENSE for details.


🙏 Thanks

Thanks for exploring this repository!
Happy coding!

GitHub   PyPI   Blog   LinkedIn

Metadata

Release files for pytest-inject-config 1.0.0

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

Source distribution (sdist)

Source distribution for pytest-inject-config 1.0.0
File Size Uploaded Provenance Actions
pytest_inject_config-1.0.0.tar.gz 6.4 kB Without provenance Details for pytest_inject_config-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytest-inject-config 1.0.0
File Interpreter ABI Platform Actions
pytest_inject_config-1.0.0-py3-none-any.whl Python 3 none any Details for pytest_inject_config-1.0.0-py3-none-any.whl

Total release size: 14.0 kB

Release files / pytest_inject_config-1.0.0.tar.gz

Download URL pytest_inject_config-1.0.0.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b2945f9737a4d32bf5f86f39653ca3a1600938c0e12b7fe2ac88be69820f9cd9
BLAKE2b-256 checksum
How to use checksums
5ac2c3b62cb2a784a880c1250e17fd2309f9028556568e92defbf861c167c997
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / pytest_inject_config-1.0.0-py3-none-any.whl

Download URL pytest_inject_config-1.0.0-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
91b2666bf4ce5e833b1a3bdb2889dab75f71db16169dc943d4ecb6f3d31bb3c6
BLAKE2b-256 checksum
How to use checksums
ba05017513045df2bc4c554d4f252523b5dd4a29512cbd29edc3c85cba5e43f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for pytest-inject-config 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

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