Package: pytest-hot-reloading
A PyTest Hot Reloading PluginA hot reloading pytest daemon, implemented as a plugin.FeaturesUses thejuriggedlibrary to watch and hot reload filesCaches test discovery in many situationsImproved performance by not having to import libraries again and again and skipping initialization logicSystem for registering workarounds in case something doesn't work out of the boxTrade-offsFirst time imports are slower (measured < 10% to > 100% slower depending on the repo)May not work with some librariesSometimes gets in a bad state and needs to be restartedRequires starting the daemon separately or adding a command line option to automatically start itIf 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-dbto preserve the test database.The minimum Python version is 3.10DemoWith the hot reloading daemonFaster subsequent runsWithout the hot reloading daemonInstallationDo not install in production code. This is exclusively for the developer environment.pip: Addpytest-hot-reloadingto yourdev-requirements.txtfile andpip install -r dev-requirements.txtpoetry:poetry add --group=dev pytest-hot-reloadingUsageAdd 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-neededargument 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/8536Note 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.JetBrains (IDEA, PyCharm, etc)Create a REGULAR Python run configuration, with pytest as themodule. 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.VS CodeThis 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 polling.
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 thatpytest --daemonis NOT how you run tests. It is only used to start
the daemon.The daemon can be stopped withpytest --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"],Arguments and Env VariablesPYTEST_DAEMON_USE_OS_EVENTSInstead of polling the file system, use OS events such as inotify to check for file changes (recommended if your system supports it)Default:FalseCommand line:--daemon-use-os-eventsPYTEST_DAEMON_POLL_THROTTLEA multipler for how aggressive the daemon does file system polling. This is not used if OS events are used.2.0 = twice as slow, less CPU usageDefault:1.0Command line:--daemon-poll-throttlePYTEST_DAEMON_PORTThe port the daemon listens on.Default:4852.Command line:--daemon-portPYTEST_DAEMON_PYTEST_NAMEThe name of the pytest executable. Used for spawning the daemon.Default:pytest.Command line:--pytest-namePYTEST_DAEMON_WATCH_GLOBSThe colon separated globs to watch.Default:./**/*.py.Command line:--daemon-watch-globsPYTEST_DAEMON_IGNORE_WATCH_GLOBSThe colon separated globs to ignore.Default:./.venv/*.Command line:--daemon-ignore-watch-globsPYTEST_DAEMON_START_IF_NEEDEDStart the pytest daemon if it is not running.Default:FalseCommand line:--daemon-start-if-neededPYTEST_DAEMON_DISABLEDisable the pytest plugin for this test run.Default:FalseCommand line:--daemon-disablePYTEST_DAEMON_DO_NOT_AUTOWATCH_FIXTURESDisable automatically watching files containing fixturesDefault:FalseCommand line:--daemon-do-not-autowatch-fixturesWorkaroundsLibraries 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 thepytest_hot_reloading.workaround.register_workarounddecorator. It may optionally yield. If it does,
then code after the yield is executed after the test suite has ran.Example:frompytest_hot_reloading.workaroundimportregister_workaround@register_workaround("my_library")defmy_library_workaround():importmy_libraryyieldmy_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.Need More Speed?If using docker, run everything out of a named volume. Do not use bind mounts (sharing with the host file system)SetPYTEST_DISABLE_PLUGIN_AUTOLOAD=1in the environment to disable automatic search and loading of pluginsNote: You will need to specify explicitly specify the plugins to use if you do this.Example:[tool.pytest.ini_options]asyncio_mode="auto"addopts="-p pytest_asyncio.plugin -p megamock.plugins.pytest -p pytest_hot_reloading.plugin"Run out of a Github Codespace or similar dedicated external environmentPrefer using OS events, if your system works well with it. It uses less CPU and can pick up changes faster. Enable it with the environment variablePYTEST_DAEMON_USE_OS_EVENTS=1. It is only disabled by default for maximum compatibility.Known IssuesThis is alpha, although it's getting closer to where it can be called betaIf you run out of docker, prefer named volumes. Bind mounds and cached file systems can create random issues where changes aren't reflected.The jurigged library is not perfect and sometimes it gets in a bad stateSome libraries were not written with hot reloading in mind, and will not work without some changes.Many systems have conservative inotify limits. Consider bumping this up if you see errors about hitting the inotify limit.Possible command to see current limit:cat /proc/sys/fs/inotify/max_user_instancessudo sysctl fs.inotify.max_user_instances=4096in thepostStartCommandwill for example, help with dev containers. Increase as needed. Consult with ChatGPT if you need assistance with your OS.Use thePYTEST_DAEMON_WATCH_GLOBSenv variable when there are simply too many files.Notespytest-xdist will have its logic disabled, even if args are passed in to enable itpytest-django will not create test database suffixes for multiworker runs such as tox.Extreme example using import perf test from megamock:Without hot reloading: 0.71sWith hot reloading (1st run): 1.79sWith hot reloading (2nd run): 0.00s