{"action":"created","issue":{"url":"https://api.github.com/repos/pytest-dev/pytest/issues/2704","repository_url":"https://api.github.com/repos/pytest-dev/pytest","labels_url":"https://api.github.com/repos/pytest-dev/pytest/issues/2704/labels{/name}","comments_url":"https://api.github.com/repos/pytest-dev/pytest/issues/2704/comments","events_url":"https://api.github.com/repos/pytest-dev/pytest/issues/2704/events","html_url":"https://github.com/pytest-dev/pytest/issues/2704","id":251387047,"number":2704,"title":"capsys fixture cannot be used with a session-scoped fixture","user":{"login":"st-pasha","id":4231472,"avatar_url":"https://avatars0.githubusercontent.com/u/4231472?v=4","gravatar_id":"","url":"https://api.github.com/users/st-pasha","html_url":"https://github.com/st-pasha","followers_url":"https://api.github.com/users/st-pasha/followers","following_url":"https://api.github.com/users/st-pasha/following{/other_user}","gists_url":"https://api.github.com/users/st-pasha/gists{/gist_id}","starred_url":"https://api.github.com/users/st-pasha/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/st-pasha/subscriptions","organizations_url":"https://api.github.com/users/st-pasha/orgs","repos_url":"https://api.github.com/users/st-pasha/repos","events_url":"https://api.github.com/users/st-pasha/events{/privacy}","received_events_url":"https://api.github.com/users/st-pasha/received_events","type":"User","site_admin":false},"labels":[{"id":224849195,"url":"https://api.github.com/repos/pytest-dev/pytest/labels/enhancement","name":"enhancement","color":"84b6eb","default":true},{"id":323103543,"url":"https://api.github.com/repos/pytest-dev/pytest/labels/feature-branch","name":"feature-branch","color":"0052cc","default":false},{"id":604021555,"url":"https://api.github.com/repos/pytest-dev/pytest/labels/fixtures","name":"fixtures","color":"5319e7","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2017-08-19T00:26:00Z","updated_at":"2017-08-21T22:53:10Z","closed_at":null,"body":"I have the following use-case: there is a class `Auditor`, which helps gather statistic about certain tests. This class is used via a fixture:\r\n```\r\n@pytest.fixture(scope=\"session\")\r\ndef auditor():\r\n    aud = Auditor()\r\n    yield aud\r\n    aud.display_summary()\r\n```\r\nThe intention being that some tests would use this fixture, report their results to the Auditor singleton, and in the end of the testing session a report table is produced.\r\n\r\nUnfortunately, currently this works only with `-s` flag, because otherwise all output is captured. Ideally I'd like to write the fixture function as \r\n```\r\n@pytest.fixture(scope=\"session\")\r\ndef auditor(capsys):\r\n    aud = Auditor()\r\n    yield aud\r\n    with capsys.disabled():\r\n        aud.display_summary()\r\n```\r\nhowever doing so produces an error that `capsys` is a function-scope capture and cannot be used with a session-scope fixture.\r\n\r\nI believe there should be a way to provide a session-scoped variant of `capsys`, so that it can be used from user-defined session-scoped fixtures."},"comment":{"url":"https://api.github.com/repos/pytest-dev/pytest/issues/comments/323873138","html_url":"https://github.com/pytest-dev/pytest/issues/2704#issuecomment-323873138","issue_url":"https://api.github.com/repos/pytest-dev/pytest/issues/2704","id":323873138,"user":{"login":"st-pasha","id":4231472,"avatar_url":"https://avatars0.githubusercontent.com/u/4231472?v=4","gravatar_id":"","url":"https://api.github.com/users/st-pasha","html_url":"https://github.com/st-pasha","followers_url":"https://api.github.com/users/st-pasha/followers","following_url":"https://api.github.com/users/st-pasha/following{/other_user}","gists_url":"https://api.github.com/users/st-pasha/gists{/gist_id}","starred_url":"https://api.github.com/users/st-pasha/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/st-pasha/subscriptions","organizations_url":"https://api.github.com/users/st-pasha/orgs","repos_url":"https://api.github.com/users/st-pasha/repos","events_url":"https://api.github.com/users/st-pasha/events{/privacy}","received_events_url":"https://api.github.com/users/st-pasha/received_events","type":"User","site_admin":false},"created_at":"2017-08-21T22:53:10Z","updated_at":"2017-08-21T22:53:10Z","body":"I found that the following hack does the trick:\r\n\r\n```\r\n@pytest.fixture(scope=\"session\")\r\ndef auditor():\r\n    aud = Auditor()\r\n    yield aud\r\n    # The property `aud.capsys` is assigned in the test below\r\n    with aud.capsys.disabled():\r\n        aud.display_summary()\r\n\r\ndef test_fake(capsys, auditor):\r\n    # This test doesn't test anything, but merely saves `capsys` fixture\r\n    # as a property of the auditor.\r\n    auditor.capsys = capsys\r\n```\r\n\r\nOf course, there is still the problem that `test_fake` may be skipped (if the user wants to run `pytest` on a subset of tests only)."}}
{"actor":{"display_login":"st-pasha"}}