Skip to content

pytest should warn about threads kept alive after testing #3662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fabioz opened this issue Jul 6, 2018 · 9 comments
Closed

pytest should warn about threads kept alive after testing #3662

fabioz opened this issue Jul 6, 2018 · 9 comments
Labels
type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature

Comments

@fabioz
Copy link
Contributor

fabioz commented Jul 6, 2018

Given some code such as:

def test_thread_keeps_process_alive():
    import threading
    import time

    def sleep_forever():
        while True:
            time.sleep(1000)

    threading.Thread(target=sleep_forever).start()

ideally pytest would warn that threads are still running after the test finishes (as the process is still running the ci will fail the build because of a timeout without any signal of what could be the issue).

As a note, the pydev test runner does give such a message, so, that implementation could be borrowed:

https://github.com/fabioz/PyDev.Debugger/blob/master/runfiles.py#L284

@pytestbot
Copy link
Contributor

GitMate.io thinks possibly related issues are #3214 (Pytest hangs when invoked after thread.start()), #1045 (pytest-warnings fails tests since 2.8), #1370 (Print out pytest-warnings?), #253 (pytest should integrate with warnings module), and #3251 (Warnings not captured during test collection).

@RonnyPfannschmidt
Copy link
Member

as pytest itself doesnt support threading, a plugin should handle it

@nicoddemus nicoddemus added the type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature label Jul 7, 2018
@nicoddemus
Copy link
Member

Thanks for the suggestion @fabioz,

I agree with @RonnyPfannschmidt, a plugin looks like the correct place for this functionality.

Should be simple as well, the code from PyDev could go into a pytest_unconfigure hook: at that point all tests and fixtures have finished, so no threads should be alive by then.

Using pytest-cookiecutter will also make it very easy to set a repository up.

I'm closing this, but thanks again for the suggestion, we appreciate it!

@lzy017
Copy link

lzy017 commented Aug 20, 2019

Could you tell me how to use pytest-cookiecutter solve the problem that pytest itself doesnt support threading please, thankyou very much @nicoddemus

@RonnyPfannschmidt
Copy link
Member

@lzy017 is just a template to get started with making a pytest plugin, its not solving/sorting any of the threading related issues, thats something else

@lzy017
Copy link

lzy017 commented Aug 20, 2019

thnaks could you tell me pytest how to support threading without fixture @RonnyPfannschmidt

@RonnyPfannschmidt
Copy link
Member

its not clear to me what you are asking of and i did not investigate the apis needed for debug tooling myself, so you are on your own of you want to tackle this

@nicoddemus
Copy link
Member

Hi @lzy017,

Could you tell me how to use pytest-cookiecutter solve the problem that pytest itself doesnt support threading please, thankyou very much @nicoddemus

My suggestion in #3662 (comment) is about writing a new plugin which would check for alive threads during pytest_unconfigure. pytest-cookiecutter makes creating new plugins a breeze.

@blueyed
Copy link
Contributor

blueyed commented Aug 20, 2019

btw: #5244 is about pytest not exiting if pdb is still active in some thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature
Projects
None yet
Development

No branches or pull requests

6 participants