Skip to content

capfd in binary mode? #2923

@asottile

Description

@asottile

I'd like something like this to work:

def test_thing(capfd_bytes):
    subprocess.check_call(('echo', '-e', '\\x98\\xfe'))
    out, err = capfd_bytes.readouterr()
    assert out == b'\x98\xfe'

My actual usecase is running tar as stdout and then inspecting the output

I'm hacking around with this (which works, but is undeniably fragile):

def test(capfd):
    ...
    capfd._capture.out.tmpfile.encoding = None
    capfd._capture.err.tmpfile.encoding = None
    out, err = capfd.readouterr()
    capfd._capture.out.tmpfile.encoding = 'UTF-8'
    capfd._capture.err.tmpfile.encoding = 'UTF-8'
    # ...

would pytest be amenable to a PR which enables something like this without hax?

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: capturerelated to the capture builtin plugintype: enhancementnew feature or API change, should be merged into features branch

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions