Skip to content

Instantly share code, notes, and snippets.

@alrocar
Last active February 17, 2023 21:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alrocar/ae42cd9c57e83ef861ee99cc232e93cf to your computer and use it in GitHub Desktop.
Save alrocar/ae42cd9c57e83ef861ee99cc232e93cf to your computer and use it in GitHub Desktop.
import os
import datetime
import requests
import orjson
def pytest_terminal_summary(terminalreporter, exitstatus, config):
token = os.environ.get('TINYBIRD_TOKEN')
commit = os.environ.get('CI_COMMIT_SHA')
job_id = os.environ.get('CI_JOB_ID')
if not commit or not job_id:
# Only get metrics about CI
return
now = str(datetime.datetime.now())
report = []
for k in terminalreporter.stats:
for test in terminalreporter.stats[k]:
try:
report.append({
'date': now,
'commit': commit,
'job_id': job_id,
'job_url': os.environ.get('CI_JOB_URL', ''),
'job_name': os.environ.get('CI_JOB_NAME', ''),
'test_nodeid': test.nodeid,
'test_name': test.head_line,
'test_part': test.when,
'duration': test.duration,
'outcome': test.outcome
})
except Exception:
pass
data = b'\n'.join(orjson.dumps(x) for x in report)
response = requests.post(f'https://api.tinybird.co/v0/events?name=ci_tests&token={token}', data=data)
if response.status_code != 202:
print("Error while uploading to tinybird")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment