A pytest plugin for test case-level dynamic dependency management
Project description
pytest-casewise-package-install
English
A pytest plugin for test case-level dynamic dependency management. Solves dependency conflicts between multiple test cases by automatically installing required package versions before test execution and cleaning up the environment afterward, ensuring isolation and consistency.
Features
- ?? Test Case-Level Package Management: Specify different package versions for each test case
- ?? Dependency Isolation: Avoid conflicts between tests requiring different package versions
- ?? Automatic Installation: Packages are installed automatically before test execution
- ?? Automatic Cleanup: Environment is restored after each test
- ?? Smart Caching: Package installations are cached and reused across tests
- ?? Multiple Usage Patterns: Decorator or marker-based syntax
Installation
pip install pytest-casewise-package-install
Quick Start
Using Decorator
from pytest_casewise_package_install import with_packages
@with_packages({"requests": "2.31.0"})
def test_with_requests():
import requests
assert requests.__version__ == "2.31.0"
Using Pytest Marker
import pytest
@pytest.mark.with_packages(packages={"requests": "2.28.0"})
def test_with_marker():
import requests
assert requests.__version__ == "2.28.0"
Multiple Packages
from pytest_casewise_package_install import with_packages
@with_packages({
"numpy": "1.26.4",
"pandas": "2.0.0"
})
def test_multiple_packages():
import numpy
import pandas
assert numpy.__version__ == "1.26.4"
assert pandas.__version__ == "2.0.0"
Configuration
You can configure the plugin behavior in pytest.ini or setup.cfg:
[pytest]
# custom cache directory (optional)
casewise_cache_dir = ~/.my_custom_cache
# automatically cleanup environment after each test (default: true)
casewise_auto_cleanup = true
# package installation timeout in seconds (default: 300)
casewise_install_timeout = 300
# verbose output (default: false)
casewise_verbose = true
Or use environment variables:
export CASEWISE_CACHE_DIR=~/.my_custom_cache
export CASEWISE_AUTO_CLEANUP=true
export CASEWISE_INSTALL_TIMEOUT=300
export CASEWISE_VERBOSE=true
How It Works
- Test Discovery: The plugin hooks into pytest's test collection phase
- Package Detection: Detects package requirements from decorators or markers
- Environment Setup: Before test execution:
- Creates isolated installation directory for required packages
- Installs packages using
pip install --target - Modifies
PYTHONPATHandsys.pathto use the isolated packages
- Test Execution: Test runs with the specified package versions
- Cleanup: After test execution:
- Restores original
PYTHONPATHandsys.path - Keeps installed packages cached for future tests
- Restores original
Advanced Usage
Test Class with Different Versions
from pytest_casewise_package_install import with_packages
class TestPackageVersions:
@with_packages({"click": "8.1.3"})
def test_click_8_1_3(self):
import click
assert click.__version__ == "8.1.3"
@with_packages({"click": "8.0.0"})
def test_click_8_0_0(self):
import click
assert click.__version__ == "8.0.0"
Reusing Package Sets
The plugin automatically caches package installations. If two tests require the same package set, the second test will reuse the first test's installation:
from pytest_casewise_package_install import with_packages
@with_packages({"requests": "2.31.0"})
def test_one():
import requests
assert requests.__version__ == "2.31.0"
@with_packages({"requests": "2.31.0"})
def test_two():
# reuses the installation from test_one
import requests
assert requests.__version__ == "2.31.0"
Use Cases
This plugin is particularly useful when:
- Testing compatibility with multiple versions of a dependency
- Running tests that require conflicting package versions
- Testing migration paths between package versions
- Ensuring test isolation in CI/CD pipelines
- Testing against different versions of transitive dependencies
Limitations
- Package installation happens during test setup, which may increase test duration
- Large packages can consume significant disk space in the cache directory
- Some packages with complex native dependencies may not work correctly with
--targetinstallation - Import-time side effects in packages may not be fully isolated
Development
Setup Development Environment
git clone https://github.com/yourusername/pytest-casewise-package-install
cd pytest-casewise-package-install
pip install -e .
Run Tests
pytest tests/
Run Examples
cd examples
pytest -v
License
MIT License
??
????????????????? pytest ?????????????????????????????????????????????????????????
????
- ?? ????????: ???????????????
- ?? ????: ??????????????????
- ?? ????: ???????????
- ?? ????: ?????????
- ?? ????: ?????????????
- ?? ??????: ???????????
??
pip install pytest-casewise-package-install
????
?????
from pytest_casewise_package_install import with_packages
@with_packages({"requests": "2.31.0"})
def test_with_requests():
import requests
assert requests.__version__ == "2.31.0"
?? Pytest Marker
import pytest
@pytest.mark.with_packages(packages={"requests": "2.28.0"})
def test_with_marker():
import requests
assert requests.__version__ == "2.28.0"
???
from pytest_casewise_package_install import with_packages
@with_packages({
"numpy": "1.26.4",
"pandas": "2.0.0"
})
def test_multiple_packages():
import numpy
import pandas
assert numpy.__version__ == "1.26.4"
assert pandas.__version__ == "2.0.0"
??
??? pytest.ini ? setup.cfg ????????
[pytest]
# ???????????
casewise_cache_dir = ~/.my_custom_cache
# ??????????????: true?
casewise_auto_cleanup = true
# ????????????: 300?
casewise_install_timeout = 300
# ???????: false?
casewise_verbose = true
????????
export CASEWISE_CACHE_DIR=~/.my_custom_cache
export CASEWISE_AUTO_CLEANUP=true
export CASEWISE_INSTALL_TIMEOUT=300
export CASEWISE_VERBOSE=true
????
- ????: ???? pytest ???????
- ???: ?????????????
- ????: ???????
- ??????????????
- ??
pip install --target??? - ??
PYTHONPATH?sys.path???????
- ????: ????????????
- ??: ??????
- ?????
PYTHONPATH?sys.path - ??????????????????
- ?????
????
??????????
from pytest_casewise_package_install import with_packages
class TestPackageVersions:
@with_packages({"click": "8.1.3"})
def test_click_8_1_3(self):
import click
assert click.__version__ == "8.1.3"
@with_packages({"click": "8.0.0"})
def test_click_8_0_0(self):
import click
assert click.__version__ == "8.0.0"
?????
???????????????????????????????????????????
from pytest_casewise_package_install import with_packages
@with_packages({"requests": "2.31.0"})
def test_one():
import requests
assert requests.__version__ == "2.31.0"
@with_packages({"requests": "2.31.0"})
def test_two():
# ???? test_one ???
import requests
assert requests.__version__ == "2.31.0"
????
??????????????
- ??????????????
- ????????????
- ????????????
- ? CI/CD ?????????
- ???????????
??
- ????????????????????????
- ????????????????????
- ????????????????????
--target?? - ?????????????????
??
??????
git clone https://github.com/yourusername/pytest-casewise-package-install
cd pytest-casewise-package-install
pip install -e .
????
pytest tests/
????
cd examples
pytest -v
???
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_casewise_package_install-0.1.0.tar.gz.
File metadata
- Download URL: pytest_casewise_package_install-0.1.0.tar.gz
- Upload date:
- Size: 23.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c466406c5a2cd1138aab436a215a27bebea04adc2389bae79953110545cc556
|
|
| MD5 |
ea74654d6029828c8f6124a5c4dfa5c6
|
|
| BLAKE2b-256 |
869b4d3cd196f61a569a77c578ffeac9270d494e2a68ae6008c4228396bb1fc3
|
File details
Details for the file pytest_casewise_package_install-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pytest_casewise_package_install-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3957644db98c614a38f4ac535b3d1d4282069fa83433a5654181b6345ec6e409
|
|
| MD5 |
6ef17c9df135a4ece13da93b1e6c6ab1
|
|
| BLAKE2b-256 |
ca8a2f8a309bcd763f182ca1f8931ef4421ee8ea9f8a17f25ef3ad830e98672b
|