-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathunitutil.py
More file actions
59 lines (46 loc) · 1.53 KB
/
unitutil.py
File metadata and controls
59 lines (46 loc) · 1.53 KB
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
# -*- coding: utf-8 -*-
#
# unitutil.py
#
# Copyright (C) 2013 Steve Canny scanny@cisco.com
#
# This module is part of python-opc and is released under the MIT License:
# http://www.opensource.org/licenses/mit-license.php
"""Utility functions for unit testing"""
import os
from mock import patch
def abspath(relpath):
thisdir = os.path.split(__file__)[0]
return os.path.abspath(os.path.join(thisdir, relpath))
def class_mock(q_class_name, request):
"""
Return a mock patching the class with qualified name *q_class_name*.
Patch is reversed after calling test returns.
"""
_patch = patch(q_class_name, autospec=True)
request.addfinalizer(_patch.stop)
return _patch.start()
def function_mock(q_function_name, request):
"""
Return a mock patching the function with qualified name
*q_function_name*. Patch is reversed after calling test returns.
"""
_patch = patch(q_function_name)
request.addfinalizer(_patch.stop)
return _patch.start()
def initializer_mock(cls, request):
"""
Return a mock for the __init__ method on *cls* where the patch is
reversed after pytest uses it.
"""
_patch = patch.object(cls, '__init__', return_value=None)
request.addfinalizer(_patch.stop)
return _patch.start()
def method_mock(cls, method_name, request):
"""
Return a mock for method *method_name* on *cls* where the patch is
reversed after pytest uses it.
"""
_patch = patch.object(cls, method_name)
request.addfinalizer(_patch.stop)
return _patch.start()