Giter Club home page Giter Club logo

pytest-pystack's Introduction

pytest-pystack

CI PyPI - Python Version PyPI PyPI - Downloads Code Style

A pytest plug-in for easy integration of PyStack in your test suite.

It can be used to automatically dump the stack trace of a hanging test in your suite.

See PyStack for further information about the tool.

Installation

To install the PyStack pytest plug-in, just run the following command in your venv:

python -m pip install pytest-pystack

Quick Start

After you have installed the pytest plug-in, you can have PyStack monitor your test suite and output a stack trace if a test takes more than 5 seconds, simply by running pytest with argument --pystack-threshold=5.

Configuration

The PyStack plug-in can be configured via the command line with the following options:

  • --pystack-threshold: Enables the plug-in and monitors all tests, generating a stack trace if they take longer than the specified threshold. Note, this neither stops nor fails the test case after the specified threshold.
  • --pystack-output-file: Appends PyStack output to a file.
  • --pystack-path: Path to the pystack executable.
  • --pystack-args: Additional args to pass to pystack remote <pid>, like --native or --native-all.

And through any pytest config file, see an example of pyproject.toml:

[tool.pytest.ini_options]
pystack_threshold=60
pystack_path="custom-version-of-pystack"
pystack_output_file="./pystack.log"
pystack_args="--native"

License

This project is Apache-2.0 licensed, as found in the LICENSE file.

Code of Conduct

This project has adopted a Code of Conduct. If you have any concerns about the Code, or behavior that you have experienced in the project, please contact us at [email protected].

Contributing

We welcome your contributions to help us improve and extend this project!

Below you will find some basic steps required to be able to contribute to the project. If you have any questions about this process or any other aspect of contributing to a Bloomberg open source project, feel free to send an email to [email protected] and we'll get your questions answered as quickly as we can.

Contribution Licensing

Since this project is distributed under the terms of an open source license, contributions that you make are licensed under the same terms. For us to be able to accept your contributions, we will need explicit confirmation from you that you are able and willing to provide them under these terms, and the mechanism we use to do this is called a Developer's Certificate of Origin (DCO). This is similar to the process used by the Linux kernel, Samba, and many other major open source projects.

To participate under these terms, all that you must do is include a line like the following as the last line of the commit message for each commit in your contribution:

Signed-Off-By: Random J. Developer <[email protected]>

The simplest way to accomplish this is to add -s or --signoff to your git commit command.

You must use your real name (sorry, no pseudonyms, and no anonymous contributions).

Steps

  • Create an Issue, select 'Feature Request', and explain the proposed change.
  • Follow the guidelines in the issue template presented to you.
  • Submit the Issue.
  • Submit a Pull Request and link it to the Issue by including "#" in the Pull Request summary.

pytest-pystack's People

Contributors

godlygeek avatar gusmonod avatar mariocj89 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

gusmonod czaki

pytest-pystack's Issues

RuntimeError: can't create new thread at interpreter shutdown on Python 3.12

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

Exception ignored in atexit callback: <function _stop at 0x7fcf97f30000>
Traceback (most recent call last):
  File "/usr/lib64/python3.12/site-packages/pytest_pystack/_monitor_process.py", line 73, in _stop
    queue.put(None)
  File "/usr/lib64/python3.12/multiprocessing/queues.py", line 94, in put
    self._start_thread()
  File "/usr/lib64/python3.12/multiprocessing/queues.py", line 177, in _start_thread
    self._thread.start()
  File "/usr/lib64/python3.12/threading.py", line 971, in start
    _start_new_thread(self._bootstrap, ())
RuntimeError: can't create new thread at interpreter shutdown

Expected Behavior

No errors.

Steps To Reproduce

Run test suite with pytest-pystack==1.0.0 and pystack_args="--native --locals".

Pystack Version

1.3.0

Python Version

3.12

Linux distribution

Fedora

Anything else?

pytest 7.4.3
pytest-xdist 3.5.0

Can not really provide any project specific details, just tried to update to Python 3.12 and got the errors.

Document how to use in github actions

Users might get the following error in CI:

๐Ÿ’€ Engine error: No such process ๐Ÿ’€

The specified process cannot be traced. This could be because the tracer
has insufficient privileges (the required capability is CAP_SYS_PTRACE).
Unprivileged processes cannot trace processes that they cannot send signals
to or those running set-user-ID/set-group-ID programs, for security reasons.
Alternatively, the process may already be being traced.

If your uid matches the uid of the target process you want to analyze, you
can do one of the following to get 'ptrace' scope permissions:

* If you are running inside a Docker container, you need to make sure you
  start the container using the '--cap-add=SYS_PTRACE' or '--privileged'
  command line arguments. Notice that this may not be enough if you are not
  running as 'root' inside the Docker container as you may need to disable
  hardening (see next points).

* Try running again with elevated permissions by running 'sudo -E !!'.

* You can disable kernel hardening for the current session temporarily (until
  a reboot happens) by running 'echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope'.

Rather than waiting for them to get the error to notice it, document that when running in github actions they should have a step that runs echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope.

We can add this in a section of the README file.

Allow to configure pytest-pystack outside the pytests configuration section

Is your feature request related to a problem? Please describe.

Option to set parameters to pystack in ini file (tool.pytest.ini_options section in pyproject.toml) is really nice. But pytest-pystack currently could be installed only on linux. So if a project is designed to work on multiple operating systems, then configuring pytest-pystack this way collides with --strict-config

It will be nice to be able to use strict config and configure pytest-pystack in file at the same time.

Describe the solution you'd like
A clear and concise description of what you want to happen.

Add option to configure pytest-pystack using own section in pyproject.toml

Describe alternatives you've considered

Play with conditional test configuration in tox/nox file.

pytest-pystack raises exception when using with pytester fixture

Describe the bug

Pytest provides pytester fixture to allow to validate if fixtures provided by a package works correctly:
https://docs.pytest.org/en/7.1.x/reference/reference.html?highlight=pytester#pytester

However, this leads to run test in test and leads pytest-pystack to crash.

Process pystack_monitor:
Traceback (most recent call last):
  File "/home/czaki/.pyenv/versions/3.11.5/lib/python3.11/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/home/czaki/.pyenv/versions/3.11.5/lib/python3.11/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/home/czaki/.pyenv/versions/napari_3.11/lib/python3.11/site-packages/pytest_pystack/_monitor_process.py", line 57, in _run_monitor
    raise Exception(
Exception: new test should not start before previous test finished

This stacktrace comes from https://github.com/napari/napari/blob/cb7f46dfe0100c3afdd622faca768bc393b19636/napari/_tests/test_pytest_plugin.py

To Reproduce
Steps to reproduce the behavior:

  1. Create a test that uses pytester fixture to run tests
  2. Run it in environment with pytest-pystack

Expected behavior

Allow use pytester with pytest-pystack

Screenshots
If applicable, add screenshots to help explain your problem.

Environment (please complete the following information):

  • Operating System and Version: GitHub actions all systems

Hide pytest stack frames

Hide pytest stack frames by default as they are often just noise to the users trying to debug a problem.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.