Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50640 views
1
def pytest_addoption(parser):
2
"""specify comma-delimited list of kernel names to limit test"""
3
parser.addoption("--kname", action="store", help="kernel name")
4
5
def pytest_generate_tests(metafunc):
6
"""default is to test all non-sage kernels listed with `jupyter kernelspec list`"""
7
option_value = metafunc.config.option.kname
8
if 'kname' in metafunc.fixturenames and option_value is not None:
9
knames = option_value.split(',')
10
metafunc.parametrize("kname", knames)
11
# nsk = list of available non-sage kernel names
12
# skip first line of command output, "Available kernels"
13
else:
14
v = [x.strip() for x in os.popen("jupyter kernelspec list").readlines()]
15
nsk = [x.split()[0] for x in v[1:] if not x.startswith('sage')]
16
metafunc.parametrize("kname", nsk)
17
18
19
#
20
# Write machine-readable report files into the $HOME directory
21
# http://doc.pytest.org/en/latest/example/simple.html#post-process-test-reports-failures
22
#
23
import os
24
import json
25
import pytest
26
import time
27
from datetime import datetime
28
29
report_json = os.path.expanduser('~/sagews-direct-test-report.json')
30
report_prom = os.path.expanduser('~/sagews-direct-test-report.prom')
31
results = []
32
start_time = None
33
34
@pytest.hookimpl
35
def pytest_configure(config):
36
global start_time
37
start_time = datetime.utcnow()
38
39
@pytest.hookimpl
40
def pytest_unconfigure(config):
41
global start_time
42
def append_file(f1, f2):
43
with open(f1, 'a') as outf1:
44
with open(f2, 'r') as inf2:
45
outf1.write(inf2.read())
46
data = {
47
'name' : 'sagews_direct.test',
48
'version' : 1,
49
'start' : str(start_time),
50
'end' : str(datetime.utcnow()),
51
'fields' : ['name', 'outcome', 'duration'],
52
'results' : results,
53
}
54
report_json_tmp = report_json + '~'
55
with open(report_json, 'w') as out:
56
json.dump(data, out, indent=1)
57
# this is a plain text prometheus report
58
# https://prometheus.io/docs/instrumenting/exposition_formats/#text-format-details
59
# timestamp milliseconds since epoch
60
ts = int(1000 * time.mktime(start_time.timetuple()))
61
# first write to temp file ...
62
report_prom_tmp = report_prom + '~'
63
with open(report_prom_tmp, 'w') as prom:
64
for (name, outcome, duration) in results:
65
labels = 'name="{name}",outcome="{outcome}"'.format(**locals())
66
line = 'sagews_direct_test{{{labels}}} {duration} {ts}'.format(**locals())
67
prom.write(line + '\n')
68
# ... then atomically overwrite the real one
69
os.rename(report_prom_tmp, report_prom)
70
71
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
72
def pytest_runtest_makereport(item, call):
73
# execute all other hooks to obtain the report object
74
outcome = yield
75
rep = outcome.get_result()
76
77
if rep.when != "call":
78
return
79
80
#import pdb; pdb.set_trace() # uncomment to inspect item and rep objects
81
# the following `res` should match the `fields` above
82
# parent: item.parent.name could be interesting, but just () for auto discovery
83
name = item.name
84
test_ = 'test_'
85
if name.startswith(test_):
86
name = name[len(test_):]
87
res = [name, rep.outcome, rep.duration]
88
results.append(res)
89
90