Skip to content

Commit

Permalink
Migrate to pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
Sigmanificient committed Jul 28, 2024
1 parent 70c6868 commit da92182
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 97 deletions.
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
flake8==2.1.0
pylint==1.2.1
coverage==5.5
nose==1.3.7
mock==3.0.5
funcsigs==1.0.2
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
long_description=readme + "\n\n" + changelog,
url="https://github.com/peplin/pygatt",
install_requires=["pyserial", "enum-compat"],
setup_requires=["coverage == 5.5", "nose == 1.3.7"],
setup_requires=["coverage == 5.5", "pytest"],
extras_require={
"GATTTOOL": ["pexpect"],
},
Expand Down
29 changes: 14 additions & 15 deletions tests/bgapi/test_bgapi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import print_function

from nose.tools import eq_, ok_
import mock
import unittest

Expand Down Expand Up @@ -60,7 +59,7 @@ def test_connect(self):
def test_connect_already_connected(self):
device = self._connect()
another_device = self.backend.connect(self.address_string)
eq_(device, another_device)
assert device == another_device

def test_serial_port_connection_failure(self):
self.mock_device.mocked_serial.read = mock.MagicMock()
Expand Down Expand Up @@ -99,8 +98,8 @@ def test_scan_and_get_devices_discovered(self):
})
self.mock_device.stage_scan_packets(scan_responses=scan_responses)
devs = self.backend.scan(timeout=.5)
eq_('Hello!', devs[0]['name'])
eq_(-80, devs[0]['rssi'])
assert 'Hello!' == devs[0]['name']
assert -80 == devs[0]['rssi']

def test_clear_bonds(self):
# Test delete stored bonds
Expand All @@ -120,34 +119,34 @@ class UsbInfoStringParsingTests(unittest.TestCase):

def test_weird_platform(self):
vid, pid = extract_vid_pid("USB VID_2458 PID_0001")
eq_(0x2458, vid)
eq_(1, pid)
assert 0x2458 == vid
assert 1 == pid

def test_linux(self):
vid, pid = extract_vid_pid("USB VID:PID=2458:0001 SNR=1")
eq_(0x2458, vid)
eq_(1, pid)
assert 0x2458 == vid
assert 1 == pid

def test_mac(self):
vid, pid = extract_vid_pid("USB VID:PID=2458:1 SNR=1")
eq_(0x2458, vid)
eq_(1, pid)
assert 0x2458 == vid
assert 1 == pid

def test_invalid(self):
eq_(None, extract_vid_pid("2458:1"))
assert extract_vid_pid("2458:1") is None


class ReturnCodeTests(unittest.TestCase):

def test_unrecognized_return_code(self):
ok_(get_return_message(123123123123123) is not None)
assert get_return_message(123123123123123) is not None


class BGAPIAddressToHexTests(unittest.TestCase):

def test_convert(self):
bgapi_address = bytearray([21, 19, 11, 210, 2, 97])
eq_("61:02:D2:0B:13:15", bgapi_address_to_hex(bgapi_address))
assert "61:02:D2:0B:13:15" == bgapi_address_to_hex(bgapi_address)


class DecodePacketTests(unittest.TestCase):
Expand All @@ -161,8 +160,8 @@ def test_decode_scan_packet(self):
135, 0, 76, 200, 60, 78]

packet_type, packet = self.lib.decode_packet(data)
eq_(bglib.EventPacketType.gap_scan_response, packet_type)
eq_(bytearray([21, 19, 11, 210, 2, 97]), packet['sender'])
assert bglib.EventPacketType.gap_scan_response == packet_type
assert bytearray([21, 19, 11, 210, 2, 97]) == packet['sender']

def test_decode_invalid(self):
with self.assertRaises(bglib.UnknownMessageType):
Expand Down
26 changes: 13 additions & 13 deletions tests/bgapi/test_device.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import print_function

from nose.tools import eq_, raises
import mock
import pytest
import unittest
from uuid import UUID

Expand Down Expand Up @@ -70,7 +70,7 @@ def test_char_read(self):
self.mock_device.stage_char_read_packets(
handle_char, 0x00, expected_value)
value = device.char_read(UUID(uuid_char))
eq_(bytearray(expected_value), value)
assert bytearray(expected_value) == value

