Skip to content

Commit 19d0aa6

Browse files
committed
TST: use unittest.skipUnless for conditional tests.
Add flags has_pyobjcryst, has_periodictable for availability of optional packages. Use unittest `skip` function to ignore tests that cannot be run. Remove mock-up classes that were used for test avoidance before.
1 parent d004a61 commit 19d0aa6

8 files changed

Lines changed: 57 additions & 37 deletions

File tree

src/diffpy/srreal/tests/run.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"""
2020

2121

22-
2322
if __name__ == '__main__':
2423
import sys
2524
from diffpy.srreal.tests import test

src/diffpy/srreal/tests/testatomradiitable.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
import unittest
88
import pickle
9-
from diffpy.srreal.tests.testutils import TestCasePeriodictableOptional
9+
from diffpy.srreal.tests.testutils import has_periodictable, _msg_noperiodictable
1010
from diffpy.srreal.atomradiitable import AtomRadiiTable, CovalentRadiiTable
1111
from diffpy.srreal.atomradiitable import ConstantRadiiTable
1212

13-
##############################################################################
13+
# ----------------------------------------------------------------------------
14+
1415
class TestAtomRadiiTable(unittest.TestCase):
1516

1617
def setUp(self):
@@ -108,8 +109,10 @@ def test_toString(self):
108109

109110
# End of class TestAtomRadiiTable
110111

111-
##############################################################################
112-
class TestCovalentRadiiTable(TestCasePeriodictableOptional):
112+
# ----------------------------------------------------------------------------
113+
114+
@unittest.skipUnless(has_periodictable, _msg_noperiodictable)
115+
class TestCovalentRadiiTable(unittest.TestCase):
113116

114117
def setUp(self):
115118
self.rtb = CovalentRadiiTable()
@@ -205,6 +208,8 @@ def test_toString(self):
205208

206209
# End of class TestCovalentRadiiTable
207210

211+
# ----------------------------------------------------------------------------
212+
208213
if __name__ == '__main__':
209214
unittest.main()
210215

src/diffpy/srreal/tests/testbondcalculator.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
import pickle
99
import numpy
1010

11-
from diffpy.srreal.tests.testutils import TestCaseObjCrystOptional
11+
from diffpy.srreal.tests.testutils import has_pyobjcryst, _msg_nopyobjcryst
1212
from diffpy.srreal.tests.testutils import loadDiffPyStructure
1313
from diffpy.srreal.tests.testutils import loadObjCrystCrystal
1414
from diffpy.srreal.bondcalculator import BondCalculator
1515

16-
##############################################################################
16+
# ----------------------------------------------------------------------------
17+
1718
class TestBondCalculator(unittest.TestCase):
1819

1920
def setUp(self):
@@ -267,9 +268,10 @@ def test_setTypeMask(self):
267268

268269
# End of class TestBondCalculator
269270

271+
# ----------------------------------------------------------------------------
270272

271-
##############################################################################
272-
class TestBondCalculatorObjCryst(TestCaseObjCrystOptional):
273+
@unittest.skipUnless(has_pyobjcryst, _msg_nopyobjcryst)
274+
class TestBondCalculatorObjCryst(unittest.TestCase):
273275

274276
def setUp(self):
275277
self.bdc = BondCalculator()
@@ -372,6 +374,9 @@ def test_filterOff(self):
372374
self.assertEqual(12, len(bdc(self.nickel)))
373375
return
374376

377+
# End of class TestBondCalculatorObjCryst
378+
379+
# ----------------------------------------------------------------------------
375380

376381
if __name__ == '__main__':
377382
unittest.main()

src/diffpy/srreal/tests/testoverlapcalculator.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
import copy
1010
import numpy
1111

12-
from diffpy.srreal.tests.testutils import TestCaseObjCrystOptional
12+
from diffpy.srreal.tests.testutils import has_pyobjcryst, _msg_nopyobjcryst
1313
from diffpy.srreal.tests.testutils import loadDiffPyStructure
1414
from diffpy.srreal.tests.testutils import loadObjCrystCrystal
1515
from diffpy.srreal.overlapcalculator import OverlapCalculator
1616

17-
##############################################################################
17+
# ----------------------------------------------------------------------------
18+
1819
class TestOverlapCalculator(unittest.TestCase):
1920

2021
pool = None
@@ -329,8 +330,10 @@ def test_neighborhoods(self):
329330

330331
# End of class TestOverlapCalculator
331332

332-
##############################################################################
333-
class TestOverlapCalculatorObjCryst(TestCaseObjCrystOptional):
333+
# ----------------------------------------------------------------------------
334+
335+
@unittest.skipUnless(has_pyobjcryst, _msg_nopyobjcryst)
336+
class TestOverlapCalculatorObjCryst(unittest.TestCase):
334337

335338
def setUp(self):
336339
self.olc = OverlapCalculator()
@@ -457,6 +460,8 @@ def test_neighborhoods(self):
457460

458461
# End of class TestOverlapCalculatorObjCryst
459462

463+
# ----------------------------------------------------------------------------
464+
460465
if __name__ == '__main__':
461466
unittest.main()
462467

src/diffpy/srreal/tests/testpdfcalcobjcryst.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import numpy
1111
from diffpy.srreal.pdfcalculator import PDFCalculator
1212

13-
from diffpy.srreal.tests.testutils import TestCaseObjCrystOptional
13+
from diffpy.srreal.tests.testutils import has_pyobjcryst, _msg_nopyobjcryst
1414
from diffpy.srreal.tests.testutils import loadObjCrystCrystal
1515
from diffpy.srreal.tests.testutils import datafile
1616
from diffpy.srreal.tests.testpdfcalculator import _maxNormDiff
@@ -54,9 +54,10 @@ def _makePDFCalculator(crst, cfgdict):
5454
setattr(pdfc, k, v)
5555
return pdfc
5656

57+
# ----------------------------------------------------------------------------
5758

58-
##############################################################################
59-
class TestPDFCalcObjcryst(TestCaseObjCrystOptional):
59+
@unittest.skipUnless(has_pyobjcryst, _msg_nopyobjcryst)
60+
class TestPDFCalcObjcryst(unittest.TestCase):
6061

6162
def _comparePDFs(self, nickname, pdfbasename, cifbasename):
6263
def setself(**kwtoset):
@@ -100,9 +101,11 @@ def test_rutileaniso(self):
100101
self.assertTrue(self.rutileaniso_mxnd < 0.057)
101102
return
102103

103-
104104
# End of class TestPDFCalcObjcryst
105105

106+
# ----------------------------------------------------------------------------
106107

107108
if __name__ == '__main__':
108109
unittest.main()
110+
111+
# End of file

src/diffpy/srreal/tests/testsfaverage.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
from diffpy.srreal.sfaverage import SFAverage
1111
from diffpy.srreal.scatteringfactortable import ScatteringFactorTable
1212
from diffpy.srreal.tests.testutils import loadDiffPyStructure
13-
from diffpy.srreal.tests.testutils import TestCaseObjCrystOptional
13+
from diffpy.srreal.tests.testutils import has_pyobjcryst, _msg_nopyobjcryst
1414
from diffpy.srreal.tests.testutils import loadObjCrystCrystal
1515

16+
# ----------------------------------------------------------------------------
1617

17-
##############################################################################
1818
class TestSFAverage(unittest.TestCase):
1919

2020
def setUp(self):
@@ -76,8 +76,10 @@ def test_fromComposition(self):
7676

7777
# End of class TestSFAverage
7878

79-
##############################################################################
80-
class TestSFAverageObjCryst(TestCaseObjCrystOptional):
79+
# ----------------------------------------------------------------------------
80+
81+
@unittest.skipUnless(has_pyobjcryst, _msg_nopyobjcryst)
82+
class TestSFAverageObjCryst(unittest.TestCase):
8183

8284
def setUp(self):
8385
self.sftx = ScatteringFactorTable.createByType('X')
@@ -102,6 +104,8 @@ def test_from_rutile(self):
102104

103105
# End of class TestSFAverageObjCryst
104106

107+
# ----------------------------------------------------------------------------
108+
105109
if __name__ == '__main__':
106110
unittest.main()
107111

src/diffpy/srreal/tests/teststructureadapter.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import numpy
1010
from diffpy.Structure import Structure
1111
from diffpy.srreal.pdfcalculator import PDFCalculator
12-
from diffpy.srreal.tests.testutils import TestCaseObjCrystOptional
12+
from diffpy.srreal.tests.testutils import has_pyobjcryst, _msg_nopyobjcryst
1313
from diffpy.srreal.tests.testutils import loadObjCrystCrystal
1414
from diffpy.srreal.tests.testutils import loadDiffPyStructure
1515
from diffpy.srreal.structureadapter import (
@@ -250,7 +250,8 @@ def test_nosymmetry_pickling(self):
250250

251251
# ----------------------------------------------------------------------------
252252

253-
class TestPyObjCrystAdapter(TestCaseObjCrystOptional):
253+
@unittest.skipUnless(has_pyobjcryst, _msg_nopyobjcryst)
254+
class TestPyObjCrystAdapter(unittest.TestCase):
254255

255256
def setUp(self):
256257
rutile_crystal = loadObjCrystCrystal('TiO2_rutile-fit.cif')
@@ -285,7 +286,7 @@ def test_objcryst_pickling(self):
285286
self.assertTrue(numpy.array_equal(g0, g1))
286287
return
287288

288-
# End of class TestNoSymmetry
289+
# End of class TestPyObjCrystAdapter
289290

290291
# ----------------------------------------------------------------------------
291292

src/diffpy/srreal/tests/testutils.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
#!/usr/bin/env python
22

33
"""Helper routines for running other unit tests.
4-
5-
TestCaseObjCrystOptional -- use this as a TestCase base class that
6-
disables unit tests when pyobjcryst is not installed.
7-
8-
TestCasePeriodictableOptional -- use this as a TestCase base class to
9-
skip unit tests when periodictable is not installed.
104
"""
115

126

@@ -16,28 +10,32 @@
1610
from diffpy.srreal.structureconverters import convertObjCrystCrystal
1711
from diffpy.srreal.tests import logger
1812

19-
# class TestCaseObjCrystOptional
13+
# Resolve availability of optional packages.
14+
15+
# pyobjcryst
2016

17+
_msg_nopyobjcryst = "No module named 'pyobjcryst'"
2118
try:
2219
import pyobjcryst.crystal
23-
from unittest import TestCase as TestCaseObjCrystOptional
2420
convertObjCrystCrystal(pyobjcryst.crystal.Crystal())
21+
has_pyobjcryst = True
2522
except ImportError:
26-
TestCaseObjCrystOptional = object
23+
has_pyobjcryst = False
2724
logger.warning('Cannot import pyobjcryst, pyobjcryst tests skipped.')
2825
except TypeError:
29-
TestCaseObjCrystOptional = object
26+
has_pyobjcryst = False
3027
logger.warning('Compiled without ObjCryst, pyobjcryst tests skipped.')
3128

32-
# class TestCasePeriodictableOptional
29+
# periodictable
3330

31+
_msg_noperiodictable = "No module named 'periodictable'"
3432
try:
3533
import periodictable
36-
from unittest import TestCase as TestCasePeriodictableOptional
34+
has_periodictable = True
3735
# silence the pyflakes syntax checker
3836
del periodictable
3937
except ImportError:
40-
TestCasePeriodictableOptional = object
38+
has_periodictable = False
4139
logger.warning('Cannot import periodictable, periodictable tests skipped.')
4240

4341
# helper functions

0 commit comments

Comments
 (0)