Skip to content

Commit 2dbae23

Browse files
committed
Using Python2.7 unittest "skipping"
This also makes the test properly runnable using py.test.
1 parent 63d665c commit 2dbae23

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

test.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
from cluster import (HierarchicalClustering, KMeansClustering, ClusteringError)
1919
from difflib import SequenceMatcher
2020
import unittest
21+
try:
22+
import numpy
23+
NUMPY_AVAILABLE = True
24+
except:
25+
NUMPY_AVAILABLE = False
2126

2227

2328
def compare_list(x, y):
@@ -212,31 +217,26 @@ def testMultidimArray(self):
212217
cl.getclusters(10)
213218

214219

220+
@unittest.skipUnless(NUMPY_AVAILABLE,
221+
'numpy not available. Associated test will not be loaded!')
215222
class NumpyTests(unittest.TestCase):
216223

217224
def testNumpyRandom(self):
218-
from cluster import KMeansClustering
219-
from numpy import random as rnd
220-
data = rnd.rand(500, 2)
225+
data = numpy.random.rand(500, 2)
221226
cl = KMeansClustering(data, lambda p0, p1: (
222227
p0[0] - p1[0]) ** 2 + (p0[1] - p1[1]) ** 2, numpy.array_equal)
223228
cl.getclusters(10)
224229

225230

226231
if __name__ == '__main__':
227232
suite = unittest.TestSuite((
228-
unittest.makeSuite(HClusterSmallListTestCase),
229-
unittest.makeSuite(HClusterIntegerTestCase),
230-
unittest.makeSuite(HClusterStringTestCase),
231-
unittest.makeSuite(KClusterSmallListTestCase),
232-
unittest.makeSuite(KCluster2DTestCase),
233-
unittest.makeSuite(KClusterSFBugs)))
234-
235-
try:
236-
import numpy # NOQA
237-
tests = unittest.makeSuite(NumpyTests)
238-
suite.addTests(tests)
239-
except ImportError:
240-
print "numpy not available. Associated test will not be loaded!"
233+
unittest.makeSuite(HClusterIntegerTestCase),
234+
unittest.makeSuite(HClusterSmallListTestCase),
235+
unittest.makeSuite(HClusterStringTestCase),
236+
unittest.makeSuite(KCluster2DTestCase),
237+
unittest.makeSuite(KClusterSFBugs)),
238+
unittest.makeSuite(KClusterSmallListTestCase),
239+
unittest.makeSuite(NumpyTests),
240+
)
241241

242242
unittest.TextTestRunner(verbosity=2).run(suite)

0 commit comments

Comments
 (0)