Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

from __future__ import absolute_import 

from __future__ import division 

from __future__ import print_function 

 

import os 

import warnings 

 

import py 

import six 

 

import _pytest._code 

from _pytest.compat import getfslineno 

from _pytest.mark.structures import NodeKeywords 

from _pytest.outcomes import fail 

 

SEP = "/" 

 

tracebackcutdir = py.path.local(_pytest.__file__).dirpath() 

 

 

def _splitnode(nodeid): 

"""Split a nodeid into constituent 'parts'. 

 

Node IDs are strings, and can be things like: 

'' 

'testing/code' 

'testing/code/test_excinfo.py' 

'testing/code/test_excinfo.py::TestFormattedExcinfo' 

 

Return values are lists e.g. 

[] 

['testing', 'code'] 

['testing', 'code', 'test_excinfo.py'] 

['testing', 'code', 'test_excinfo.py', 'TestFormattedExcinfo', '()'] 

""" 

if nodeid == "": 

# If there is no root node at all, return an empty list so the caller's logic can remain sane 

return [] 

parts = nodeid.split(SEP) 

# Replace single last element 'test_foo.py::Bar' with multiple elements 'test_foo.py', 'Bar' 

parts[-1:] = parts[-1].split("::") 

return parts 

 

 

def ischildnode(baseid, nodeid): 

"""Return True if the nodeid is a child node of the baseid. 

 

E.g. 'foo/bar::Baz' is a child of 'foo', 'foo/bar' and 'foo/bar::Baz', but not of 'foo/blorp' 

""" 

base_parts = _splitnode(baseid) 

node_parts = _splitnode(nodeid) 

if len(node_parts) < len(base_parts): 

return False 

return node_parts[: len(base_parts)] == base_parts 

 

 

class Node(object): 

""" base class for Collector and Item the test collection tree. 

Collector subclasses have children, Items are terminal nodes.""" 

 

def __init__( 

self, name, parent=None, config=None, session=None, fspath=None, nodeid=None 

): 

#: a unique name within the scope of the parent node 

self.name = name 

 

#: the parent collector node. 

self.parent = parent 

 

#: the pytest config object 

self.config = config or parent.config 

 

#: the session this node is part of 

self.session = session or parent.session 

 

#: filesystem path where this node was collected from (can be None) 

self.fspath = fspath or getattr(parent, "fspath", None) 

 

#: keywords/markers collected from all scopes 

self.keywords = NodeKeywords(self) 

 

#: the marker objects belonging to this node 

self.own_markers = [] 

 

#: allow adding of extra keywords to use for matching 

self.extra_keyword_matches = set() 

 

# used for storing artificial fixturedefs for direct parametrization 

self._name2pseudofixturedef = {} 

 

if nodeid is not None: 

assert "::()" not in nodeid 

self._nodeid = nodeid 

else: 

self._nodeid = self.parent.nodeid 

if self.name != "()": 

self._nodeid += "::" + self.name 

 

@property 

def ihook(self): 

""" fspath sensitive hook proxy used to call pytest hooks""" 

return self.session.gethookproxy(self.fspath) 

 

def __repr__(self): 

return "<%s %s>" % (self.__class__.__name__, getattr(self, "name", None)) 

 

def warn(self, warning): 

"""Issue a warning for this item. 

 

Warnings will be displayed after the test session, unless explicitly suppressed 

 

:param Warning warning: the warning instance to issue. Must be a subclass of PytestWarning. 

 

:raise ValueError: if ``warning`` instance is not a subclass of PytestWarning. 

 

Example usage:: 

 

.. code-block:: python 

 

node.warn(PytestWarning("some message")) 

""" 

from _pytest.warning_types import PytestWarning 

 

if not isinstance(warning, PytestWarning): 

raise ValueError( 

"warning must be an instance of PytestWarning or subclass, got {!r}".format( 

warning 

) 

) 

path, lineno = get_fslocation_from_item(self) 

warnings.warn_explicit( 

warning, 

category=None, 

filename=str(path), 

lineno=lineno + 1 if lineno is not None else None, 

) 

 

