tparse

repository·main·Indexed 22 days ago

https://github.com/mfridman/tparse

A command line tool designed to analyze and summarize `go test` output. It transforms verbose JSON test output (generated via the `-json` flag) into readable, colorized tables to help identify failures, panics, and test durations. It supports piping directly from `go test`, reading from saved files, and streaming raw output in real-time using the `-follow` flag.

Tokens
1.7K
Snippets
3
Records
10
Agent score
78%

What's inside tparse

  1. How tparse output is ordered

    main

    When using various flags, tparse follows a specific print order:

    1. Raw go test output (only if the -follow flag is used).
    2. Passed/skipped table (if -all, -skip, or -pass flags are used).
    3. Failed tests and panics.
    4. Package-level summary table.

    By default, tparse always returns test failures and panics, followed by a package-level summary table.

  2. Analyze go test output with tparse

    main

    tparse is a command line tool for analyzing and summarizing go test output.

    Important: You must run go test with the -json flag for tparse to work.

    There are two primary ways to use it:

    1. Piping output directly

    Run go test with the -json flag and pipe the output to tparse.

    set -o pipefail && go test fmt -json | tparse -all

    2. Using a saved file

    Save the go test -json output to a file first, then point tparse to that file using the -file option.

    go test fmt -json > fmt.out
    tparse -all -file=fmt.out
  3. Use FollowOutput to stream raw test logs

    main

    When running long-running test suites, you can use FollowOutput to see the raw go test output in real-time alongside tparse's progress indicators.

    • Set FollowOutput: true to stream raw output to stdout.
    • Set FollowOutputWriter to a specific io.WriteCloser to redirect the raw stream to a file instead of stdout.
  4. Use the tparse CLI to parse go test JSON output

    main

    tparse is a command-line tool designed to parse and format the JSON output from go test. It provides structured tables for test results, summaries, and coverage information.

    Common Usage Patterns

    1. Piping directly from go test (Standard):

    go test ./... -json | tparse

    2. Reading from a saved file: If you have already saved the JSON output to a file:

    tparse -file pkgs.out

    3. Following raw output: To see the raw go test output in real-time while tparse processes it:

    go test ./... -json | tparse -follow

    Or to write the raw output to a specific file while following:

    go test ./... -json | tparse -follow-output raw_output.json
    go test ./... -json | tparse
  5. Configure tparse using the Options struct

    main

    The Options struct allows you to control how tparse parses, follows, and displays test results.

    Key Configuration Fields

    FieldTypeDescription
    Outputio.WriterWhere to write the final output (tables, summary, etc.).
    FileNamestringPath to a file containing Go test output. If empty, stdin must be a pipe.
    DisableColorboolDisables all color output.
    FormatOutputFormatSets the output format for tables.
    Sorterparse.PackageSorterSets the sort order for the table.
    ShowNoTestsboolDisplays packages containing no test files or empty test files.
    FollowOutputboolIf true, follows the raw output as go test is running.
    FollowOutputWriterio.WriteCloserTakes precedence over FollowOutput. Writes raw output to this writer.
    ProgressboolPrints a single summary line for each package once completed.
    ProgressOutputio.WriterWhere to write progress updates (defaults to stdout).
    ComparestringPath to a previous test output file to include a diff in the summary table.
    IncludeTimestampboolWhen enabled with FollowOutput, includes timestamps with log lines.
    DisableTableOutputboolDisables all table output (useful for testing tparse itself).
    TestTableOptionsTestTableOptionsOptions for the tests table (e.g., filtering by Pass or Skip).
    SummaryTableOptionsSummaryTableOptionsOptions for the summary table.
  6. Configure tparse output display options

    main

    Use the following flags to customize the summary and detail level of the tparse output:

    • -all: A combination of -pass and -skip. Provides additional info like skipped tests and elapsed time for each passed test.
    • -pass: Shows additional info on passed tests. Tests are grouped by package and sorted by elapsed time in descending order.
    • -skip: Shows information about skipped tests.
    • -follow: Prints the raw go test output. It parses the JSON and prints it back out as if you ran go test without the -json flag, eliminating the need for tee /dev/tty.
    • -smallscreen: Useful for narrow displays; it divides long test names and renders them vertically.
    • -file: Specifies a file containing go test -json output to be parsed.

    Run tparse -h to see the full list of options.

  7. Run the tparse engine with Options

    main

    The Run function is the primary entrypoint for executing the tparse engine. It processes Go test output (either from a file or a pipe) and generates formatted tables and summaries.

    To use Run, provide an Options struct. If FileName is provided, tparse reads from that file. If FileName is empty, tparse expects input via a named pipe on stdin.

  8. tparse CLI Reference: Options and Flags

    main

    The tparse CLI supports several flags to customize the output format, sorting, and visibility of test results.

    Display Options

    • -all: Display table event for both pass and skip. (Failed items are always displayed).
    • -pass: Display table for passed tests.
    • -skip: Display table for skipped tests.
    • -notests: Display packages containing no test files or empty test files.
    • -progress: Print a single summary line for each package. Useful for long-running test suites.

    Formatting and Sorting

    • -format [basic|plain|markdown]: The output format for tables. Default is basic.
    • -sort [name|elapsed|cover]: Sort table output by attribute. Default is name.
    • -smallscreen: Split subtest names vertically to fit on smaller screens.
    • -trimpath: Remove path prefix from package names in output, simplifying their display.
    • -nocolor: Disable all colors. Also respects the NO_COLOR environment variable.

    Performance and Filtering

    • -slow [int]: Number of slowest tests to display. Default is 0 (displays all).
    • -compare [file]: Compare against a previous test output file (experimental).

    Input and Output

    • -file [path]: Read test output from a file.
    • -follow: Follow raw output from go test to stdout.
    • -follow-output [path]: Write raw output from go test to a file (takes precedence over -follow).
    • -include-timestamp: Include timestamps in follow output.