Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
pytest-hot-reloading
Advanced tools
A hot reloading pytest daemon, implemented as a plugin.
It was noted that VS Code does not properly detect tests finishing. This is due to buggy experimental behavior that you opt-in to. To opt-out, add to your settings:
"python.experiments.optOutFrom": [
"pythonTestAdapter"
],
If it takes less than 5 seconds to do all of the imports necessary to run a unit test, then you probably don't need this.
If you're using Django, recommended to use --keep-db
to preserve the test database.
The minimum Python version is 3.10
Faster subsequent runs
Do not install in production code. This is exclusively for the developer environment.
pip: Add pytest-hot-reloading
to your dev-requirements.txt
file and pip install -r dev-requirements.txt
poetry: poetry add --group=dev pytest-hot-reloading
Add the plugin to the pytest arguments.
Example using pyproject.toml:
[tool.pytest.ini_options]
addopts = "-p pytest_hot_reloading.plugin"
When running pytest, the plugin will detect whether the daemon is running.
If the daemon is not running, it will error unless the --daemon-start-if-needed
argument is passed.
The current version of the VS Code Python extension is not, by default, compatible with automatically starting the daemon. The test runner will hang. However, you can revert to legacy behavior which will allow for using the automatic starting. See the VS Code section below for more information.
The recommended way to run the daemon is to give it its own run run profile so you can easily use the debugger in tests. As a convenience
you can also, if you're using a dev container, add this to your postStartCommand: pytest --daemon &
. If the daemon doesn't start from your postStartCommand, see: https://github.com/microsoft/vscode-remote-release/issues/8536
Note that a pid file is created to track the pid.
Imports and in many cases initialization logic are not reran on subsequent runs, which can be a huge time saver.
Currently, if you want to debug, you will want to run the daemon manually with debugging.
Create a REGULAR Python run configuration, with pytest as the module. For parameters, add --daemon
. Strongly consider storing
in the project so it is shared with other developers. Note that you most likely also need to set the working directory to the
project root where the pytest configuration is located so that it knows to use the plugin you configured earlier.
For more information on parameters, see the VS Code section below.
This can easily be done in VS Code with the following launch profile:
{
"name": "Pytest Daemon",
"type": "python",
"request": "launch",
"module": "pytest",
"justMyCode": false,
"args": [
"--daemon",
//
// everything below this is optional
//
"--daemon-port",
"4852", // the default value
"--daemon-watch-globs",
"./*.py" // the default value
// "./my-project/*.py:./some-thing-else/*.py", // example of colon separated globs
"--daemon-ignore-watch-globs",
"./.venv/*" // this is the default value, also colon separated globs
]
},
The daemon can be configured to use either file system polling or OS-based file system events. The polling behavior is used by default and has higher compatibility. For example, if you're using Docker for Windows with WSL2, you're going to have a bad time with inotify.
If the daemon is already running and you run pytest with --daemon
, then the old one will be stopped
and a new one will be started. Note that pytest --daemon
is NOT how you run tests. It is only used to start
the daemon.
The daemon can be stopped with pytest --stop-daemon
. This can be used if it gets into a bad state.
To enable automatically starting the server, you have to, currently, disable the new Python Test Adapter:
In your devcontainer.json or user settings:
"python.experiments.optOutFrom": [
"pythonTestAdapter"
],
Then enable automatically starting the daemon in your settings:
"python.testing.pytestArgs": [
"--daemon-start-if-needed",
"tests"
],
PYTEST_DAEMON_USE_OS_EVENTS
False
--daemon-use-os-events
PYTEST_DAEMON_POLL_THROTTLE
1.0
--daemon-poll-throttle
PYTEST_DAEMON_PORT
4852
.--daemon-port
PYTEST_DAEMON_PYTEST_NAME
pytest
.--pytest-name
PYTEST_DAEMON_WATCH_GLOBS
./**/*.py
.--daemon-watch-globs
PYTEST_DAEMON_IGNORE_WATCH_GLOBS
./.venv/*
.--daemon-ignore-watch-globs
PYTEST_DAEMON_START_IF_NEEDED
False
--daemon-start-if-needed
PYTEST_DAEMON_DISABLE
False
--daemon-disable
PYTEST_DAEMON_DO_NOT_AUTOWATCH_FIXTURES
False
--daemon-do-not-autowatch-fixtures
Libraries that use mutated globals may need a workaround to work with this plugin. The preferred route is to have the library update its code to not mutate globals in a test environment, or to restore them after a test suite has ran. In some cases, that isn't possible, usually because the person with the problem doesn't own the library and can't wait around for a fix.
To register a workaround, create a function that is decorated by the
pytest_hot_reloading.workaround.register_workaround
decorator. It may optionally yield. If it does,
then code after the yield is executed after the test suite has ran.
Example:
from pytest_hot_reloading.workaround import register_workaround
@register_workaround("my_library")
def my_library_workaround():
import my_library
yield
my_library.some_global = BackToOriginalValue()
If you are a library author, you can disable any workarounds for your library by creating an empty
module _clear_hot_reload_workarounds.py
. If this is successfully imported, then workarounds for
the given module will not be executed.
PYTEST_DISABLE_PLUGIN_AUTOLOAD=1
in the environment to disable automatic search and loading of plugins
[tool.pytest.ini_options]
asyncio_mode = "auto"
addopts = "-p pytest_asyncio.plugin -p megamock.plugins.pytest -p pytest_hot_reloading.plugin"
PYTEST_DAEMON_USE_OS_EVENTS=1
. It is only disabled by default for maximum compatibility.cat /proc/sys/fs/inotify/max_user_instances
sudo sysctl fs.inotify.max_user_instances=4096
in the postStartCommand
will for example, help with dev containers. Increase as needed. Consult with ChatGPT if you need assistance with your OS.PYTEST_DAEMON_WATCH_GLOBS
env variable when there are simply too many files.FAQs
Unknown package
We found that pytest-hot-reloading demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.