question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

capfd not working in 3.3.1

See original GitHub issue

Fixtures that use capfd don’t seem to work in version 3.3.1 running on Ubuntu 14.04 LTS with anaconda 3.6.3

import sys

@pytest.fixture()
def fix(capfd):
    yield None
    out, err = capfd.readouterr()
    stdout_filename = "/tmp/stdout"
    stderr_filename = "/tmp/stderr"
    mode = 'w'
    with open(stdout_filename, mode) as stdout:
        stdout.write(out)
    with open(stderr_filename, mode) as stderr:
        stderr.write(err)

def test_a(fix):
    print("lolcatz")
    print("this is a test")
    sys.stderr.write("err")

The stdout and stderr files end up empty after running that test. Using this set of pytest dependencies

certifi (2017.11.5)
pip (9.0.1)
pluggy (0.6.0)
py (1.5.2)
pytest (3.3.1)
setuptools (38.2.4)
six (1.11.0)
wheel (0.30.0)

Steps of replication are

  1. Create the above test
  2. Run pytest on it.
  3. cat /tmp/std* (they will be empty)
  4. Downgrade pytest ( pip install autohil==3.2.5 )
  5. Run the test again
  6. cat /tmp/std*

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
trrichardcommented, Dec 19, 2017

@nicoddemus I found the error.

https://github.com/nicoddemus/pytest/commit/9919269ed048b6e9147ee3301532e3591b9a112b#diff-d8641d58661f0fa63a36c6f6e85715d9R163

It’s a 1 liner. Remove activate_fixture(item) from teardown. It causes a double start which effectively clears the results before they show up in the fixture.

0reactions
nicoddemuscommented, Aug 18, 2018

@AlexanderTyrrell #3830 will also fix that AttributeError: 'tuple' object has no attribute 'out' error. 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use logging, pytest fixture and capsys? - Stack Overflow
Reverse logger, capsys , make logger request the capsys fixture and use capfd do not change anything. I tried pytest-catchlog plugin and it ......
Read more >
pytest Documentation - Read the Docs
an empty directory as the current working directory but otherwise do not care for the concrete directory. Here is how.
Read more >
Changelog — pytest documentation
This solves some issues with tests that involve pytest environment variables. ... CaptureFixture[str] for the capfd and capsys fixtures. pytest.
Read more >
python-pytest-3.3.2-bp150.2.4 - SUSE Package Hub -
(#2644) - Fix error on Windows and Python 3.6+ when sys.stdout has been replaced with a stream-like object which does not implement the...
Read more >
pytest API Manual - Hubwiz.com
... Some Issues and Questions · pytest fixtures: explicit, modular, scalable · pytest-2.3: ... pytest 3.3.2 (2017-12-25) · pytest 3.3.1 (2017-12-05) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found