Skip to content

Commit 59df302

Browse files
jrbourbeaujakirkham
authored andcommitted
Use pytest.importorskip in tests (#492)
* Use pytest.importorskip and add skip_test_env_var * Adds changelog entry
1 parent 005a9ac commit 59df302

File tree

6 files changed

+40
-145
lines changed

6 files changed

+40
-145
lines changed

docs/release.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ Upcoming Release
2828
* Removed support for Python 2.
2929
By :user:`jhamman`; :issue:`393`, :issue:`470`.
3030

31+
* Updates tests to use ``pytest.importorskip``.
32+
By :user:`James Bourbeau <jrbourbeau>`; :issue:`492`
33+
3134
* Update ``DirectoryStore`` to create files with more permissive permissions.
3235
By :user:`Eduardo Gonzalez <eddienko>` and :user:`James Bourbeau <jrbourbeau>`; :issue:`493`
3336

zarr/tests/test_convenience.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -654,14 +654,14 @@ def test_logging(self):
654654

655655

656656
def temp_h5f():
657+
h5py = pytest.importorskip("h5py")
657658
fn = tempfile.mktemp()
658659
atexit.register(os.remove, fn)
659660
h5f = h5py.File(fn, mode='w')
660661
atexit.register(lambda v: v.close(), h5f)
661662
return h5f
662663

663664

664-
@unittest.skipIf(h5py is None, 'h5py is not installed')
665665
class TestCopyHDF5ToZarr(TestCopy):
666666

667667
def __init__(self, *args, **kwargs):
@@ -672,7 +672,6 @@ def __init__(self, *args, **kwargs):
672672
self.new_dest = group
673673

674674

675-
@unittest.skipIf(h5py is None, 'h5py is not installed')
676675
class TestCopyZarrToHDF5(TestCopy):
677676

678677
def __init__(self, *args, **kwargs):
@@ -683,7 +682,6 @@ def __init__(self, *args, **kwargs):
683682
self.new_dest = temp_h5f
684683

685684

686-
@unittest.skipIf(h5py is None, 'h5py is not installed')
687685
class TestCopyHDF5ToHDF5(TestCopy):
688686

689687
def __init__(self, *args, **kwargs):

zarr/tests/test_core.py

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,7 @@
2323
LRUStoreCache, NestedDirectoryStore, SQLiteStore,
2424
atexit_rmglob, atexit_rmtree, init_array, init_group)
2525
from zarr.util import buffer_size
26-
27-
try:
28-
import azure.storage.blob as asb
29-
except ImportError: # pragma: no cover
30-
asb = None
31-
32-
33-
# also check for environment variables indicating whether tests requiring
34-
# services should be run
35-
ZARR_TEST_ABS = os.environ.get('ZARR_TEST_ABS', '0')
26+
from zarr.tests.util import skip_test_env_var
3627

3728

3829
# noinspection PyMethodMayBeStatic
@@ -1404,13 +1395,12 @@ def test_nbytes_stored(self):
14041395
assert expect_nbytes_stored == z.nbytes_stored
14051396

14061397

1407-
@pytest.mark.skipif(asb is None or ZARR_TEST_ABS == '0',
1408-
reason="azure-blob-storage could not be imported or tests not"
1409-
"enabled via environment variable")
1398+
@skip_test_env_var("ZARR_TEST_ABS")
14101399
class TestArrayWithABSStore(TestArray):
14111400

14121401
@staticmethod
14131402
def absstore():
1403+
asb = pytest.importorskip("azure.storage.blob")
14141404
blob_client = asb.BlockBlobService(is_emulated=True)
14151405
blob_client.delete_container('test')
14161406
blob_client.create_container('test')
@@ -1737,17 +1727,11 @@ def test_nbytes_stored(self):
17371727
pass # not implemented
17381728

17391729

1740-
try:
1741-
import bsddb3
1742-
except ImportError: # pragma: no cover
1743-
bsddb3 = None
1744-
1745-
1746-
@unittest.skipIf(bsddb3 is None, 'bsddb3 is not installed')
17471730
class TestArrayWithDBMStoreBerkeleyDB(TestArray):
17481731

17491732
@staticmethod
17501733
def create_array(read_only=False, **kwargs):
1734+
bsddb3 = pytest.importorskip("bsddb3")
17511735
path = mktemp(suffix='.dbm')
17521736
atexit.register(os.remove, path)
17531737
store = DBMStore(path, flag='n', open=bsddb3.btopen)
@@ -1762,17 +1746,11 @@ def test_nbytes_stored(self):
17621746
pass # not implemented
17631747

17641748

1765-
try:
1766-
import lmdb
1767-
except ImportError: # pragma: no cover
1768-
lmdb = None
1769-
1770-
1771-
@unittest.skipIf(lmdb is None, 'lmdb is not installed')
17721749
class TestArrayWithLMDBStore(TestArray):
17731750

17741751
@staticmethod
17751752
def create_array(read_only=False, **kwargs):
1753+
pytest.importorskip("lmdb")
17761754
path = mktemp(suffix='.lmdb')
17771755
atexit.register(atexit_rmtree, path)
17781756
store = LMDBStore(path, buffers=True)
@@ -1790,11 +1768,11 @@ def test_nbytes_stored(self):
17901768
pass # not implemented
17911769

17921770

1793-
@unittest.skipIf(lmdb is None, 'lmdb is not installed')
17941771
class TestArrayWithLMDBStoreNoBuffers(TestArray):
17951772

17961773
@staticmethod
17971774
def create_array(read_only=False, **kwargs):
1775+
pytest.importorskip("lmdb")
17981776
path = mktemp(suffix='.lmdb')
17991777
atexit.register(atexit_rmtree, path)
18001778
store = LMDBStore(path, buffers=False)
@@ -1809,17 +1787,11 @@ def test_nbytes_stored(self):
18091787
pass # not implemented
18101788

18111789

1812-
try:
1813-
import sqlite3
1814-
except ImportError: # pragma: no cover
1815-
sqlite3 = None
1816-
1817-
1818-
@unittest.skipIf(sqlite3 is None, 'python built without sqlite')
18191790
class TestArrayWithSQLiteStore(TestArray):
18201791

18211792
@staticmethod
18221793
def create_array(read_only=False, **kwargs):
1794+
pytest.importorskip("sqlite3")
18231795
path = mktemp(suffix='.db')
18241796
atexit.register(atexit_rmtree, path)
18251797
store = SQLiteStore(path)

zarr/tests/test_hierarchy.py

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,7 @@
2222
atexit_rmtree, group_meta_key, init_array,
2323
init_group)
2424
from zarr.util import InfoReporter
25-
26-
try:
27-
import azure.storage.blob as asb
28-
except ImportError: # pragma: no cover
29-
asb = None
30-
31-
32-
# also check for environment variables indicating whether tests requiring
33-
# services should be run
34-
ZARR_TEST_ABS = os.environ.get('ZARR_TEST_ABS', '0')
25+
from zarr.tests.util import skip_test_env_var
3526

3627

3728
# noinspection PyStatementEffect
@@ -887,13 +878,12 @@ def create_store():
887878
return store, None
888879

889880

890-
@pytest.mark.skipif(asb is None or ZARR_TEST_ABS == '0',
891-
reason="azure-blob-storage could not be imported or tests not enabled"
892-
"via environment variable")
881+
@skip_test_env_var("ZARR_TEST_ABS")
893882
class TestGroupWithABSStore(TestGroup):
894883

895884
@staticmethod
896885
def create_store():
886+
asb = pytest.importorskip("azure.storage.blob")
897887
blob_client = asb.BlockBlobService(is_emulated=True)
898888
blob_client.delete_container('test')
899889
blob_client.create_container('test')
@@ -933,50 +923,32 @@ def create_store():
933923
return store, None
934924

935925

936-
try:
937-
import bsddb3
938-
except ImportError: # pragma: no cover
939-
bsddb3 = None
940-
941-
942-
@unittest.skipIf(bsddb3 is None, 'bsddb3 is not installed')
943926
class TestGroupWithDBMStoreBerkeleyDB(TestGroup):
944927

945928
@staticmethod
946929
def create_store():
930+
bsddb3 = pytest.importorskip("bsddb3")
947931
path = tempfile.mktemp(suffix='.dbm')
948932
atexit.register(os.remove, path)
949933
store = DBMStore(path, flag='n', open=bsddb3.btopen)
950934
return store, None
951935

952936

953-
try:
954-
import lmdb
955-
except ImportError: # pragma: no cover
956-
lmdb = None
957-
958-
959-
@unittest.skipIf(lmdb is None, 'lmdb is not installed')
960937
class TestGroupWithLMDBStore(TestGroup):
961938

962939
@staticmethod
963940
def create_store():
941+
pytest.importorskip("lmdb")
964942
path = tempfile.mktemp(suffix='.lmdb')
965943
atexit.register(atexit_rmtree, path)
966944
store = LMDBStore(path)
967945
return store, None
968946

969947

970-
try:
971-
import sqlite3
972-
except ImportError: # pragma: no cover
973-
sqlite3 = None
974-
975-
976-
@unittest.skipIf(sqlite3 is None, 'python built without sqlite')
977948
class TestGroupWithSQLiteStore(TestGroup):
978949

979950
def create_store(self):
951+
pytest.importorskip("sqlite3")
980952
path = tempfile.mktemp(suffix='.db')
981953
atexit.register(atexit_rmtree, path)
982954
store = SQLiteStore(path)

0 commit comments

Comments
 (0)