-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtest_utils.py
79 lines (54 loc) · 1.82 KB
/
test_utils.py
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
import inspect
import os
import sys
import pytest
from loafer.utils import add_current_dir_to_syspath, import_callable
def test_import_function():
func = import_callable('loafer.utils.import_callable')
assert callable(func)
assert inspect.isfunction(func)
def test_import_class():
klass = import_callable('loafer.exceptions.ProviderError')
assert klass.__name__ == 'ProviderError'
assert inspect.isclass(klass)
def test_error_on_method_name():
with pytest.raises(ImportError):
import_callable('unittest.mock.Mock.call_count')
def test_error_on_invalid_name():
with pytest.raises(ImportError):
import_callable('invalid-1234')
with pytest.raises(ImportError):
import_callable('')
def test_error_on_module():
with pytest.raises(ImportError):
import_callable('examples')
def test_error_on_non_callable():
with pytest.raises(ImportError):
import_callable('loafer')
@pytest.mark.xfail(os.getcwd() == '/tmp', run=False,
reason='This test is invalid if you are at /tmp')
def test_current_dir_in_syspath():
old_current = os.getcwd()
os.chdir('/tmp')
current = os.getcwd()
if current not in sys.path:
sys.path.append(current)
@add_current_dir_to_syspath
def inner_test():
assert current in sys.path
inner_test()
assert current in sys.path
sys.path.remove(current)
os.chdir(old_current)
@pytest.mark.xfail(os.getcwd() == '/tmp', run=False,
reason='This test is invalid if you are at /tmp')
def test_current_dir_not_in_syspath():
old_current = os.getcwd()
os.chdir('/tmp')
current = os.getcwd()
@add_current_dir_to_syspath
def inner_test():
assert current in sys.path
inner_test()
assert current not in sys.path
os.chdir(old_current)