# Test ignore of packet with wrong handle
expected_value = [0xBE, 0xEF, 0x15, 0xF0, 0x0D]
Expand All @@ -79,7 +79,7 @@ def test_char_read(self):
self.mock_device.stage_char_read_packets(
handle_char, 0x00, expected_value)
value = device.char_read(UUID(uuid_char))
eq_(bytearray(expected_value), value)
assert bytearray(expected_value) == value

def test_read_nonstandard_4byte_char(self):
device = self._connect()
Expand All @@ -95,9 +95,8 @@ def test_read_nonstandard_4byte_char(self):
self.mock_device.stage_char_read_packets(
handle_char, 0x00, expected_value)
value = device.char_read(UUID(str(uuid16_to_uuid(uuid_char))))
eq_(bytearray(expected_value), value)
assert bytearray(expected_value) == value

@raises(ExpectedResponseTimeout)
def test_read_timeout_wrong_handle(self):
device = self._connect()
uuid_char = '01234567-0123-0123-0123-0123456789AB'
Expand All @@ -108,10 +107,11 @@ def test_read_timeout_wrong_handle(self):
uuid_char, handle_char,
uuid_desc, handle_desc])
# Test char_read
expected_value = [0xBE, 0xEF, 0x15, 0xF0, 0x0D]
self.mock_device.stage_char_read_packets(
0, 0x00, expected_value)
device.char_read(UUID(uuid_char))
with pytest.raises(ExpectedResponseTimeout):
expected_value = [0xBE, 0xEF, 0x15, 0xF0, 0x0D]
self.mock_device.stage_char_read_packets(
0, 0x00, expected_value)
device.char_read(UUID(uuid_char))

def test_char_write(self):
device = self._connect()
Expand Down Expand Up @@ -142,7 +142,7 @@ def test_get_rssi(self):
device = self._connect()
# Test get_rssi
self.mock_device.stage_get_rssi_packets()
eq_(-80, device.get_rssi())
assert -80 == device.get_rssi()

def test_discover_characteristics(self):
device = self._connect()
Expand All @@ -156,6 +156,6 @@ def test_discover_characteristics(self):
str(uuid_char), handle_char,
uuid_desc, handle_desc])
characteristics = device.discover_characteristics()
eq_(characteristics[uuid_char].handle, handle_char)
eq_(characteristics[uuid_char].descriptors[uuid16_to_uuid(0x2902)],
handle_desc)
assert characteristics[uuid_char].handle == handle_char
assert (characteristics[uuid_char].descriptors[uuid16_to_uuid(0x2902)]
== handle_desc)
58 changes: 26 additions & 32 deletions tests/gatttool/test_backend.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import print_function

from nose.tools import eq_, ok_
from mock import patch, MagicMock

import pexpect
Expand Down Expand Up @@ -45,13 +44,13 @@ def tearDown(self):
def test_scan(self):
# TODO mock a successful scan
devices = self.backend.scan()
ok_(devices is not None)
eq_(0, len(devices))
assert devices is not None
assert 0 == len(devices)

def test_connect(self):
address = "11:22:33:44:55:66"
device = self.backend.connect(address)
ok_(device is not None)
assert device is not None

def test_disconnect_callback(self):
# Just keep saying we got the "Disconnected" response
Expand All @@ -64,23 +63,18 @@ def rate_limited_expect_d(*args, **kwargs):
address = "11:22:33:44:55:66"
device = self.backend.connect(address)
device.register_disconnect_callback(mock_callback)
eq_(mock_callback in
device._backend._receiver._event_vector[
"disconnected"]["callback"],
True)
assert (mock_callback in device._backend._receiver._event_vector[
"disconnected"]["callback"])

self.spawn.return_value.expect.side_effect = rate_limited_expect_d
time.sleep(0.1)
ok_(mock_callback.called)
assert mock_callback.called

device.remove_disconnect_callback(mock_callback)
eq_(mock_callback not in
device._backend._receiver._event_vector[
"disconnected"]["callback"],
True)
eq_(len(device._backend._receiver._event_vector[
"disconnected"]["callback"]) > 0,
True)
assert (mock_callback not in device._backend._receiver._event_vector[
"disconnected"]["callback"])
assert (len(device._backend._receiver._event_vector[
"disconnected"]["callback"]) > 0)

