forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
report.py
94 lines (88 loc) · 2.74 KB
/
report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
from __future__ import print_function
import json
def report_discovered(
tests,
parents,
# *,
pretty=False,
simple=False,
_send=print,
**_ignored
):
"""Serialize the discovered tests and write to stdout."""
if simple:
data = [
{
"id": test.id,
"name": test.name,
"testroot": test.path.root,
"relfile": test.path.relfile,
"lineno": test.lineno,
"testfunc": test.path.func,
"subtest": test.path.sub or None,
"markers": test.markers or [],
}
for test in tests
]
else:
byroot = {}
for parent in parents:
rootdir = parent.name if parent.root is None else parent.root
try:
root = byroot[rootdir]
except KeyError:
root = byroot[rootdir] = {
"id": rootdir,
"parents": [],
"tests": [],
}
if not parent.root:
root["id"] = parent.id
continue
root["parents"].append(
{
# "id" must match what the testing framework recognizes.
"id": parent.id,
"kind": parent.kind,
"name": parent.name,
"parentid": parent.parentid,
}
)
if parent.relpath is not None:
root["parents"][-1]["relpath"] = parent.relpath
for test in tests:
# We are guaranteed that the parent was added.
root = byroot[test.path.root]
testdata = {
# "id" must match what the testing framework recognizes.
"id": test.id,
"name": test.name,
# TODO: Add a "kind" field
# (e.g. "unittest", "function", "doctest")
"source": test.source,
"markers": test.markers or [],
"parentid": test.parentid,
}
root["tests"].append(testdata)
data = [
{
"rootid": byroot[root]["id"],
"root": root,
"parents": byroot[root]["parents"],
"tests": byroot[root]["tests"],
}
for root in sorted(byroot)
]
kwargs = {}
if pretty:
# human-formatted
kwargs = dict(
sort_keys=True,
indent=4,
separators=(",", ": "),
# ...
)
serialized = json.dumps(data, **kwargs)
_send(serialized)