title: Testing logging output with pytest
body: <p>I am trying to write a test, using pytest, that would check that a specific function is writing out a warning to the log when needed. For example:</p>
  
  <p>In module.py:</p>
  
  <pre><code>import logging
  LOGGER = logging.getLogger(__name__)
  
  def run_function():
      if something_bad_happens:
          LOGGER.warning('Something bad happened!')
  </code></pre>
  
  <p>In test_module.py:</p>
  
  <pre><code>import logging
  from module import run_function
  
  LOGGER = logging.getLogger(__name__)
  
  def test_func():
      LOGGER.info('Testing now.')
      run_function()
      ~ somehow get the stdout/log of run_function() ~
      assert 'Something bad happened!' in output
  </code></pre>
  
  <p>I have seen that you can supposedly get the log or the stdout/stderr with pytest by passing <code>capsys</code> or <code>caplog</code> as an argument to the test, and then using either <code>capsus.readouterr()</code> or <code>caplog.records</code> to access the output. </p>
  
  <p>However, when I try those methods, I only see "Testing now.", and not "Something bad happened!". It seems like the logging output that is happening within the call to <code>run_function()</code> is not accessible from <code>test_func()</code>? </p>
  
  <p>The same thing happens if I try a more direct method, such as <code>sys.stdout.getvalue()</code>. Which is confusing, because <code>run_function()</code> is writing to the terminal, so I would think that would be accessible from <code>stdout</code>...?</p>
  
  <p>Basically, does anyone know how I can access that 'Something bad happened!' from within <code>test_func()</code>?</p>