def test_auto_reconnect_call(self):
# Just keep saying we got the "Disconnected" response
Expand All @@ -94,7 +88,7 @@ def rate_limited_expect_d(*args, **kwargs):
device._backend.reconnect = MagicMock()
self.spawn.return_value.expect.side_effect = rate_limited_expect_d
time.sleep(0.1)
ok_(device._backend.reconnect.called)
assert device._backend.reconnect.called

def test_no_reconnect_default(self):
# Just keep saying we got the "Disconnected" response
Expand All @@ -108,7 +102,7 @@ def rate_limited_expect_d(*args, **kwargs):
device._backend.reconnect = MagicMock()
self.spawn.return_value.expect.side_effect = rate_limited_expect_d
time.sleep(0.1)
ok_(not device._backend.reconnect.called)
assert not device._backend.reconnect.called

def test_no_reconnect_disconnect(self):
# Just keep saying we got the "Disconnected" response
Expand All @@ -123,7 +117,7 @@ def rate_limited_expect_d(*args, **kwargs):
device.disconnect()
self.spawn.return_value.expect.side_effect = rate_limited_expect_d
time.sleep(0.1)
ok_(not device._backend.reconnect.called)
assert not device._backend.reconnect.called

def test_auto_reconnect(self):
# Just keep saying we got the "Disconnected" response
Expand All @@ -145,7 +139,7 @@ def rate_limited_expect_c(*args, **kwargs):
device.resubscribe_all = MagicMock()
self.spawn.return_value.expect.side_effect = rate_limited_expect_c
time.sleep(0.1)
ok_(device.resubscribe_all.called)
assert device.resubscribe_all.called

def test_single_byte_notification(self):
event = {
Expand All @@ -155,9 +149,9 @@ def test_single_byte_notification(self):
device = self.backend.connect(address)
device.receive_notification = MagicMock()
device._backend._handle_notification_string(event)
ok_(device.receive_notification.called)
eq_(0x24, device.receive_notification.call_args[0][0])
eq_(bytearray([0x64]), device.receive_notification.call_args[0][1])
assert device.receive_notification.called
assert 0x24 == device.receive_notification.call_args[0][0]
assert bytearray([0x64]) == device.receive_notification.call_args[0][1]

def test_multi_byte_notification(self):
event = {
Expand All @@ -168,10 +162,10 @@ def test_multi_byte_notification(self):
device = self.backend.connect(address)
device.receive_notification = MagicMock()
device._backend._handle_notification_string(event)
ok_(device.receive_notification.called)
eq_(0x24, device.receive_notification.call_args[0][0])
eq_(bytearray([0x64, 0x46, 0x72]),
device.receive_notification.call_args[0][1])
assert device.receive_notification.called
assert 0x24 == device.receive_notification.call_args[0][0]
assert (bytearray([0x64, 0x46, 0x72])
== device.receive_notification.call_args[0][1])

def test_empty_notification(self):
event = {
Expand All @@ -181,7 +175,7 @@ def test_empty_notification(self):
device = self.backend.connect(address)
device.receive_notification = MagicMock()
device._backend._handle_notification_string(event)
ok_(device.receive_notification.called)
assert device.receive_notification.called

def test_malformed_notification(self):
event = {
Expand All @@ -191,7 +185,7 @@ def test_malformed_notification(self):
device = self.backend.connect(address)
device.receive_notification = MagicMock()
device._backend._handle_notification_string(event)
ok_(not device.receive_notification.called)
assert not device.receive_notification.called

def test_indication(self):
event = {
Expand All @@ -201,6 +195,6 @@ def test_indication(self):
device = self.backend.connect(address)
device.receive_notification = MagicMock()
device._backend._handle_notification_string(event)
ok_(device.receive_notification.called)
eq_(0x24, device.receive_notification.call_args[0][0])
eq_(bytearray([0x64]), device.receive_notification.call_args[0][1])
assert device.receive_notification.called
assert 0x24 == device.receive_notification.call_args[0][0]
assert bytearray([0x64]) == device.receive_notification.call_args[0][1]
Loading

0 comments on commit da92182

Please sign in to comment.