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

"""py.test hacks to support XFAIL/XPASS""" 

 

from __future__ import print_function, division 

 

import sys 

import functools 

import os 

 

from sympy.core.compatibility import get_function_name 

 

try: 

import py 

from py.test import skip 

USE_PYTEST = getattr(sys, '_running_pytest', False) 

except ImportError: 

USE_PYTEST = False 

 

ON_TRAVIS = os.getenv('TRAVIS_BUILD_NUMBER', None) 

 

if not USE_PYTEST: 

def raises(expectedException, code=None): 

""" 

Tests that ``code`` raises the exception ``expectedException``. 

 

``code`` may be a callable, such as a lambda expression or function 

name. 

 

If ``code`` is not given or None, ``raises`` will return a context 

manager for use in ``with`` statements; the code to execute then 

comes from the scope of the ``with``. 

 

``raises()`` does nothing if the callable raises the expected exception, 

otherwise it raises an AssertionError. 

 

Examples 

======== 

 

>>> from sympy.utilities.pytest import raises 

 

>>> raises(ZeroDivisionError, lambda: 1/0) 

>>> raises(ZeroDivisionError, lambda: 1/2) 

Traceback (most recent call last): 

... 

AssertionError: DID NOT RAISE 

 

>>> with raises(ZeroDivisionError): 

... n = 1/0 

>>> with raises(ZeroDivisionError): 

... n = 1/2 

Traceback (most recent call last): 

... 

AssertionError: DID NOT RAISE 

 

Note that you cannot test multiple statements via 

``with raises``: 

 

>>> with raises(ZeroDivisionError): 

... n = 1/0 # will execute and raise, aborting the ``with`` 

... n = 9999/0 # never executed 

 

This is just what ``with`` is supposed to do: abort the 

contained statement sequence at the first exception and let 

the context manager deal with the exception. 

 

To test multiple statements, you'll need a separate ``with`` 

for each: 

 

>>> with raises(ZeroDivisionError): 

... n = 1/0 # will execute and raise 

>>> with raises(ZeroDivisionError): 

... n = 9999/0 # will also execute and raise 

 

""" 

if code is None: 

return RaisesContext(expectedException) 

elif callable(code): 

try: 

code() 

except expectedException: 

return 

raise AssertionError("DID NOT RAISE") 

elif isinstance(code, str): 

raise TypeError( 

'\'raises(xxx, "code")\' has been phased out; ' 

'change \'raises(xxx, "expression")\' ' 

'to \'raises(xxx, lambda: expression)\', ' 

'\'raises(xxx, "statement")\' ' 

'to \'with raises(xxx): statement\'') 

else: 

raise TypeError( 

'raises() expects a callable for the 2nd argument.') 

 

class RaisesContext(object): 

def __init__(self, expectedException): 

self.expectedException = expectedException 

 

def __enter__(self): 

return None 

 

def __exit__(self, exc_type, exc_value, traceback): 

if exc_type is None: 

raise AssertionError("DID NOT RAISE") 

return issubclass(exc_type, self.expectedException) 

 

class XFail(Exception): 

pass 

 

class XPass(Exception): 

pass 

 

class Skipped(Exception): 

pass 

 

def XFAIL(func): 

def wrapper(): 

try: 

func() 

except Exception as e: 

message = str(e) 

if message != "Timeout": 

raise XFail(get_function_name(func)) 

else: 

raise Skipped("Timeout") 

raise XPass(get_function_name(func)) 

 

wrapper = functools.update_wrapper(wrapper, func) 

return wrapper 

 

def skip(str): 

raise Skipped(str) 

 

def SKIP(reason): 

"""Similar to :func:`skip`, but this is a decorator. """ 

def wrapper(func): 

def func_wrapper(): 

raise Skipped(reason) 

 

func_wrapper = functools.update_wrapper(func_wrapper, func) 

return func_wrapper 

 

return wrapper 

 

def slow(func): 

func._slow = True 

 

def func_wrapper(): 

func() 

 

func_wrapper = functools.update_wrapper(func_wrapper, func) 

return func_wrapper 

 

else: 

XFAIL = py.test.mark.xfail 

slow = py.test.mark.slow 

 

def SKIP(reason): 

def skipping(func): 

@functools.wraps(func) 

def inner(*args, **kwargs): 

skip(reason) 

return inner 

 

return skipping