JUnit XML Formatter for flake8

0.0.6 · maintenance · verified Mon Apr 13

flake8-formatter-junit-xml is a Python package that provides a custom formatter for the flake8 linter, enabling it to output analysis results in the JUnit XML format. This is particularly useful for integrating flake8 reports into Continuous Integration/Continuous Delivery (CI/CD) systems like Jenkins, CircleCI, or GitLab, which typically consume JUnit XML for displaying test and code quality results. The current version is 0.0.6, and its release cadence appears to be infrequent, with the last update in 2018.

Warnings

Install

Quickstart

To use the `flake8-formatter-junit-xml` formatter, install the package and then invoke `flake8` from the command line, specifying `--format junit-xml`. The output can then be redirected to an XML file, which CI/CD systems can parse. Note that this formatter is loaded via flake8's entrypoint mechanism, so there's no direct Python import for user-level code.

# First, ensure flake8 is installed (if not already):
# pip install flake8
# 
# Then, run flake8 with the custom formatter:
# flake8 --format junit-xml your_project/ > flake8_report.xml

# Example: Create a dummy Python file with a linting error
with open('example.py', 'w') as f:
    f.write('def foo ( ) : pass\n')

# Run flake8 with the formatter and direct output to a file
import subprocess
import os

output_file = 'flake8_report.xml'
try:
    # Using shell=True for direct execution of the command string
    # In production, consider passing commands as a list for security
    subprocess.run(
        f'flake8 --format junit-xml example.py > {output_file}', 
        shell=True, 
        check=True, 
        capture_output=True
    )
    print(f"JUnit XML report generated at {output_file}")
    with open(output_file, 'r') as f:
        print(f.read())
except subprocess.CalledProcessError as e:
    print(f"Flake8 command failed with error: {e.stderr.decode()}")
except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Clean up dummy file and report
    if os.path.exists('example.py'):
        os.remove('example.py')
    if os.path.exists(output_file):
        os.remove(output_file)

view raw JSON →