Skip to content

Commit

Permalink
Simplified device specific testing, added documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfjlaros committed Mar 4, 2021
1 parent 2e19057 commit dcc2a25
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 47 deletions.
39 changes: 38 additions & 1 deletion docs/install.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Installation
============

The software is distributed via PyPI_, it can be installed with ``pip``:
The software is distributed via PyPI_, it can be installed with ``pip``.

::

Expand All @@ -20,6 +20,43 @@ the following commands.
cd arduino-simple-rpc
pip install .

Development
~~~~~~~~~~~

Tests are written in the pytest_ framework which can be installed with ``pip``.

::

pip install pytest

To run the automated tests, run ``py.test`` in the root of the project folder.

By default, all tests that rely on particular hardware to be connected are
disabled. The ``--device`` parameter can be used to enable these device
specific tests.

To test the Bluetooth_ interface.

::

py.test --device bt

To test the HardwareSerial_ interface.

::

py.test --device serial

To test the WiFi_ interface.

::

py.test --device wifi


.. _PyPI: https://pypi.org/project/arduino-simple-rpc
.. _GitHub: https://github.com/jfjlaros/arduino-simple-rpc.git
.. _pytest: https://docs.pytest.org/en/stable/index.html
.. _Bluetooth: https://github.com/jfjlaros/simpleRPC/tree/master/examples/bluetooth
.. _HardwareSerial: https://github.com/jfjlaros/simpleRPC/tree/master/examples/hardwareserial
.. _WiFi: https://github.com/jfjlaros/simpleRPC/tree/master/examples/esp32
4 changes: 4 additions & 0 deletions tests/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
_devices = {
'serial': '/dev/ttyACM0',
'wifi': 'socket://192.168.21.53:1025',
'bt': '/dev/rfcomm0'}
1 change: 0 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from pytest import fixture, skip


def pytest_addoption(parser: object) -> None:
parser.addoption('--device', type=str, default='')

Expand Down
7 changes: 3 additions & 4 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from simple_rpc.cli import _describe_method, rpc_call, rpc_list
from simple_rpc.extras import json_utf8_decode, json_utf8_encode


_device = '/dev/ttyACM0'
from conf import _devices


def test_json_utf8_encode() -> None:
Expand Down Expand Up @@ -37,13 +36,13 @@ def test_describe_method() -> None:
def test_rpc_list() -> None:
handle = StringIO()

rpc_list(handle, _device, 9600, 1)
rpc_list(handle, _devices['serial'], 9600, 1)
assert 'ping data\n Echo a value.\n' in handle.getvalue()


@mark.test_device('serial')
def test_rpc_call() -> None:
handle = StringIO()

rpc_call(handle, _device, 9600, 1, 'ping', ['10'])
rpc_call(handle, _devices['serial'], 9600, 1, 'ping', ['10'])
assert handle.getvalue() == '10\n'
74 changes: 33 additions & 41 deletions tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,79 +5,71 @@
from simple_rpc import Interface
from simple_rpc.simple_rpc import _version

from conf import _devices

class _TestLib(object):

class _TestDevice(object):
def test_pre_open(self: object) -> None:
assert not _interface.is_open()
assert _interface.methods == {}
assert not self._interface.is_open()
assert self._interface.methods == {}

def test_open(self: object) -> None:
_interface.open()
assert _interface.is_open()
assert _interface.methods != {}
self._interface.open()
assert self._interface.is_open()
assert self._interface.methods != {}

def test_version(self: object) -> None:
assert _interface._version == _version
assert self._interface._version == _version

def test_ping(self: object) -> None:
assert _interface.ping(3) == 3
assert self._interface.ping(3) == 3

def test_type_1(self: object) -> None:
assert _interface.methods['ping']['return']['typename'] == 'int'
assert self._interface.methods['ping']['return']['typename'] == 'int'

def test_fmt_1(self: object) -> None:
assert _interface.methods['ping']['return']['fmt'] == b'B'
assert self._interface.methods['ping']['return']['fmt'] == b'B'

def test_param_1(self: object) -> None:
assert _interface.methods['ping']['parameters'][0]['typename'] == 'int'
assert (
self._interface.methods['ping']['parameters'][0]['typename'] ==
'int')

def test_param_2(self: object) -> None:
assert _interface.methods['ping']['parameters'][0]['fmt'] == b'B'
assert self._interface.methods['ping']['parameters'][0]['fmt'] == b'B'

def test_param_3(self: object) -> None:
assert _interface.methods['ping']['parameters'][0]['name'] == 'data'
assert (
self._interface.methods['ping']['parameters'][0]['name'] == 'data')

def test_doc_1(self: object) -> None:
assert _interface.methods['ping']['doc'] == 'Echo a value.'
assert self._interface.methods['ping']['doc'] == 'Echo a value.'

def test_doc_2(self: object) -> None:
assert _interface.methods['ping']['parameters'][0]['doc'] == 'Value.'
assert (
self._interface.methods['ping']['parameters'][0]['doc'] ==
'Value.')

def test_close(self: object) -> None:
assert _interface.is_open()
assert _interface.methods != {}
_interface.close()
assert self._interface.is_open()
assert self._interface.methods != {}
self._interface.close()

def test_post_close(self: object) -> None:
assert not _interface.is_open()
assert _interface.methods == {}


def test_init_serial() -> None:
global _interface
_interface = Interface('/dev/ttyACM0', autoconnect=False)
assert not self._interface.is_open()
assert self._interface.methods == {}


@mark.test_device('serial')
class TestSerial(_TestLib):
pass


def test_init_wifi() -> None:
global _interface
_interface = Interface('socket://192.168.21.53:1025', autoconnect=False)
class TestSerial(_TestDevice):
_interface = Interface(_devices['serial'], autoconnect=False)


@mark.test_device('wifi')
class TestWiFi(_TestLib):
pass


def test_init_bluetooth() -> None:
global _interface
_interface = Interface('/dev/rfcomm0', autoconnect=False)
class TestWiFi(_TestDevice):
_interface = Interface(_devices['wifi'], autoconnect=False)


@mark.test_device('bt')
class TestBluetooth(_TestLib):
pass
class TestBluetooth(_TestDevice):
_interface = Interface(_devices['bt'], autoconnect=False)

0 comments on commit dcc2a25

Please sign in to comment.