#!/usr/bin/env python import json import subprocess class PackageTest: def __init__(self): self.status = "" self.tests = [] self.totaltime = 0 res = {} output = subprocess.run(["go","test","-count=1","-json","./..."], capture_output=True, text=True) output = str(output.stdout) output = output.split('\n') for line in output[:-1]: # parse the json parsed = json.loads(line) action = parsed["Action"] # skip if action in ["start", "output", "run"]: continue # create blank if doesn't exist if parsed["Package"] not in res: res[parsed["Package"]] = PackageTest() pkg = res[parsed["Package"]] if "Test" not in parsed: # top level package result pkg.status = action if "Elapsed" in parsed: pkg.totaltime = parsed["Elapsed"] else: # individual test pkg.tests.append((parsed["Test"],parsed["Action"],parsed["Elapsed"])) totalRan = 0 totalPassed = 0 totalTime = 0 # generating output from parsed json for name, info in res.items(): pkgname = name.split('/') pkgname = '/'.join(name.split('/')[1:]) if info.status == "skip": print("Skipped %s" % (pkgname)) continue print("\nTesting %s:" % (pkgname)) passed = 0 total = 0 for test in info.tests: total += 1 out = [] if test[1] == "pass": passed += 1 out = [" " + test[0] + ":",'\033[32mpass\033[0m ',str(test[2]) + 's'] elif test[1] == "fail": out = [" " + test[0] + ":",'\033[31mfail\033[0m ',str(test[2]) + 's'] print(f"{out[0] : <30}{out[1] : >5}{out[2] : >8}") result = "" if info.status == "pass": result = "\033[32mPASSED\033[0m" else: result = "\033[31mFAILED\033[0m" # keep track of grand totals totalRan += total totalPassed += passed totalTime += info.totaltime print(" %s %d/%d in %.3fs" % (result, passed, total, info.totaltime)) # output overall test statistics if totalRan == totalPassed: result = "\033[32mPASSED\033[0m" else: result = "\033[31mFAILED\033[0m" print("\nSUMMARY:\n\t%s %d/%d in %.3fs" % (result, totalPassed, totalRan, totalTime))