Browse Source

module: add pytest for unit and snapshot testing with travis-ci

cn 8 months ago
parent
commit
2086f111f2
9 changed files with 244 additions and 1 deletions
  1. 3
    0
      .gitignore
  2. 8
    0
      .travis.yml
  3. 2
    1
      README.md
  4. 2
    0
      pytest.ini
  5. 4
    0
      requirements.txt
  6. 0
    0
      tests/__init__.py
  7. 0
    0
      tests/snapshots/__init__.py
  8. 128
    0
      tests/snapshots/snap_test_veml6070.py
  9. 97
    0
      tests/test_veml6070.py

+ 3
- 0
.gitignore View File

@@ -1 +1,4 @@
1
+.cache/
2
+env/
3
+__pycache__/
1 4
 *.pyc

+ 8
- 0
.travis.yml View File

@@ -0,0 +1,8 @@
1
+---
2
+sudo: false
3
+language: python
4
+python:
5
+  - "2.7"
6
+
7
+script:
8
+  - pytest

+ 2
- 1
README.md View File

@@ -1,6 +1,7 @@
1
-
2 1
 # python-veml6070
3 2
 
3
+[![Build Status](https://travis-ci.org/cmur2/python-veml6070.svg?branch=master)](https://travis-ci.org/cmur2/python-veml6070)
4
+
4 5
 A Python library for accessing the [VEML6070 digital UV light sensor](http://www.vishay.com/docs/84277/veml6070.pdf) from Vishay via `python-smbus` using the I2C interface.
5 6
 
6 7
 Default settings are suitable for Raspberry Pi 2 and 3 and was successfully tested using a [breakout](https://github.com/watterott/VEML6070-Breakout).

+ 2
- 0
pytest.ini View File

@@ -0,0 +1,2 @@
1
+[pytest]
2
+norecursedirs = env* .*

+ 4
- 0
requirements.txt View File

@@ -0,0 +1,4 @@
1
+mock==2.0.0
2
+pylint==1.8.2
3
+pytest==3.0.6
4
+snapshottest==0.5.0

+ 0
- 0
tests/__init__.py View File


+ 0
- 0
tests/snapshots/__init__.py View File


+ 128
- 0
tests/snapshots/snap_test_veml6070.py View File

@@ -0,0 +1,128 @@
1
+# -*- coding: utf-8 -*-
2
+# snapshottest: v1 - https://goo.gl/zC4yUc
3
+from __future__ import unicode_literals
4
+
5
+from snapshottest import Snapshot
6
+
7
+
8
+snapshots = Snapshot()
9
+
10
+snapshots['TestVeml6070::test_disable 1'] = {
11
+    'readlog': [
12
+    ],
13
+    'writelog': [
14
+        (
15
+            '56',
16
+            [
17
+                6,
18
+                6,
19
+                6
20
+            ]
21
+        )
22
+    ]
23
+}
24
+
25
+snapshots['TestVeml6070::test_enable 1'] = {
26
+    'readlog': [
27
+    ],
28
+    'writelog': [
29
+        (
30
+            '56',
31
+            [
32
+                6,
33
+                6,
34
+                6
35
+            ]
36
+        )
37
+    ]
38
+}
39
+
40
+snapshots['TestVeml6070::test_integration_time 1'] = {
41
+    'readlog': [
42
+    ],
43
+    'writelog': [
44
+        (
45
+            '56',
46
+            [
47
+                2,
48
+                2,
49
+                14
50
+            ]
51
+        )
52
+    ]
53
+}
54
+
55
+snapshots['TestVeml6070::test_setup 1'] = {
56
+    'readlog': [
57
+    ],
58
+    'writelog': [
59
+        (
60
+            '56',
61
+            [
62
+                6,
63
+                6
64
+            ]
65
+        )
66
+    ]
67
+}
68
+
69
+snapshots['TestVeml6070::test_uva_light_intensity 1'] = {
70
+    'readlog': [
71
+        (
72
+            '56',
73
+            [
74
+                6,
75
+                6
76
+            ]
77
+        ),
78
+        (
79
+            '57',
80
+            [
81
+                1,
82
+                1
83
+            ]
84
+        )
85
+    ],
86
+    'writelog': [
87
+        (
88
+            '56',
89
+            [
90
+                6,
91
+                6,
92
+                6,
93
+                6,
94
+                14,
95
+                14,
96
+                14
97
+            ]
98
+        )
99
+    ]
100
+}
101
+
102
+snapshots['TestVeml6070::test_uva_light_intensity_raw 1'] = {
103
+    'readlog': [
104
+        (
105
+            '56',
106
+            [
107
+                52
108
+            ]
109
+        ),
110
+        (
111
+            '57',
112
+            [
113
+                18
114
+            ]
115
+        )
116
+    ],
117
+    'writelog': [
118
+        (
119
+            '56',
120
+            [
121
+                6,
122
+                6,
123
+                6,
124
+                6
125
+            ]
126
+        )
127
+    ]
128
+}

+ 97
- 0
tests/test_veml6070.py View File

@@ -0,0 +1,97 @@
1
+
2
+# import unittest
3
+import sys
4
+import time
5
+
6
+import mock
7
+
8
+import snapshottest
9
+
10
+# Note: prepare globally mocked modules first and then load our module
11
+MOCKED_SMBUS_MODULE = mock.Mock()
12
+sys.modules['smbus'] = MOCKED_SMBUS_MODULE
13
+time.sleep = lambda s: None
14
+import veml6070
15
+
16
+# inspired by https://github.com/adafruit/Adafruit_Python_GPIO/blob/master/Adafruit_GPIO/I2C.py
17
+class MockSMBus(object):
18
+    def __init__(self, initial_read=None):
19
+        self._writelog = {}
20
+        self._readlog = {}
21
+        self.initial_read = initial_read or {}
22
+
23
+    def read_byte(self, addr):
24
+        val = self.initial_read.get(addr).pop(0)
25
+        self._readlog.setdefault(addr, []).append(val)
26
+        return val
27
+
28
+    def write_byte(self, addr, val):
29
+        self._writelog.setdefault(addr, []).append(val)
30
+
31
+    def _get_log(self):
32
+        return {
33
+            'readlog': [(str(key), value) for (key, value) in self._readlog.items()],
34
+            'writelog': [(str(key), value) for (key, value) in self._writelog.items()]
35
+        }
36
+
37
+# def create_veml6070(**kwargs):
38
+#     mockbus = MockSMBus()
39
+#     smbus = Mock()
40
+#     smbus.SMBus.return_value = mockbus
41
+#     with patch.dict('sys.modules', {'smbus': smbus}):
42
+#         import veml6070
43
+#         # Note: our module constants only available in this scope
44
+#         return (veml6070.Veml6070(**kwargs), mockbus)
45
+
46
+def setup_mockbus(**kwargs):
47
+    mockbus = MockSMBus(**kwargs)
48
+    MOCKED_SMBUS_MODULE.SMBus.return_value = mockbus
49
+    return mockbus
50
+
51
+class TestVeml6070(snapshottest.TestCase):
52
+
53
+    def test_setup(self):
54
+        mockbus = setup_mockbus()
55
+        veml = veml6070.Veml6070()
56
+        self.assertIsNotNone(veml)
57
+        self.assertMatchSnapshot(mockbus._get_log())
58
+
59
+    def test_integration_time(self):
60
+        mockbus = setup_mockbus()
61
+        veml = veml6070.Veml6070(integration_time=veml6070.INTEGRATIONTIME_1_2T)
62
+        self.assertEqual(veml.get_integration_time(), veml6070.INTEGRATIONTIME_1_2T)
63
+        veml.set_integration_time(veml6070.INTEGRATIONTIME_4T)
64
+        self.assertEqual(veml.get_integration_time(), veml6070.INTEGRATIONTIME_4T)
65
+        self.assertMatchSnapshot(mockbus._get_log())
66
+
67
+    def test_enable(self):
68
+        mockbus = setup_mockbus()
69
+        veml = veml6070.Veml6070()
70
+        veml.enable()
71
+        self.assertMatchSnapshot(mockbus._get_log())
72
+
73
+    def test_disable(self):
74
+        mockbus = setup_mockbus()
75
+        veml = veml6070.Veml6070()
76
+        veml.disable()
77
+        self.assertMatchSnapshot(mockbus._get_log())
78
+
79
+    def test_uva_light_intensity_raw(self):
80
+        mockbus = setup_mockbus(initial_read={
81
+            0x38+1: [0x12],
82
+            0x38+0: [0x34]
83
+        })
84
+        veml = veml6070.Veml6070()
85
+        self.assertEqual(veml.get_uva_light_intensity_raw(), 0x1234)
86
+        self.assertMatchSnapshot(mockbus._get_log())
87
+
88
+    def test_uva_light_intensity(self):
89
+        mockbus = setup_mockbus(initial_read={
90
+            0x38+1: [0x01, 0x01],
91
+            0x38+0: [0x06, 0x06]
92
+        })
93
+        veml = veml6070.Veml6070()
94
+        self.assertEqual(veml.get_uva_light_intensity(), 0x0106 * 0.05625 / 1)
95
+        veml.set_integration_time(veml6070.INTEGRATIONTIME_4T)
96
+        self.assertEqual(veml.get_uva_light_intensity(), 0x0106 * 0.05625 / 4)
97
+        self.assertMatchSnapshot(mockbus._get_log())