How can I change the color of the file name and line number in pytest output?

I have the above output from pytest to make it easier to find info of interest. One thing I’d like to do is make he file name and line number pop out in a different color.

I have a custom log_helper module I’m using:

import logging
import os
import pprint

# Initialize the logger at the module level, so it's only created once
logger = logging.getLogger('debug_logger')

# Set up the logger only if it hasn't been set up already
if not logger.handlers:
    log_level = os.environ.get('LOG_LEVEL', 'WARNING').upper()
    logger.setLevel(getattr(logging, log_level, logging.WARNING))

    formatter = logging.Formatter('%(levelname)s: %(message)s')
    handler = logging.StreamHandler()
    handler.setFormatter(formatter)
    logger.addHandler(handler)

class RelativePathFormatter(logging.Formatter):
    def __init__(self, *args, base_path=None, **kwargs):
        super().__init__(*args, **kwargs)
        # Define the base path (in your case ~/python)
        self.base_path = base_path if base_path else os.path.expanduser('~/python')
    
    def format(self, record):
        # Modify the pathname to be relative to the base path
        if record.pathname.startswith(self.base_path):
            record.pathname = os.path.relpath(record.pathname, self.base_path)
        return super().format(record)

# Setup logger using the custom formatter
def get_logger(name='debug_logger'):
    logger = logging.getLogger(name)

    # Remove existing handlers to avoid conflicts
    if logger.hasHandlers():
        logger.handlers.clear()

    log_level = os.environ.get('LOG_LEVEL', 'DEBUG').upper()
    logger.setLevel(getattr(logging, log_level, logging.DEBUG))

    # Apply custom formatter
    formatter = RelativePathFormatter(
        '%(levelname)-8s %(pathname)s:%(lineno)d - %(message)s',
        base_path=os.path.expanduser('~/python')
    )

    handler = logging.StreamHandler()
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    return logger

# Debug print function that respects log level and logs caller info
def d(data, depth=None):
    """
    Debug print function that logs data in DEBUG mode with optional depth control for complex data structures.
    Shows the correct file and line number of the caller, not log_helper.py.
    
    :param data: The data to log (complex or simple).
    :param depth: Optional depth to limit the structure's representation, defaults to None.
    """
    logger = get_logger()  # Get the global logger
    
    # Bail out immediately if the logger is not in DEBUG mode
    if not logger.isEnabledFor(logging.DEBUG):
        return
    
    # Format the data for debug output
    formatted_data = pprint.pformat(data, depth=depth)
    
    # Check if the formatted data has multiple lines
    if "n" in formatted_data:
        # Add a newline before the data if it spans multiple lines
        formatted_data = "n" + formatted_data

    # Adjust the stack level so the logger reports the caller's file and line number
    logger.debug(formatted_data, stacklevel=2)

I’m also using a pytest.ini file:

[pytest]
# Include numbered files as test files
python_files = test_*.py *_test.py [0-9]*-*.py 00-*_test
log_cli_format = %(levelname)-8s %(pathname)s:%(lineno)d - %(message)s 

Finally, I have this conftest.py file:

# File: ~/python/tests/conftest.py

import time
from pathlib import Path
from collections import defaultdict

import pytest

# ANSI color codes
CYAN = '33[96m'
YELLOW = '33[93m'
GREEN = '33[92m'
RED = '33[91m'
RESET = '33[0m'

test_results = {}
test_structure = defaultdict(lambda: defaultdict(list))
start_time = None

import pytest  # Make sure pytest is imported
import logging

@pytest.hookimpl(trylast=True)
def pytest_configure(config):
    logging_plugin = config.pluginmanager.get_plugin("logging-plugin")

    # Change color on existing log level
    logging_plugin.log_cli_handler.formatter.add_color_level(logging.DEBUG, "blue")
    logging_plugin.log_cli_handler.formatter.add_color_level(logging.INFO, "cyan")

def pytest_sessionstart(session):
    global start_time
    start_time = time.time()

def pytest_collection_modifyitems(session, config, items):
    # for item in items:
    #     path = Path(item.fspath).relative_to(Path(session.config.rootdir))
    #     dir_path = path.parent
    #     file_name = path.name
    #     test_structure[dir_path][file_name].append(item)

    # Sort items based on their location in the file
    items.sort(key=lambda x: x.location[1])

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    result = outcome.get_result()
    
    if result.when == 'call':
        test_results[item.nodeid] = result.outcome

def pytest_sessionfinish(session, exitstatus):
    global start_time
    duration = time.time() - start_time

    verbosity = session.config.option.verbose

    if verbosity > 1:
        print("n")
        for dir_path in sorted(test_structure.keys()):
            print(f"n{CYAN}{dir_path}{RESET}")
            for file_name in sorted(test_structure[dir_path].keys()):
                print(f"  {YELLOW}{file_name}{RESET}")
                for item in test_structure[dir_path][file_name]:
                    result = test_results.get(item.nodeid, "UNKNOWN")
                    if result == "passed":
                        color = GREEN
                    elif result == "failed":
                        color = RED
                    else:
                        color = YELLOW
                    indent = "    "
                    if "::" in item.name:
                        class_name, test_name = item.name.split("::")
                        print(f"    {class_name}")
                        indent = "      "
                    else:
                        test_name = item.name
                    print(f"{indent}{test_name} {color}{result.upper()}{RESET}")
    
    # Print summary
    passed = sum(1 for result in test_results.values() if result == "passed")
    failed = sum(1 for result in test_results.values() if result == "failed")
    total = len(test_results)
    print(f"n{GREEN if failed == 0 else RED}=== {passed} passed, {failed} failed, {total} total in {duration:.2f}s ==={RESET}n")

def pytest_terminal_summary(terminalreporter, exitstatus, config):
    # Completely override the default summary
    pass

I’ve tried a few different approaches but haven’t gotten anywhere.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật