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

import py 

 

from _pytest._code.code import TerminalRepr 

 

 

def getslaveinfoline(node): 

try: 

return node._slaveinfocache 

except AttributeError: 

d = node.slaveinfo 

ver = "%s.%s.%s" % d["version_info"][:3] 

node._slaveinfocache = s = "[%s] %s -- Python %s %s" % ( 

d["id"], 

d["sysplatform"], 

ver, 

d["executable"], 

) 

return s 

 

 

class BaseReport(object): 

when = None 

 

def __init__(self, **kw): 

self.__dict__.update(kw) 

 

def toterminal(self, out): 

if hasattr(self, "node"): 

out.line(getslaveinfoline(self.node)) 

 

longrepr = self.longrepr 

if longrepr is None: 

return 

 

if hasattr(longrepr, "toterminal"): 

longrepr.toterminal(out) 

else: 

try: 

out.line(longrepr) 

except UnicodeEncodeError: 

out.line("<unprintable longrepr>") 

 

def get_sections(self, prefix): 

for name, content in self.sections: 

if name.startswith(prefix): 

yield prefix, content 

 

@property 

def longreprtext(self): 

""" 

Read-only property that returns the full string representation 

of ``longrepr``. 

 

.. versionadded:: 3.0 

""" 

tw = py.io.TerminalWriter(stringio=True) 

tw.hasmarkup = False 

self.toterminal(tw) 

exc = tw.stringio.getvalue() 

return exc.strip() 

 

@property 

def caplog(self): 

"""Return captured log lines, if log capturing is enabled 

 

.. versionadded:: 3.5 

""" 

return "\n".join( 

content for (prefix, content) in self.get_sections("Captured log") 

) 

 

@property 

def capstdout(self): 

"""Return captured text from stdout, if capturing is enabled 

 

.. versionadded:: 3.0 

""" 

return "".join( 

content for (prefix, content) in self.get_sections("Captured stdout") 

) 

 

@property 

def capstderr(self): 

"""Return captured text from stderr, if capturing is enabled 

 

.. versionadded:: 3.0 

""" 

return "".join( 

content for (prefix, content) in self.get_sections("Captured stderr") 

) 

 

passed = property(lambda x: x.outcome == "passed") 

failed = property(lambda x: x.outcome == "failed") 

skipped = property(lambda x: x.outcome == "skipped") 

 

@property 

def fspath(self): 

return self.nodeid.split("::")[0] 

 

 

class TestReport(BaseReport): 

""" Basic test report object (also used for setup and teardown calls if 

they fail). 

""" 

 

def __init__( 

self, 

nodeid, 

location, 

keywords, 

outcome, 

longrepr, 

when, 

sections=(), 

duration=0, 

user_properties=None, 

**extra 

): 

#: normalized collection node id 

self.nodeid = nodeid 

 

#: a (filesystempath, lineno, domaininfo) tuple indicating the 

#: actual location of a test item - it might be different from the 

#: collected one e.g. if a method is inherited from a different module. 

self.location = location 

 

#: a name -> value dictionary containing all keywords and 

#: markers associated with a test invocation. 

self.keywords = keywords 

 

#: test outcome, always one of "passed", "failed", "skipped". 

self.outcome = outcome 

 

#: None or a failure representation. 

self.longrepr = longrepr 

 

#: one of 'setup', 'call', 'teardown' to indicate runtest phase. 

self.when = when 

 

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

#: defined properties of the test 

self.user_properties = list(user_properties or []) 

 

#: list of pairs ``(str, str)`` of extra information which needs to 

#: marshallable. Used by pytest to add captured text 

#: from ``stdout`` and ``stderr``, but may be used by other plugins 

#: to add arbitrary information to reports. 

self.sections = list(sections) 

 

#: time it took to run just the test 

self.duration = duration 

 

self.__dict__.update(extra) 

 

def __repr__(self): 

return "<TestReport %r when=%r outcome=%r>" % ( 

self.nodeid, 

self.when, 

self.outcome, 

) 

 

 

class CollectReport(BaseReport): 

when = "collect" 

 

def __init__(self, nodeid, outcome, longrepr, result, sections=(), **extra): 

self.nodeid = nodeid 

self.outcome = outcome 

self.longrepr = longrepr 

self.result = result or [] 

self.sections = list(sections) 

self.__dict__.update(extra) 

 

@property 

def location(self): 

return (self.fspath, None, self.fspath) 

 

def __repr__(self): 

return "<CollectReport %r lenresult=%s outcome=%r>" % ( 

self.nodeid, 

len(self.result), 

self.outcome, 

) 

 

 

class CollectErrorRepr(TerminalRepr): 

def __init__(self, msg): 

self.longrepr = msg 

 

def toterminal(self, out): 

out.line(self.longrepr, red=True)