# methods for ordering nodes 

@property 

def nodeid(self): 

""" a ::-separated string denoting its collection tree address. """ 

return self._nodeid 

 

def __hash__(self): 

return hash(self.nodeid) 

 

def setup(self): 

pass 

 

def teardown(self): 

pass 

 

def listchain(self): 

""" return list of all parent collectors up to self, 

starting from root of collection tree. """ 

chain = [] 

item = self 

while item is not None: 

chain.append(item) 

item = item.parent 

chain.reverse() 

return chain 

 

def add_marker(self, marker, append=True): 

"""dynamically add a marker object to the node. 

 

:type marker: ``str`` or ``pytest.mark.*`` object 

:param marker: 

``append=True`` whether to append the marker, 

if ``False`` insert at position ``0``. 

""" 

from _pytest.mark import MarkDecorator, MARK_GEN 

 

if isinstance(marker, six.string_types): 

marker = getattr(MARK_GEN, marker) 

elif not isinstance(marker, MarkDecorator): 

raise ValueError("is not a string or pytest.mark.* Marker") 

self.keywords[marker.name] = marker 

if append: 

self.own_markers.append(marker.mark) 

else: 

self.own_markers.insert(0, marker.mark) 

 

def iter_markers(self, name=None): 

""" 

:param name: if given, filter the results by the name attribute 

 

iterate over all markers of the node 

""" 

return (x[1] for x in self.iter_markers_with_node(name=name)) 

 

def iter_markers_with_node(self, name=None): 

""" 

:param name: if given, filter the results by the name attribute 

 

iterate over all markers of the node 

returns sequence of tuples (node, mark) 

""" 

for node in reversed(self.listchain()): 

for mark in node.own_markers: 

if name is None or getattr(mark, "name", None) == name: 

yield node, mark 

 

def get_closest_marker(self, name, default=None): 

"""return the first marker matching the name, from closest (for example function) to farther level (for example 

module level). 

 

:param default: fallback return value of no marker was found 

:param name: name to filter by 

""" 

return next(self.iter_markers(name=name), default) 

 

def listextrakeywords(self): 

""" Return a set of all extra keywords in self and any parents.""" 

extra_keywords = set() 

for item in self.listchain(): 

extra_keywords.update(item.extra_keyword_matches) 

return extra_keywords 

 

def listnames(self): 

return [x.name for x in self.listchain()] 

 

def addfinalizer(self, fin): 

""" register a function to be called when this node is finalized. 

 

This method can only be called when this node is active 

in a setup chain, for example during self.setup(). 

""" 

self.session._setupstate.addfinalizer(fin, self) 

 

def getparent(self, cls): 

""" get the next parent node (including ourself) 

which is an instance of the given class""" 

current = self 

while current and not isinstance(current, cls): 

current = current.parent 

return current 

 

def _prunetraceback(self, excinfo): 

pass 

 

def _repr_failure_py(self, excinfo, style=None): 

if excinfo.errisinstance(fail.Exception): 

if not excinfo.value.pytrace: 

return six.text_type(excinfo.value) 

fm = self.session._fixturemanager 

if excinfo.errisinstance(fm.FixtureLookupError): 

return excinfo.value.formatrepr() 

tbfilter = True 

if self.config.option.fulltrace: 

style = "long" 

else: 

tb = _pytest._code.Traceback([excinfo.traceback[-1]]) 

self._prunetraceback(excinfo) 

if len(excinfo.traceback) == 0: 

excinfo.traceback = tb 

tbfilter = False # prunetraceback already does it 

if style == "auto": 

style = "long" 

# XXX should excinfo.getrepr record all data and toterminal() process it? 

if style is None: 

if self.config.option.tbstyle == "short": 

style = "short" 

else: 

style = "long" 

 

if self.config.option.verbose > 1: 

truncate_locals = False 

else: 

truncate_locals = True 

 

try: 

os.getcwd() 

abspath = False 

except OSError: 

abspath = True 

 

return excinfo.getrepr( 

funcargs=True, 

abspath=abspath, 

showlocals=self.config.option.showlocals, 

style=style, 

tbfilter=tbfilter, 

truncate_locals=truncate_locals, 

) 

 

