mirror of
https://github.com/cmur2/python-bme280.git
synced 2024-12-21 06:54:22 +01:00
module: add pytest for unit and snapshot testing with travis-ci
This commit is contained in:
parent
d6d215d1f9
commit
6aabf21cf8
3
.gitignore
vendored
3
.gitignore
vendored
@ -1 +1,4 @@
|
||||
.cache/
|
||||
env/
|
||||
__pycache__/
|
||||
*.pyc
|
||||
|
14
.travis.yml
Normal file
14
.travis.yml
Normal file
@ -0,0 +1,14 @@
|
||||
---
|
||||
sudo: false
|
||||
language: python
|
||||
python:
|
||||
- "2.7"
|
||||
- "3.6"
|
||||
|
||||
matrix:
|
||||
fast_finish: true
|
||||
allow_failures:
|
||||
- python: "3.6"
|
||||
|
||||
script:
|
||||
- pytest
|
@ -1,6 +1,7 @@
|
||||
|
||||
# python-bme280
|
||||
|
||||
[![Build Status](https://travis-ci.org/cmur2/python-bme280.svg?branch=master)](https://travis-ci.org/cmur2/python-bme280)
|
||||
|
||||
A Python library for accessing the [BME280 combined humidity and pressure](https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME280_DS001-11.pdf) from Bosch via `python-smbus` using the I2C interface.
|
||||
|
||||
Default settings are suitable for Raspberry Pi 2 and 3 and was successfully tested using a [breakout](https://github.com/watterott/BME280-Breakout).
|
||||
|
@ -1,4 +1,6 @@
|
||||
|
||||
from bme280 import Bme280
|
||||
#from bme280 import HO_SKIPPED, HO_1, HO_2, HO_4, HO_8, HO_16, PO_SKIPPED, PO_1, PO_2, PO_4, PO_8, PO_16, TO_SKIPPED, TO_1, TO_2, TO_4, TO_8, TO_16
|
||||
#from bme280 import MODE_SLEEP, MODE_FORCED, MODE_NORMAL
|
||||
from bme280 import HO_SKIPPED, HO_1, HO_2, HO_4, HO_8, HO_16, PO_SKIPPED, PO_1, PO_2, PO_4, PO_8, PO_16, TO_SKIPPED, TO_1, TO_2, TO_4, TO_8, TO_16
|
||||
from bme280 import MODE_SLEEP, MODE_FORCED, MODE_NORMAL
|
||||
from bme280 import TSTANDBY_0_5, TSTANDBY_62_5, TSTANDBY_125, TSTANDBY_250, TSTANDBY_500, TSTANDBY_1000, TSTANDBY_10, TSTANDBY_20
|
||||
from bme280 import FILTER_OFF, FILTER_2, FILTER_4, FILTER_8, FILTER_16
|
||||
|
2
pytest.ini
Normal file
2
pytest.ini
Normal file
@ -0,0 +1,2 @@
|
||||
[pytest]
|
||||
norecursedirs = env* .*
|
3
requirements.txt
Normal file
3
requirements.txt
Normal file
@ -0,0 +1,3 @@
|
||||
mock==2.0.0
|
||||
pytest==3.0.6
|
||||
snapshottest==0.5.0
|
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
133
tests/bme280_test.py
Normal file
133
tests/bme280_test.py
Normal file
@ -0,0 +1,133 @@
|
||||
|
||||
# import unittest
|
||||
import sys
|
||||
import time
|
||||
|
||||
import mock
|
||||
|
||||
import snapshottest
|
||||
|
||||
# Note: prepare globally mocked modules first and then load our module
|
||||
MOCKED_SMBUS_MODULE = mock.Mock()
|
||||
sys.modules['smbus'] = MOCKED_SMBUS_MODULE
|
||||
time.sleep = lambda s: None
|
||||
import bme280
|
||||
|
||||
# inspired by https://github.com/adafruit/Adafruit_Python_GPIO/blob/master/Adafruit_GPIO/I2C.py
|
||||
class MockSMBus(object):
|
||||
def __init__(self, initial_read=None):
|
||||
self._log = []
|
||||
self.initial_read = initial_read or {}
|
||||
|
||||
def read_byte(self, addr):
|
||||
val = self.initial_read.get(addr).pop(0)
|
||||
self._log.append(('r', addr, val))
|
||||
return val
|
||||
|
||||
def write_byte(self, addr, val):
|
||||
self._log.append(('w', addr, val))
|
||||
|
||||
def read_byte_data(self, addr, cmd):
|
||||
val = self.initial_read.get(addr).pop(0)
|
||||
self._log.append(('w', addr, cmd))
|
||||
self._log.append(('r', addr, val))
|
||||
return val
|
||||
|
||||
def write_byte_data(self, addr, cmd, val):
|
||||
self._log.append(('w', addr, cmd))
|
||||
self._log.append(('w', addr, val))
|
||||
|
||||
def setup_mockbus(**kwargs):
|
||||
mockbus = MockSMBus(**kwargs)
|
||||
MOCKED_SMBUS_MODULE.SMBus.return_value = mockbus
|
||||
return mockbus
|
||||
|
||||
def calibration_read():
|
||||
return [0x00] * (24+1+7)
|
||||
|
||||
class TestBme280(snapshottest.TestCase):
|
||||
|
||||
def test_setup(self):
|
||||
mockbus = setup_mockbus(initial_read={
|
||||
0x76: calibration_read()
|
||||
})
|
||||
bme = bme280.Bme280()
|
||||
MOCKED_SMBUS_MODULE.SMBus.assert_called_with(1)
|
||||
self.assertIsNotNone(bme)
|
||||
self.assertMatchSnapshot(mockbus._log)
|
||||
|
||||
def test_chip_id(self):
|
||||
mockbus = setup_mockbus(initial_read={
|
||||
0x76: calibration_read() + [0x23]
|
||||
})
|
||||
bme = bme280.Bme280()
|
||||
self.assertEqual(bme.get_chip_id(), 0x23)
|
||||
self.assertMatchSnapshot(mockbus._log)
|
||||
|
||||
def test_reset(self):
|
||||
mockbus = setup_mockbus(initial_read={
|
||||
0x76: calibration_read()
|
||||
})
|
||||
bme = bme280.Bme280()
|
||||
bme.reset()
|
||||
self.assertMatchSnapshot(mockbus._log)
|
||||
|
||||
def test_status(self):
|
||||
mockbus = setup_mockbus(initial_read={
|
||||
0x76: calibration_read() + [0x08, 0x00, 0x01, 0x00]
|
||||
})
|
||||
bme = bme280.Bme280()
|
||||
self.assertTrue(bme.is_status_measuring())
|
||||
self.assertFalse(bme.is_status_measuring())
|
||||
self.assertTrue(bme.is_status_image_register_updating())
|
||||
self.assertFalse(bme.is_status_image_register_updating())
|
||||
self.assertMatchSnapshot(mockbus._log)
|
||||
|
||||
def test_oversampling(self):
|
||||
mockbus = setup_mockbus(initial_read={
|
||||
0x76: calibration_read()
|
||||
})
|
||||
bme = bme280.Bme280()
|
||||
self.assertEqual(bme.get_humidity_oversampling(), bme280.HO_1)
|
||||
bme.set_humidity_oversampling(bme280.HO_16)
|
||||
self.assertEqual(bme.get_humidity_oversampling(), bme280.HO_16)
|
||||
self.assertEqual(bme.get_temperature_oversampling(), bme280.TO_1)
|
||||
bme.set_temperature_oversampling(bme280.TO_16)
|
||||
self.assertEqual(bme.get_temperature_oversampling(), bme280.TO_16)
|
||||
self.assertEqual(bme.get_pressure_oversampling(), bme280.PO_1)
|
||||
bme.set_pressure_oversampling(bme280.PO_16)
|
||||
self.assertEqual(bme.get_pressure_oversampling(), bme280.PO_16)
|
||||
self.assertMatchSnapshot(mockbus._log)
|
||||
|
||||
def test_mode(self):
|
||||
mockbus = setup_mockbus(initial_read={
|
||||
0x76: calibration_read()
|
||||
})
|
||||
bme = bme280.Bme280()
|
||||
self.assertEqual(bme.get_mode(), bme280.MODE_SLEEP)
|
||||
bme.set_mode(bme280.MODE_FORCED)
|
||||
self.assertEqual(bme.get_mode(), bme280.MODE_FORCED)
|
||||
self.assertMatchSnapshot(mockbus._log)
|
||||
|
||||
def test_config(self):
|
||||
mockbus = setup_mockbus(initial_read={
|
||||
0x76: calibration_read()
|
||||
})
|
||||
bme = bme280.Bme280()
|
||||
self.assertEqual(bme.get_tstandy(), bme280.TSTANDBY_1000)
|
||||
bme.set_tstandy(bme280.TSTANDBY_20)
|
||||
self.assertEqual(bme.get_tstandy(), bme280.TSTANDBY_20)
|
||||
self.assertEqual(bme.get_filter(), bme280.FILTER_OFF)
|
||||
bme.set_filter(bme280.FILTER_16)
|
||||
self.assertEqual(bme.get_filter(), bme280.FILTER_16)
|
||||
self.assertMatchSnapshot(mockbus._log)
|
||||
|
||||
def test_data(self):
|
||||
mockbus = setup_mockbus(initial_read={
|
||||
0x76: calibration_read() + [0x00] * 8
|
||||
})
|
||||
bme = bme280.Bme280()
|
||||
bme.set_mode(bme280.MODE_FORCED)
|
||||
t, p, h = bme.get_data()
|
||||
# self.assertEqual(t, 0x00)
|
||||
self.assertMatchSnapshot(mockbus._log)
|
0
tests/snapshots/__init__.py
Normal file
0
tests/snapshots/__init__.py
Normal file
3052
tests/snapshots/snap_bme280_test.py
Normal file
3052
tests/snapshots/snap_bme280_test.py
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user