CodeRef
CodeRef
  • Software
    • VSCode
  • Midjourney_AI
    • Midjourney Cheat Sheet
  • aws
    • S3 Reference
    • Services
  • bash
    • .bashrc File Contents
    • CAN
    • _System Config File Locations
    • argument-length-limits
    • Conditionals
    • Data Structures
    • File Permissions
    • File Syncing
    • File System
    • Functions
    • General
    • Loops
    • My Functions
    • Networking
    • Number Operations
    • OpenVPN
    • Operators
    • Resource Management
    • Serial RS232
    • Spinning Wheel Animation
    • SSH
    • Text Operations
    • Environment Variables
  • cpp
    • ChatGPT | Pointers vs. References
    • arrays
    • Classes
    • Data Types / Structures
    • Enumerated Classes
    • Exception Handling
    • Function Objects
    • Functions
    • I/O
    • Loops
    • Macros
    • Namespaces
    • New Features
    • Pointers
    • Scope
    • Smart Pointers
    • Raw String Literals
    • Style Guide
    • Switch Case
    • Templating
    • How to Use tinyxml2
    • Useful Libraries
    • google-test
    • Conditionals
    • Rule of Three/Five
    • Optional Parameters
    • Keywords
    • Filesystem
    • Random
    • Casting
    • tools
  • git
    • Code Review Dependency Strategy
    • Git Bisect Guide
    • Git Reference
    • removing-cherry-picks
    • Useful Tools
    • Graphite Reference
  • js
    • functions
    • Javascript Reference
  • linux
    • Display
    • Dual Boot with Windows
    • File System
    • NVIDIA
    • Sending/Receiving TCP/UDP Messages in Ubuntu
    • dynamically_linked_binaries
  • markdown
    • Images
    • obsidian-reference
  • python
    • Classes
    • Exceptions
    • Functions
    • Operations
    • Python Reference
    • unittest_command-line-args
    • unittest_magicmock_GPT
    • unittest_mock
    • unittest_printing
    • unittest_subtest
    • useful-stuff
    • jupyter
    • poetry
  • ros
    • _ROS Cheat Sheet
    • Create New Workspace
    • Install ROS
    • Node Sample - Listener
    • Node Sample - Talker
    • Node Template
    • Setup
    • urdf
  • excel
    • excel-reference
  • windows
    • File System
    • WSL - Windows Subsystem for Linux
    • WSL
  • software_engineering
    • uncle_bob_lectures
      • Overview
      • Lesson 01 - Notes
  • web
    • Front End
    • Hugo
    • new_frontend_tools
  • sql
    • cheatsheet
Powered by GitBook
On this page
  1. python

unittest_printing

From Codeium.

By default, the output produced by print() statements in Python unittests may not appear in real-time during the test execution when using Bazel. This behavior is due to the buffering of the standard output.

To ensure that the print() statements are displayed in real-time during the test execution in Bazel, you can disable the output buffering. Here's an example:

import unittest
import sys

class MyTestCase(unittest.TestCase):
	def setUp(self):
		# Disable output buffering
		self.original_stdout = sys.stdout
		sys.stdout = sys.stderr
	
	def tearDown(self):
		# Restore output buffering
		sys.stdout = self.original_stdout
	
	def test_something(self):
		print("This message will be printed to the Bazel test output in real-time")

		self.assertEqual(2 + 2, 4, "Custom failure message that will be printed to the Bazel test output in real-time")

if __name__ == '__main__':
	unittest.main(buffer=False)

In this example, we override the setUp() and tearDown() methods of the unittest.TestCase class to disable and restore the output buffering, respectively. By redirecting sys.stdout to sys.stderr in the setUp() method, the output produced by print() statements will be immediately flushed and displayed in real-time during the test execution.

Additionally, when running the unittest using unittest.main(), you can pass buffer=False to disable the output buffering.

With these modifications, the print() statements will be visible in real-time during the test execution when running the tests using Bazel.

If you pass buffer=False to unittest.main(), it will disable the output buffering by default. In that case, you don't need to perform the setup and teardown steps to override sys.stdout.

Here's an updated example without the setup and teardown steps:

import unittest

class MyTestCase(unittest.TestCase):
	def test_something(self):
		print("This message will be printed to the Bazel test output in real-time")
	
		self.assertEqual(2 + 2, 4, "Custom failure message that will be printed to the Bazel test output in real-time")

if __name__ == '__main__':
	unittest.main(buffer=False)

In this updated example, we remove the setUp() and tearDown() methods. Instead, we directly use unittest.main(buffer=False) to disable the output buffering.

By passing buffer=False to unittest.main(), the print() statements will be immediately flushed and displayed in real-time during the test execution when running the tests using Bazel.

Last updated 11 months ago