1
1
import sys
2
- import xml .etree .ElementTree as ET
3
2
from test .support import TestStats
3
+ from typing import TYPE_CHECKING
4
4
5
5
from .runtests import RunTests
6
6
from .result import State , TestResult
9
9
printlist , count , format_duration )
10
10
11
11
12
+ if TYPE_CHECKING :
13
+ # Needed to annotate `TestResults.testsuite_xml` accurately
14
+ # Delay the runtime import until later,
15
+ # so that we only import it if we actually have to
16
+ import xml .etree .ElementTree as ET
17
+
18
+
12
19
EXITCODE_BAD_TEST = 2
13
20
EXITCODE_ENV_CHANGED = 3
14
21
EXITCODE_NO_TESTS_RAN = 4
@@ -32,7 +39,7 @@ def __init__(self):
32
39
self .test_times : list [tuple [float , TestName ]] = []
33
40
self .stats = TestStats ()
34
41
# used by --junit-xml
35
- self .testsuite_xml : list [ET .Element ] = []
42
+ self .testsuite_xml : list [" ET.Element" ] = []
36
43
37
44
def get_executed (self ):
38
45
return (set (self .good ) | set (self .bad ) | set (self .skipped )
@@ -132,6 +139,9 @@ def prepare_rerun(self) -> tuple[TestTuple, FilterDict]:
132
139
return (tuple (tests ), match_tests_dict )
133
140
134
141
def add_junit (self , xml_data : list [str ]):
142
+ # Local import, so that we only import this if we actually need the xml module.
143
+ # It's best to import as few things as possible when running a test.
144
+ import xml .etree .ElementTree as ET
135
145
for e in xml_data :
136
146
try :
137
147
self .testsuite_xml .append (ET .fromstring (e ))
0 commit comments