diff --git a/requirements.txt b/requirements.txt index 42182d0e..735bc388 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/setup.py b/setup.py index f0c08790..bd8a928d 100644 --- a/setup.py +++ b/setup.py @@ -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"], }, diff --git a/tests/bgapi/test_bgapi.py b/tests/bgapi/test_bgapi.py index f7c639b5..d12fc978 100644 --- a/tests/bgapi/test_bgapi.py +++ b/tests/bgapi/test_bgapi.py @@ -1,6 +1,5 @@ from __future__ import print_function -from nose.tools import eq_, ok_ import mock import unittest @@ -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() @@ -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 @@ -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): @@ -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): diff --git a/tests/bgapi/test_device.py b/tests/bgapi/test_device.py index 548175fd..324f0657 100644 --- a/tests/bgapi/test_device.py +++ b/tests/bgapi/test_device.py @@ -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 @@ -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] @@ -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() @@ -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' @@ -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() @@ -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() @@ -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) diff --git a/tests/gatttool/test_backend.py b/tests/gatttool/test_backend.py index 9454f803..4a07cde2 100644 --- a/tests/gatttool/test_backend.py +++ b/tests/gatttool/test_backend.py @@ -1,6 +1,5 @@ from __future__ import print_function -from nose.tools import eq_, ok_ from mock import patch, MagicMock import pexpect @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 = { @@ -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 = { @@ -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 = { @@ -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 = { @@ -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 = { @@ -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] diff --git a/tests/gatttool/test_device.py b/tests/gatttool/test_device.py index 3862cdcc..bd706fac 100644 --- a/tests/gatttool/test_device.py +++ b/tests/gatttool/test_device.py @@ -1,7 +1,7 @@ +import pytest import unittest import uuid from mock import MagicMock, patch -from nose.tools import ok_, eq_, raises from pygatt.exceptions import NotConnectedError from pygatt.backends import Characteristic @@ -23,8 +23,8 @@ def setUp(self): def test_bond(self): self.device.bond() - ok_(self.backend.bond.called) - eq_(self.device, self.backend.bond.call_args[0][0]) + assert self.backend.bond.called + assert self.device == self.backend.bond.call_args[0][0] def test_char_read(self): expected_value = bytearray(range(4)) @@ -33,11 +33,11 @@ def test_char_read(self): ) as get_handle: char_uuid = uuid.uuid4() value = self.device.char_read(char_uuid) - ok_(not get_handle.called) - ok_(self.backend.char_read.called) - eq_(self.device, self.backend.char_read.call_args[0][0]) - eq_(char_uuid, self.backend.char_read.call_args[0][1]) - eq_(expected_value, value) + assert not get_handle.called + assert self.backend.char_read.called + assert self.device == self.backend.char_read.call_args[0][0] + assert char_uuid == self.backend.char_read.call_args[0][1] + assert expected_value == value def test_char_write(self): with patch.object(self.device, 'get_handle', return_value=24 @@ -45,17 +45,17 @@ def test_char_write(self): char_uuid = uuid.uuid4() value = bytearray(range(4)) self.device.char_write(char_uuid, value) - ok_(get_handle.called) - eq_(char_uuid, get_handle.call_args[0][0]) - ok_(self.backend.char_write_handle.called) - eq_(self.device, self.backend.char_write_handle.call_args[0][0]) - eq_(24, self.backend.char_write_handle.call_args[0][1]) - eq_(value, self.backend.char_write_handle.call_args[0][2]) + assert get_handle.called + assert char_uuid == get_handle.call_args[0][0] + assert self.backend.char_write_handle.called + assert self.device == self.backend.char_write_handle.call_args[0][0] + assert 24 == self.backend.char_write_handle.call_args[0][1] + assert value == self.backend.char_write_handle.call_args[0][2] def test_disconnect(self): self.device.disconnect() - ok_(self.backend.disconnect.called) - eq_(self.device, self.backend.disconnect.call_args[0][0]) + assert self.backend.disconnect.called + assert self.device == self.backend.disconnect.call_args[0][0] def test_additional_disconnect_callback(self): mock_callback = MagicMock() @@ -66,20 +66,20 @@ def test_additional_disconnect_callback(self): self.backend._receiver.remove_callback.assert_called_with( "disconnected", mock_callback) - @raises(NotConnectedError) def test_write_after_disconnect(self): - self.device.disconnect() - self.device.char_read(uuid.uuid4()) + with pytest.raises(NotConnectedError): + self.device.disconnect() + self.device.char_read(uuid.uuid4()) def test_get_handle(self): handle = self.device.get_handle(self.char_uuid) - ok_(self.backend.discover_characteristics.called) - eq_(self.device, self.backend.discover_characteristics.call_args[0][0]) - eq_(self.expected_handle, handle) + assert self.backend.discover_characteristics.called + assert self.device == self.backend.discover_characteristics.call_args[0][0] + assert self.expected_handle == handle def test_get_cached_handle(self): handle = self.device.get_handle(self.char_uuid) with patch.object(self.backend, 'discover_characteristics') as discover: next_handle = self.device.get_handle(self.char_uuid) - eq_(handle, next_handle) - ok_(not discover.called) + assert handle == next_handle + assert not discover.called diff --git a/tests/test_device.py b/tests/test_device.py index 4b3bede9..75087a4a 100644 --- a/tests/test_device.py +++ b/tests/test_device.py @@ -1,7 +1,6 @@ import unittest import uuid from mock import MagicMock, patch -from nose.tools import ok_, eq_ from pygatt import BLEDevice from pygatt.backends import Characteristic @@ -28,17 +27,17 @@ def _subscribe(self): callback = MagicMock() with patch.object(self.device, 'char_write_handle') as char_write: self.device.subscribe(self.device.CHAR_UUID, callback=callback) - ok_(char_write.called) - eq_(self.device.EXPECTED_HANDLE + 1, char_write.call_args[0][0]) - eq_(bytearray([1, 0]), char_write.call_args[0][1]) + assert char_write.called + assert self.device.EXPECTED_HANDLE + 1 == char_write.call_args[0][0] + assert bytearray([1, 0]) == char_write.call_args[0][1] return callback def _unsubscribe(self): callback = MagicMock() with patch.object(self.device, 'char_write_handle') as char_write: self.device.unsubscribe(self.device.CHAR_UUID) - eq_(self.device.EXPECTED_HANDLE + 1, char_write.call_args[0][0]) - eq_(bytearray([0, 0]), char_write.call_args[0][1]) + assert self.device.EXPECTED_HANDLE + 1 == char_write.call_args[0][0] + assert bytearray([0, 0]) == char_write.call_args[0][1] return callback def test_subscribe(self): @@ -54,22 +53,22 @@ def test_subscribe_another_callback(self): with patch.object(self.device, 'char_write_handle') as char_write: self.device.subscribe(self.device.CHAR_UUID, callback=another_callback) - ok_(not char_write.called) + assert not char_write.called def test_receive_notification(self): callback = self._subscribe() value = bytearray([24]) self.device.receive_notification(TestBLEDevice.EXPECTED_HANDLE, value) - ok_(callback.called) - eq_(TestBLEDevice.EXPECTED_HANDLE, callback.call_args[0][0]) - eq_(value, callback.call_args[0][1]) + assert callback.called + assert TestBLEDevice.EXPECTED_HANDLE == callback.call_args[0][0] + assert value == callback.call_args[0][1] def test_ignore_notification_for_another_handle(self): callback = self._subscribe() value = bytearray([24]) self.device.receive_notification( TestBLEDevice.EXPECTED_HANDLE + 1, value) - ok_(not callback.called) + assert not callback.called def test_unicode_get_handle(self): try: