22import pytest
33import random
44from struct import pack
5-
6- try :
7- import numpy as np
8- NUMPY_AVAILABLE = True
9- except ImportError :
10- NUMPY_AVAILABLE = False
5+ from .conftest import numpy as np
116
127
138class TestBit :
@@ -41,26 +36,34 @@ def test_bytes(self) -> None:
4136 assert Bit (b'\xff \x00 \xf0 ' ).to_text () == '111111110000000011110000'
4237 assert Bit (b'\xfe \x07 \x00 ' ).to_text () == '111111100000011100000000'
4338
44- @pytest .mark .skipif (not NUMPY_AVAILABLE , reason = 'NumPy required' )
4539 def test_ndarray (self ) -> None :
40+ if np is None :
41+ pytest .skip ('NumPy required' )
42+
4643 arr = np .array ([True , False , True ])
4744 assert Bit (arr ).to_list () == [True , False , True ]
4845 assert np .array_equal (Bit (arr ).to_numpy (), arr )
4946
50- @pytest .mark .skipif (not NUMPY_AVAILABLE , reason = 'NumPy required' )
5147 def test_ndarray_unpackbits (self ) -> None :
48+ if np is None :
49+ pytest .skip ('NumPy required' )
50+
5251 arr = np .unpackbits (np .array ([254 , 7 , 0 ], dtype = np .uint8 ))
5352 assert Bit (arr ).to_text () == '111111100000011100000000'
5453
55- @pytest .mark .skipif (not NUMPY_AVAILABLE , reason = 'NumPy required' )
5654 def test_ndarray_uint8 (self ) -> None :
55+ if np is None :
56+ pytest .skip ('NumPy required' )
57+
5758 arr = np .array ([254 , 7 , 0 ], dtype = np .uint8 )
5859 with pytest .raises (ValueError ) as error :
5960 Bit (arr )
6061 assert str (error .value ) == 'expected elements to be boolean'
6162
62- @pytest .mark .skipif (not NUMPY_AVAILABLE , reason = 'NumPy required' )
6363 def test_ndarray_uint16 (self ) -> None :
64+ if np is None :
65+ pytest .skip ('NumPy required' )
66+
6467 arr = np .array ([254 , 7 , 0 ], dtype = np .uint16 )
6568 with pytest .raises (ValueError ) as error :
6669 Bit (arr ) # type: ignore
@@ -86,14 +89,14 @@ def test_equality(self) -> None:
8689 def test_from_text (self ) -> None :
8790 vec = Bit .from_text ('101' )
8891 assert vec .to_list () == [True , False , True ]
89- if NUMPY_AVAILABLE :
92+ if np is not None :
9093 assert np .array_equal (vec .to_numpy (), [True , False , True ])
9194 assert vec .to_text () == '101'
9295
9396 def test_from_binary (self ) -> None :
9497 data = pack ('>iB' , 3 , 5 << 5 )
9598 vec = Bit .from_binary (data )
9699 assert vec .to_list () == [True , False , True ]
97- if NUMPY_AVAILABLE :
100+ if np is not None :
98101 assert np .array_equal (vec .to_numpy (), [True , False , True ])
99102 assert vec .to_binary () == data
0 commit comments