blob: 2eaa15cf37a42ff8adc8aff6f773cf1057cfff56 [file] [log] [blame]
Tor Norbye3a2425a2013-11-04 10:16:08 -08001import os
2import sys
3helpers_dir = os.getenv("PYCHARM_HELPERS_DIR", sys.path[0])
4if sys.path[0] != helpers_dir:
5 sys.path.insert(0, helpers_dir)
6
7from tcmessages import TeamcityServiceMessages
8from pycharm_run_utils import adjust_sys_path
9
10adjust_sys_path(False)
11
12messages = TeamcityServiceMessages(prepend_linebreak=True)
13messages.testMatrixEntered()
14try:
15 import pytest
16 PYVERSION = [int(x) for x in pytest.__version__.split(".")]
17except:
18 import py
19 PYVERSION = [int(x) for x in py.__version__.split(".")]
20
21def get_name(nodeid):
22 return nodeid.split("::")[-1]
23
24def fspath_to_url(fspath):
25 return "file:///" + str(fspath).replace("\\", "/")
26
27if PYVERSION > [1, 4, 0]:
28 items = {}
29 current_suite = None
30 current_file = None
31 current_file_suite = None
32
33 def pytest_runtest_logstart(nodeid, location):
34 path = "file://" + os.path.realpath(location[0])
35 if location[1]:
36 path += ":" +str(location[1] + 1)
37 global current_suite, current_file, current_file_suite
38 current_file = nodeid.split("::")[0]
39
40 file_suite = current_file.split("/")[-1]
41 if file_suite != current_file_suite:
42 if current_suite:
43 messages.testSuiteFinished(current_suite)
44 if current_file_suite:
45 messages.testSuiteFinished(current_file_suite)
46 current_file_suite = file_suite
47 if current_file_suite:
48 messages.testSuiteStarted(current_file_suite, location="file://" + os.path.realpath(location[0]))
49
50 if location[2].find(".") != -1:
51 suite = location[2].split(".")[0]
52 name = location[2].split(".")[-1]
53 else:
54 name = location[2]
55 splitted = nodeid.split("::")
56 try:
57 ind = splitted.index(name.split("[")[0])
58 except ValueError:
59 try:
60 ind = splitted.index(name)
61 except ValueError:
62 ind = 0
63 if splitted[ind-1] == current_file:
64 suite = None
65 else:
66 suite = current_suite
67 if suite != current_suite:
68 if current_suite:
69 messages.testSuiteFinished(current_suite)
70 current_suite = suite
71 if current_suite:
72 messages.testSuiteStarted(current_suite, location="file://" + os.path.realpath(location[0]))
73 messages.testStarted(name, location=path)
74 items[nodeid] = name
75
76 def pytest_runtest_logreport(report):
77 name = items[report.nodeid]
78
79 if report.skipped:
80 messages.testIgnored(name)
81 elif report.failed:
82 messages.testFailed(name, details=report.longrepr)
83 elif report.when == "call":
84 messages.testFinished(name)
85
86 def pytest_sessionfinish(session, exitstatus):
87 if current_suite:
88 messages.testSuiteFinished(current_suite)
89 if current_file_suite:
90 messages.testSuiteFinished(current_file_suite)
91
92 from _pytest.terminal import TerminalReporter
93 class PycharmTestReporter(TerminalReporter):
94 def __init__(self, config, file=None):
95 TerminalReporter.__init__(self, config, file)
96
97 def summary_errors(self):
98 reports = self.getreports('error')
99 if not reports:
100 return
101 for rep in self.stats['error']:
102 name = rep.nodeid.split("/")[-1]
103 location = None
104 if hasattr(rep, 'location'):
105 location, lineno, domain = rep.location
106
107 messages.testSuiteStarted(name, location=fspath_to_url(location))
108 messages.testStarted("<noname>", location=fspath_to_url(location))
109 TerminalReporter.summary_errors(self)
110 messages.testError("<noname>")
111 messages.testSuiteFinished(name)
112
113else:
114 def pytest_collectstart(collector):
115 if collector.name != "()":
116 messages.testSuiteStarted(collector.name, location=fspath_to_url(collector.fspath))
117
118 def pytest_runtest_makereport(item, call):
119 if call.when == "setup":
120 fspath, lineno, msg = item.reportinfo()
121 url = fspath_to_url(fspath)
122 if lineno: url += ":" + str(lineno)
123 # messages.testStarted(item.name, location=url)
124
125 def pytest_runtest_logreport(report):
126 if report.item._args:
127 name = report.item.function.__name__ + str(report.item._args)
128 else:
129 name = report.item.name
130 if report.failed:
131 messages.testFailed(name, details=report.longrepr)
132 elif report.skipped:
133 messages.testIgnored(name)
134 else:
135 messages.testFinished(name)
136
137 def pytest_collectreport(report):
138 if report.collector.name != "()":
139 messages.testSuiteFinished(report.collector.name)
140
141 def pytest_itemstart(item, node=None):
142 if item._args:
143 name = item.function.__name__ + str(item._args)
144 else:
145 name = item.name
146 if hasattr(item, "_fslineno"):
147 path = fspath_to_url(item._fslineno[0]) + ":" + str(item._fslineno[1] + 1)
148 else:
149 path = fspath_to_url(item.fspath)
150 messages.testStarted(name, location=path)