JamesHutchison / pytest-hot-reloading

A hot reloading pytest daemon, implemented as a plugin
MIT License
86 stars 2 forks source link

--daemon-start-if-needed needs ability to set custom command for starting the daemon #87

Open ddorian opened 4 months ago

ddorian commented 4 months ago

Hi,

I'm using gevent and I start pytest with:

 python -m gevent.monkey --module pytest

Using --daemon-start-if-needed breaks because it starts pytest normally. Thus need a config to set how to auto start the daemon.

JamesHutchison commented 4 months ago

Are you able to solve this using PYTEST_DAEMON_PYTEST_NAME (see the readme on usage)?

I believe with this:

https://pypi.org/project/pytest-gevent/

You can change the value from pytest to pytest-gevent

Its almost midnight for me so I'll check back on this tomorrow morning or afternoon. Thanks!

JamesHutchison commented 4 months ago

Actually looking at the implementation that might not work because it's expecting a module name

args = [
                sys.executable,
                "-m",
                pytest_name,
                "--daemon",
                "--daemon-port",
                str(port),
            ]

But maybe writing a wrapper module would work.

ddorian commented 4 months ago

I believe with this: https://pypi.org/project/pytest-gevent/

That library is deprecated and it says to use my way in their readme https://github.com/asottile-archive/pytest-gevent

Actually looking at the implementation that might not work because it's expecting a module name

Yep it won't work, just tried some different variations.

But maybe writing a wrapper module would work.

That could be it.

JamesHutchison commented 4 months ago

Did the wrapper end up resolving your issue? I'm also curious about your use case because the library doesn't support running tests concurrently (it will turn off xdist via the workaround feature, for example), but maybe you need it for other reasons.

ddorian commented 4 months ago

I wasn't able to work on the wrapper. Use case for this library is just faster test execution locally while working on the codebase.

xdist isn't related to this case, though I'm blocked there too on gevent https://github.com/pytest-dev/pytest-xdist/issues/1069. I'll probably use pytest-split for multi-core running of test suite.

JamesHutchison commented 4 months ago

I don't recommend rerunning your whole test suite. The intended use case is the same as you see in the gif in the readme; you execute single tests or test files from the IDE. You would use the CI to automatically run a test suite for your project and in that case you wouldn't see a benefit from using the hot reloader.

ddorian commented 4 months ago

I agree, I never mentioned xdist, you did here:

I'm also curious about your use case because the library doesn't support running tests concurrently

Maybe you thought (gevent => concurrent-tests)?

JamesHutchison commented 4 months ago

I was just calling it out to maybe save you some time if that was your goal.

Another thing is that sys.executible in that arg list means it reuses the same application that ran it. You could change your executable to something like this (ChatGPT threw this together):

#!/bin/bash

# Store the first argument, which is assumed to be '-m'
module_flag=$1

# Store the second argument, which is the name of the module, e.g., 'pytest'
module_name=$2

# Remove the first two arguments from the list of all arguments
shift 2

# Run the python command with the modified module and the remaining arguments
python -m gevent --module $module_name "$@"

You would then invoke it like this: my_script -m pytest --daemon-start-if-needed <path to test>

The daemon args would become:

            args = [
                "my_script",
                "-m",
                "pytest",
                "--daemon",
                "--daemon-port",
                str(port),
            ]
ddorian commented 4 months ago

That script doesn't work. I'll just use a runfile from the IDE for now, thank you.