Create LogCaptureHandler if necessary (closes #6240)
This commit is contained in:
		
							parent
							
								
									6df0b9c41a
								
							
						
					
					
						commit
						4dfc461036
					
				
							
								
								
									
										1
									
								
								AUTHORS
								
								
								
								
							
							
						
						
									
										1
									
								
								AUTHORS
								
								
								
								
							|  | @ -99,6 +99,7 @@ Erik M. Bray | |||
| Evan Kepner | ||||
| Fabien Zarifian | ||||
| Fabio Zadrozny | ||||
| Felix Nieuwenhuizen | ||||
| Feng Ma | ||||
| Florian Bruhin | ||||
| Floris Bruynooghe | ||||
|  |  | |||
|  | @ -0,0 +1,2 @@ | |||
| Fixes an issue where logging during collection step caused duplication of log | ||||
| messages to stderr. | ||||
|  | @ -613,6 +613,12 @@ class LoggingPlugin: | |||
|                 with catching_logs(self.log_file_handler, level=self.log_file_level): | ||||
|                     yield | ||||
|             else: | ||||
|                 # Add a dummy handler to ensure logging.root.handlers is not empty. | ||||
|                 # If it were empty, then a `logging.warning()` call (and similar) during collection | ||||
|                 # would trigger a `logging.basicConfig()` call, which would add a `StreamHandler` | ||||
|                 # handler, which would cause all subsequent logs which reach the root to be also | ||||
|                 # printed to stdout, which we don't want (issue #6240). | ||||
|                 with catching_logs(logging.NullHandler()): | ||||
|                     yield | ||||
| 
 | ||||
|     @contextmanager | ||||
|  |  | |||
|  | @ -1493,3 +1493,32 @@ def test__get_multicapture() -> None: | |||
|     pytest.raises(ValueError, _get_multicapture, "unknown").match( | ||||
|         r"^unknown capturing method: 'unknown'" | ||||
|     ) | ||||
| 
 | ||||
| 
 | ||||
| def test_logging_while_collecting(testdir): | ||||
|     """Issue #6240: Calls to logging.xxx() during collection causes all logging calls to be duplicated to stderr""" | ||||
|     p = testdir.makepyfile( | ||||
|         """\ | ||||
|         import logging | ||||
| 
 | ||||
|         logging.warning("during collection") | ||||
| 
 | ||||
|         def test_logging(): | ||||
|             logging.warning("during call") | ||||
|             assert False | ||||
|         """ | ||||
|     ) | ||||
|     result = testdir.runpytest_subprocess(p) | ||||
|     assert result.ret == ExitCode.TESTS_FAILED | ||||
|     result.stdout.fnmatch_lines( | ||||
|         [ | ||||
|             "*test_*.py F*", | ||||
|             "====* FAILURES *====", | ||||
|             "____*____", | ||||
|             "*--- Captured log call*", | ||||
|             "WARNING * during call", | ||||
|             "*1 failed*", | ||||
|         ] | ||||
|     ) | ||||
|     result.stdout.no_fnmatch_line("*Captured stderr call*") | ||||
|     result.stdout.no_fnmatch_line("*during collection*") | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue