title: In pytest how do I temporarily disable capture in a class method?
body: <p>In pytest (3.04; python 3.4) I'm trying to disable output capture under certain circumstances.  I'm trying to follow the example on <a href="http://doc.pytest.org/en/latest/capture.html" rel="nofollow noreferrer">this doc page</a>.  However, I'm unable to specify capsys as a funcarg.  As a follow-up, I'd like to accept both funcarg and on-funcarg arguments so that I can use a class method that takes one or more normal arguments. How can I do that?  </p>
  
  <p>Ideally, this class method would work:</p>
  
  <pre><code>    def always_print(self, message, capsys):
          with capsys.disabled():
              print(message)
  </code></pre>
  
  <p>But, I can't even get this to work:</p>
  
  <pre><code>def always_print(capsys):
      with capsys.disabled():
          print('FIXME')
  </code></pre>
  
  <p>Getting the error:</p>
  
  <pre><code>...
  &gt;       always_print()
  E       TypeError: always_print() missing 1 required positional argument: 'capsys'
  </code></pre>
  
  <p><strong>Edit 1:</strong>
  Piotr's answer solved my specific issue. However, I also discovered two important caveats that I hadn't picked up in the documentation or other posts, so sharing here for others's benefit: </p>
  
  <ol>
  <li>it appears that capsys.disabled() only applies to stdout and <em>not</em> stderr, which is where I was originally sending my debug messages per *nix best practice.</li>
  <li>If you set a file handle to sys.stdout <em>before</em> calling capsys.disabled(), then due to the magic file discriptor mangling that pytest does, this will not work.</li>
  </ol>
  
  <p>So, for example, you'll need to do it this way (say, if your kwargs may contain an optional "file" keyword, like the built-in print() does:</p>
  
  <pre><code> fhandle = kwargs.get('file', sys.stdout) #will not work!
      with capsys.disabled():
          fhandle = kwargs.get('file', sys.stdout) #must be in context
          print(message, file=fhandle)
  </code></pre>