{"id":17912379,"url":"https://github.com/seberg/pytest-valgrind","last_synced_at":"2026-03-04T05:02:48.256Z","repository":{"id":66721921,"uuid":"164952948","full_name":"seberg/pytest-valgrind","owner":"seberg","description":"Pytest plugin reporting valgrind errors as test failures","archived":false,"fork":false,"pushed_at":"2024-05-29T17:44:26.000Z","size":32,"stargazers_count":9,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-26T09:18:04.324Z","etag":null,"topics":["memory-leaks","pytest-plugin","valgrind"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/seberg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-09T23:13:35.000Z","updated_at":"2025-01-21T23:35:41.000Z","dependencies_parsed_at":"2024-10-28T19:45:12.801Z","dependency_job_id":"55f06e36-9787-46d0-b551-7554d81571bb","html_url":"https://github.com/seberg/pytest-valgrind","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/seberg/pytest-valgrind","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seberg%2Fpytest-valgrind","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seberg%2Fpytest-valgrind/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seberg%2Fpytest-valgrind/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seberg%2Fpytest-valgrind/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/seberg","download_url":"https://codeload.github.com/seberg/pytest-valgrind/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/seberg%2Fpytest-valgrind/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30071898,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-04T03:25:38.285Z","status":"ssl_error","status_checked_at":"2026-03-04T03:25:05.086Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["memory-leaks","pytest-plugin","valgrind"],"created_at":"2024-10-28T19:44:46.427Z","updated_at":"2026-03-04T05:02:48.230Z","avatar_url":"https://github.com/seberg.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"Valgrind testing helper for pytest\n==================================\n\nThis is much of a hack, but it can help you test a C extension module with\nvalgrind. In particular if you are interested in checking for memory leaks.\n\nWhen might I want to use this?:\n  * You have a non-trivial C-extention and should really check it with valgrind.\n  * You have a non-trivial C-extention module that does memory allocations\n    and might leak memory.\n  * You are interested not only in reference count leaks (for which other tools\n    are probably be better). Or hope valgrind can give you some information on the leak.\n  * You are looking for a simple way to get an overview over which tests cause\n    errors detected by valgrind.\n\nWhy not just run the test in valgrind?:\n  * Memory leak checking on valgrind is normally done only at exit. This\n    will run a leak check after every test allowing you to narrow down where\n    a leak occurs without additional effort.\n  * This reports which test fails valgrind and the exact error associated\n    with it. So it may be a bit more convenient\n\nWhy should I not use this?:\n  * It is a a hack with you passing in the valgrind log file to pytest\n    (if you want a helpful error summary).\n  * The error reporting probably has some quirks and hides normal errors.\n  * You want to inspect the full valgrind information and this way you are\n    more likely to miss errors that only show up between tests (e.g. module\n    import/teardown).\n\n**Testing for memory leaks after every test seems to be a bit flaky and\nincreadibly slow. Also I do not know the details well enough to be sure that\nthere are no other issues.**\n\n\nHow to use the plugin\n---------------------\n\nTo use this module, you need to first install it using `pip install .` or\n`pip install . --user`. It currently only supports Python 3 and requires\na C compiler as well as typical valgrind installation (`valgrind/valgrind.h`).\n\nTo then use it, use a normal pytest invocation giving the `--valgrind` option,\nhowever, you also need to run everything in valgrind itself.\n\nThe easiest way to do this is (requires python 3.6 I believe) is the sample\ninvocation below (or similar pytest command). There is an example test in the\n`example` subfolder, which includes a similar invocation as documentation:\n```\nPYTHONMALLOC=malloc valgrind --show-leak-kinds=definite --log-file=/tmp/valgrind-output \\\n    python -m pytest -vv --valgrind --valgrind-log=/tmp/valgrind-output\n```\n\nNote that the `PYTHONMALLOC=malloc` command is crucial (and only works on newer\npython versions). Alternatively, a python debug build with the `--valgrind`\noption can be used. If neither is used, the output will be useless due to\nfalse positives by python (suppression could be added, but are less reliable).\n*It is the responsibility of the caller to pass the correct valgrind arguments.\nyou must pass `--show-leak-kinds=definite` and should use `--log-file` as above!*\n\nYou should provide a log file in this invocation and pass it into pytest. Otherwise\n`pytest-valgrind` will not be able to provide a nice error report. Any normal failures\nwill be skipped. For example in numpy, typically the floating point errors\nfail to report, which causes test failures.\n\nThe first version effectively ignored the ``--errors-for-leak-kinds`` valgrind\noption. In the current version ``--errors-for-leak-kinds=definite`` will work\n(and ignore \"indirect\" leaks), but you must still pass\n``--show-leak-kinds=definite`` it appears.\n\nAny valgrind error or memory leak occuring *during* the test will lead to the\ntest being recorded as *FAILURE*. You will have to search the valgrind log\nfile for the specific error.\n\nWhen running this on a module it makes sense to compile with debug symbols\nusually a ``-g`` option.\nFor example to run on NumPy, it is also necessary to use:\n``--continue-on-collection-errors`` since some tests fail during collection\notherwise.  (This is due to missing support for longdoubles or floating point\nerrors.)\n\n\n### Options\n\n* `--valgrind` enables the plugin.\n* `--valgrind-log=\u003clog_file\u003e` Should be given. This is the same file passed to\n  valgrind as `--log-file=\u003clog_file\u003e`. If not given, the error reports do not\n  include any valgrind output.\n* `--no-memcheck` will disable checking for memory leaks after every function\n  call (or actually at all). If you are sure there are no leaks, this might\n  speed up execution.\n* `--memcheck-before-func` will run a memory check before each test call. This\n  should not be necessary, so maybe should be removed again.\n\n\nReported failures and marking tests\n-----------------------------------\n\nThis plugin ignores all normal exceptions and replaces them with `KNOWNFAIL`/`xfail`\nright now. It will report failures only for errors/leaks reported by valgrind.\nIt seems that known failures that produce valgrind errors are also reported as known failure.\n\nYou can mark tests with `pytest.mark.valgrind_known_leak(reason=\"?!\")`\nor `pytest.mark.valgrind_known_error(reason=\"Oooops\")` (or both) to make the test result\nan `xfail` specifically for this plugin and specific to either leaks or other errors\nreported by valgrind.\nThese are, however, currently not defined by the plugin (not sure if they should).\n\nNot all errors are necessarily due to your own code, sometimes false positives can be reported\nfrom known good functions. For example `strcmp` can do this if the valgrind suppressions are not\nup to date. Such failures should be fixed with a valgrind suppression file and not using\npytest markers.\n\n\nNotes, Caveats, and Solutions\n-----------------------------\n\nPlease check the valgrind documentation if you wish to modify your output.\nValgrind starts hiding duplicate errors, so if is highlighted as an error\nbut there is no error in the valgrind output, it might be that the error\nis a repetition of a previous one and thus hidden.\n\nFurter notes:\n\n  * If valgrind has bad interaction causing errors during test gathering\n    this may hang pytest. In that case, you may use\n    `--continue-on-collection-errors` as a quick workaround.\n  * CPython always causes \"possible\" leaks to implement the garbage\n    collector/circular reference counting.\n    Due to this, we perform a sanity check: if ``obj = object()``\n    reports a \"leak\" valgrinds ``--errors-for-leak-kinds`` is ignored (the\n    default includes \"possible\"). ``--errors-for-leak-kinds=definite`` will\n    not be ignored, and possibly there are other ways to make the above\n    check pass.\n  * Testing leaks this often slows down testing even more compared to a\n    simple run with valgrind.\n  * It does not seem too elegant, since a valgrind output file is passed\n    to the pytest as an argument (I doubt there is a solution for this).\n  * If you do not have useful function names in your output maybe you did\n    not build a debug build?\n  * Valgrind has lots of options, please check them!\n  * No, I did not check this on different systems/compilers. So if it\n    breaks, you may have to modify the code or setup.py.\n  * By default checks for leaks once before the first test and once after\n    every test. This means:\n       - Leaks occuring before any test is run are not reported!\n       - Leaks should not really occur between tests, but if they do they\n         are reported for the next test.\n  * If your program has caches (e.g. numpy has a small array cache) this might\n    cause leaks to behave non-deterministic, or the object that is being leaked\n    not correspond to the actual leaked object (since the allocation occured\n    originally for another object).\n  * The tool runs the garbage collector repeatedly after every single test,\n    this may be very slow.\n  * I do not know pytest plugins well (and the documentation is not super\n    easy at the time), so a lot of how the pytest things are done can and\n    should probably be much done better.\n\n\nI do not like this or have a better version!\n--------------------------------------------\n\nSure, feel free to create pull requests, if you have something better I will\nbe happy to remove/rename this.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseberg%2Fpytest-valgrind","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fseberg%2Fpytest-valgrind","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fseberg%2Fpytest-valgrind/lists"}