-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage testing.py
More file actions
102 lines (84 loc) · 2.63 KB
/
package testing.py
File metadata and controls
102 lines (84 loc) · 2.63 KB
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
# Python program that can be executed to report whether particular
# python packages are available on the system.
# Source : http://www.southampton.ac.uk/~fangohr/blog/installation-of-python-spyder-numpy-sympy-scipy-pytest-matplotlib-via-anaconda-2014.html
"""
@author: Kanika
"""
import os
import math
import sys
def test_numpy():
try:
import numpy as np
except ImportError:
print("Could not import numpy -> numpy failed")
return None
# Simple test
a = np.arange(0, 100, 1)
assert np.sum(a) == sum(a)
print("-> numpy OK")
def test_scipy():
try:
import scipy
except ImportError:
print("Could not import 'scipy' -> scipy failed")
return None
# Simple test
import scipy.integrate
assert abs(scipy.integrate.quad(lambda x: x * x, 0, 6)[0] - 72.0) < 1e-6
print("-> scipy OK")
def test_pylab():
"""Actually testing matplotlib, as pylab is part of matplotlib."""
try:
import pylab
except ImportError:
print("Could not import 'matplotlib/pylab' -> failed")
return None
# Creata plot for testing purposes
xvalues = [i * 0.1 for i in range(100)]
yvalues = [math.sin(x) for x in xvalues]
pylab.plot(xvalues, yvalues, "-o", label="sin(x)")
pylab.legend()
pylab.xlabel('x')
testfilename='pylab-testfigure.png'
# check that file does not exist yet:
if os.path.exists(testfilename):
print("Skipping plotting to file as file {} exists already."\
.format(testfilename))
else:
# Write plot to file
pylab.savefig(testfilename)
# Then check that file exists
assert os.path.exists(testfilename)
print("-> pylab OK")
os.remove(testfilename)
def test_sympy():
try:
import sympy
except ImportError:
print("Could not import 'sympy' -> fail")
return None
# simple test
x = sympy.Symbol('x')
my_f = x ** 2
assert sympy.diff(my_f,x) == 2 * x
print("-> sympy OK")
def test_pytest():
try:
import pytest
except ImportError:
print("Could not import 'pytest' -> fail")
return None
print("-> pytest OK")
if __name__ == "__main__":
print("Running using Python {}".format(sys.version))
print("Testing numpy... "),
test_numpy()
print("Testing scipy... "),
test_scipy()
print("Testing matplotlib..."),
test_pylab()
print("Testing sympy... "),
test_sympy()
print("Testing pytest... "),
test_pytest()