{"id":15542012,"url":"https://github.com/longlho/pytest-cache","last_synced_at":"2025-07-12T02:35:56.307Z","repository":{"id":8760191,"uuid":"10442508","full_name":"longlho/pytest-cache","owner":"longlho","description":"Mirror of https://bitbucket.org/hpk42/pytest-cache and improvements","archived":false,"fork":false,"pushed_at":"2013-06-02T22:12:25.000Z","size":188,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-03T12:23:47.454Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/longlho.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-06-02T21:30:31.000Z","updated_at":"2013-10-22T16:13:25.000Z","dependencies_parsed_at":"2022-09-08T23:50:25.716Z","dependency_job_id":null,"html_url":"https://github.com/longlho/pytest-cache","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/longlho%2Fpytest-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/longlho%2Fpytest-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/longlho%2Fpytest-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/longlho%2Fpytest-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/longlho","download_url":"https://codeload.github.com/longlho/pytest-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243239974,"owners_count":20259333,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-10-02T12:20:24.806Z","updated_at":"2025-03-12T15:23:30.197Z","avatar_url":"https://github.com/longlho.png","language":"Python","readme":"pytest-cache: working with cross-testrun state\n=====================================================\n\n[![Build Status](https://secure.travis-ci.org/longlho/pytest-cache.png?branch=master)](http://travis-ci.org/longlho/pytest-cache)\n\nUsage\n---------\n\ninstall via::\n\n    pip install pytest-cache\n\nafter which other plugins can access a new `config.cache`_ object \nwhich helps sharing values between ``py.test`` invocations.\n\nThe plugin also introduces a new ``--lf`` option to rerun the \nlast failing tests and a ``--clearcache`` option to remove \ncache contents ahead of a test run.\n\n\nThe new --lf (rerun last failing) option\n------------------------------------------\n\nThe cache plugin introduces the ``--lf`` option to py.test which\nalows to rerun all test failures of a previous test run.  \nIf no tests failed previously, all tests will be run as normal.  \nIt is thus usually fine to always pass ``--lf``.\n\nAs an example, let's create 50 test invocation of which\nonly 2 fail::\n\n    # content of test_50.py\n    import pytest\n\n    @pytest.mark.parametrize(\"i\", range(50))\n    def test_num(i):\n        if i in (17,25):\n           pytest.fail(\"bad luck\") \n\nIf you run this for the first time you will see two failures::\n\n    $ py.test -q\n    collecting ... collected 50 items\n    .................F.......F........................\n    ================================= FAILURES =================================\n    _______________________________ test_num[17] _______________________________\n    \n    i = 17\n    \n        @pytest.mark.parametrize(\"i\", range(50))\n        def test_num(i):\n            if i in (17,25):\n    \u003e          pytest.fail(\"bad luck\")\n    E          Failed: bad luck\n    \n    test_50.py:6: Failed\n    _______________________________ test_num[25] _______________________________\n    \n    i = 25\n    \n        @pytest.mark.parametrize(\"i\", range(50))\n        def test_num(i):\n            if i in (17,25):\n    \u003e          pytest.fail(\"bad luck\")\n    E          Failed: bad luck\n    \n    test_50.py:6: Failed\n    2 failed, 48 passed in 0.06 seconds\n\nIf you then run it with ``--lf`` you will re-run the last two failures::\n\n    $ py.test -q --lf\n    collecting ... collected 50 items\n    FF\n    ================================= FAILURES =================================\n    _______________________________ test_num[17] _______________________________\n    \n    i = 17\n    \n        @pytest.mark.parametrize(\"i\", range(50))\n        def test_num(i):\n            if i in (17,25):\n    \u003e          pytest.fail(\"bad luck\")\n    E          Failed: bad luck\n    \n    test_50.py:6: Failed\n    _______________________________ test_num[25] _______________________________\n    \n    i = 25\n    \n        @pytest.mark.parametrize(\"i\", range(50))\n        def test_num(i):\n            if i in (17,25):\n    \u003e          pytest.fail(\"bad luck\")\n    E          Failed: bad luck\n    \n    test_50.py:6: Failed\n    ======================== 48 tests deselected by '' =========================\n    2 failed, 48 deselected in 0.01 seconds\n\nThe last line indicates that 48 tests have not been run.\n\n.. _`config.cache`:\n\nThe new config.cache object\n--------------------------------\n\n.. regendoc:wipe\n\nPlugins or conftest.py support code can get a cached value \nusing the pytest ``config`` object.  Here is a basic example\nplugin which implements a `funcarg \u003chttp://pytest.org/latest/funcargs.html\u003e`_\nwhich re-uses previously created state across py.test invocations::\n\n    # content of test_caching.py\n    import time\n\n    def pytest_funcarg__mydata(request):\n        val = request.config.cache.get(\"example/value\", None)\n        if val is None:\n            time.sleep(9*0.6) # expensive computation :)\n            val = 42\n            request.config.cache.set(\"example/value\", val)\n        return val \n\n    def test_function(mydata):\n        assert mydata == 23\n\nIf you run this command once, it will take a while because\nof the sleep::\n\n    $ py.test -q\n    collecting ... collected 1 items\n    F\n    ================================= FAILURES =================================\n    ______________________________ test_function _______________________________\n    \n    mydata = 42\n    \n        def test_function(mydata):\n    \u003e       assert mydata == 23\n    E       assert 42 == 23\n    \n    test_caching.py:12: AssertionError\n    1 failed in 5.43 seconds\n\nIf you run it a second time the value will be retrieved from\nthe cache and this will be quick::\n\n    $ py.test -q\n    collecting ... collected 1 items\n    F\n    ================================= FAILURES =================================\n    ______________________________ test_function _______________________________\n    \n    mydata = 42\n    \n        def test_function(mydata):\n    \u003e       assert mydata == 23\n    E       assert 42 == 23\n    \n    test_caching.py:12: AssertionError\n    1 failed in 0.02 seconds\n\nConsult the `pytest-cache API \u003chttp://packages.python.org/pytest-cache/api.html\u003e`_\nfor more details.\n\n\nInspecting Cache content\n-------------------------------\n\nYou can always peek at the content of the cache using the\n``--cache`` command line option::\n\n    $ py.test --cache\n    =========================== test session starts ============================\n    platform linux2 -- Python 2.7.3 -- pytest-2.2.5.dev2\n    cachedir: /home/hpk/tmp/doc-exec-257/.cache\n    ------------------------------- cache values -------------------------------\n    cache/lastfailed contains:\n      set(['test_caching.py::test_function'])\n    example/value contains:\n      42\n    \n    =============================  in 0.01 seconds =============================\n\nClearing Cache content\n-------------------------------\n\nYou can instruct pytest to clear all cache files and values \nby adding the ``--clearcache`` option like this::\n\n    py.test --clearcache\n\nThis is recommended for invocations from Continuous Integration\nservers where isolation and correctness is more important\nthan speed.\n\nNotes\n-------------\n\nmore info on py.test: http://pytest.org\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flonglho%2Fpytest-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flonglho%2Fpytest-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flonglho%2Fpytest-cache/lists"}