repr_failure = _repr_failure_py 

 

 

def get_fslocation_from_item(item): 

"""Tries to extract the actual location from an item, depending on available attributes: 

 

* "fslocation": a pair (path, lineno) 

* "obj": a Python object that the item wraps. 

* "fspath": just a path 

 

:rtype: a tuple of (str|LocalPath, int) with filename and line number. 

""" 

result = getattr(item, "location", None) 

if result is not None: 

return result[:2] 

obj = getattr(item, "obj", None) 

if obj is not None: 

return getfslineno(obj) 

return getattr(item, "fspath", "unknown location"), -1 

 

 

class Collector(Node): 

""" Collector instances create children through collect() 

and thus iteratively build a tree. 

""" 

 

class CollectError(Exception): 

""" an error during collection, contains a custom message. """ 

 

def collect(self): 

""" returns a list of children (items and collectors) 

for this collection node. 

""" 

raise NotImplementedError("abstract") 

 

def repr_failure(self, excinfo): 

""" represent a collection failure. """ 

if excinfo.errisinstance(self.CollectError): 

exc = excinfo.value 

return str(exc.args[0]) 

return self._repr_failure_py(excinfo, style="short") 

 

def _prunetraceback(self, excinfo): 

if hasattr(self, "fspath"): 

traceback = excinfo.traceback 

ntraceback = traceback.cut(path=self.fspath) 

if ntraceback == traceback: 

ntraceback = ntraceback.cut(excludepath=tracebackcutdir) 

excinfo.traceback = ntraceback.filter() 

 

 

def _check_initialpaths_for_relpath(session, fspath): 

for initial_path in session._initialpaths: 

if fspath.common(initial_path) == initial_path: 

return fspath.relto(initial_path) 

 

 

class FSCollector(Collector): 

def __init__(self, fspath, parent=None, config=None, session=None, nodeid=None): 

fspath = py.path.local(fspath) # xxx only for test_resultlog.py? 

name = fspath.basename 

if parent is not None: 

rel = fspath.relto(parent.fspath) 

if rel: 

name = rel 

name = name.replace(os.sep, SEP) 

self.fspath = fspath 

 

session = session or parent.session 

 

if nodeid is None: 

nodeid = self.fspath.relto(session.config.rootdir) 

 

if not nodeid: 

nodeid = _check_initialpaths_for_relpath(session, fspath) 

if nodeid and os.sep != SEP: 

nodeid = nodeid.replace(os.sep, SEP) 

 

super(FSCollector, self).__init__( 

name, parent, config, session, nodeid=nodeid, fspath=fspath 

) 

 

 

class File(FSCollector): 

""" base class for collecting tests from a file. """ 

 

 

class Item(Node): 

""" a basic test invocation item. Note that for a single function 

there might be multiple test invocation items. 

""" 

 

nextitem = None 

 

def __init__(self, name, parent=None, config=None, session=None, nodeid=None): 

super(Item, self).__init__(name, parent, config, session, nodeid=nodeid) 

self._report_sections = [] 

 

#: user properties is a list of tuples (name, value) that holds user 

#: defined properties for this test. 

self.user_properties = [] 

 

def add_report_section(self, when, key, content): 

""" 

Adds a new report section, similar to what's done internally to add stdout and 

stderr captured output:: 

 

item.add_report_section("call", "stdout", "report section contents") 

 

:param str when: 

One of the possible capture states, ``"setup"``, ``"call"``, ``"teardown"``. 

:param str key: 

Name of the section, can be customized at will. Pytest uses ``"stdout"`` and 

``"stderr"`` internally. 

 

:param str content: 

The full contents as a string. 

""" 

if content: 

self._report_sections.append((when, key, content)) 

 

def reportinfo(self): 

return self.fspath, None, "" 

 

@property 

def location(self): 

try: 

return self._location 

except AttributeError: 

location = self.reportinfo() 

fspath = self.session._node_location_to_relpath(location[0]) 

location = (fspath, location[1], str(location[2])) 

self._location = location 

return location