Skip to content

Modernize all the test cases #596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@
}

tests_require = [
'mock~=2.0',
'pytest~=4.3',
'pytest-timeout~=1.3',
'pytest-cov~=2.6',
'codecov~=2.0',
'future',
'six',
'hypothesis'
] + extras_require['serial']
Expand Down
2 changes: 0 additions & 2 deletions test/back2back_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
This module tests two virtual buses attached to each other.
"""

from __future__ import absolute_import, print_function
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 we are in the future.


import sys
import unittest
from time import sleep
Expand Down
4 changes: 1 addition & 3 deletions test/listener_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
"""
"""

from __future__ import absolute_import, print_function

from time import sleep
import unittest
import random
Expand Down Expand Up @@ -117,7 +115,7 @@ def test_filetype_to_instance(extension, klass):
test_filetype_to_instance(".log", can.CanutilsLogReader)

# test file extensions that are not supported
with self.assertRaisesRegexp(NotImplementedError, ".xyz_42"):
with self.assertRaisesRegex(NotImplementedError, ".xyz_42"):
test_filetype_to_instance(".xyz_42", can.Printer)

def testLoggerTypeResolution(self):
Expand Down
14 changes: 2 additions & 12 deletions test/logformats_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,12 @@
TODO: implement CAN FD support testing
"""

from __future__ import print_function, absolute_import, division

import logging
import unittest
import tempfile
import os
from abc import abstractmethod, ABCMeta

try:
# Python 3
from itertools import zip_longest
except ImportError:
# Python 2
from itertools import izip_longest as zip_longest
from itertools import zip_longest

import can

Expand All @@ -39,7 +31,7 @@
logging.basicConfig(level=logging.DEBUG)


class ReaderWriterTest(unittest.TestCase, ComparingMessagesTestCase):
class ReaderWriterTest(unittest.TestCase, ComparingMessagesTestCase, metaclass=ABCMeta):
"""Tests a pair of writer and reader by writing all data first and
then reading all data and checking if they could be reconstructed
correctly. Optionally writes some comments as well.
Expand All @@ -50,8 +42,6 @@ class ReaderWriterTest(unittest.TestCase, ComparingMessagesTestCase):
(Source: `*Wojciech B.* on StackOverlfow <https://stackoverflow.com/a/22836015/3753684>`_)
"""

__metaclass__ = ABCMeta

def __init__(self, *args, **kwargs):
unittest.TestCase.__init__(self, *args, **kwargs)
self._setup_instance()
Expand Down
4 changes: 1 addition & 3 deletions test/message_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
This module contains a helper for writing test cases that need to compare messages.
"""

from __future__ import absolute_import, print_function

from copy import copy


class ComparingMessagesTestCase(object):
class ComparingMessagesTestCase:
"""
Must be extended by a class also extending a unittest.TestCase.

Expand Down
8 changes: 2 additions & 6 deletions test/network_test.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
#!/usr/bin/env python
# coding: utf-8

from __future__ import print_function

import unittest
import threading
try:
import queue
except ImportError:
import Queue as queue
import queue
import random

import logging

logging.getLogger(__file__).setLevel(logging.WARNING)

# make a random bool:
Expand Down
7 changes: 1 addition & 6 deletions test/notifier_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@

import unittest
import time
try:
import asyncio
except ImportError:
asyncio = None
import asyncio

import can

Expand Down Expand Up @@ -45,7 +42,6 @@ def test_multiple_bus(self):

class AsyncNotifierTest(unittest.TestCase):

@unittest.skipIf(asyncio is None, 'Test requires asyncio')
def test_asyncio_notifier(self):
loop = asyncio.get_event_loop()
bus = can.Bus('test', bustype='virtual', receive_own_messages=True)
Expand All @@ -60,6 +56,5 @@ def test_asyncio_notifier(self):
bus.shutdown()



if __name__ == '__main__':
unittest.main()
6 changes: 2 additions & 4 deletions test/serial_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@
Copyright: 2017 Boris Wenzlaff
"""

from __future__ import division

import unittest
from mock import patch
from unittest.mock import patch

import can
from can.interfaces.serial.serial_can import SerialBus

from .message_helper import ComparingMessagesTestCase


class SerialDummy(object):
class SerialDummy:
"""
Dummy to mock the serial communication
"""
Expand Down
2 changes: 0 additions & 2 deletions test/simplecyclic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
This module tests cyclic send tasks.
"""

from __future__ import absolute_import

from time import sleep
import unittest
import gc
Expand Down
6 changes: 1 addition & 5 deletions test/test_detect_available_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
:meth:`can.BusABC.detect_available_configs`.
"""

from __future__ import absolute_import

import sys
import unittest
if sys.version_info.major > 2:
basestring = str

from can import detect_available_configs

Expand All @@ -33,7 +29,7 @@ def test_general_values(self):
for config in configs:
self.assertIn('interface', config)
self.assertIn('channel', config)
self.assertIsInstance(config['interface'], basestring)
self.assertIsInstance(config['interface'], str)

def test_content_virtual(self):
configs = detect_available_configs(interfaces='virtual')
Expand Down
5 changes: 1 addition & 4 deletions test/test_kvaser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import time
import logging
import unittest
try:
from unittest.mock import Mock, patch
except ImportError:
from mock import patch, Mock
from unittest.mock import Mock, patch

import pytest

Expand Down
1 change: 1 addition & 0 deletions test/test_load_file_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
# coding: utf-8

import shutil
import tempfile
import unittest
Expand Down
2 changes: 0 additions & 2 deletions test/test_message_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
This module tests :meth:`can.BusABC._matches_filters`.
"""

from __future__ import absolute_import

import unittest

from can import Bus, Message
Expand Down
2 changes: 0 additions & 2 deletions test/test_message_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
This module tests :class:`can.MessageSync`.
"""

from __future__ import absolute_import

from copy import copy
from time import time
import gc
Expand Down
6 changes: 1 addition & 5 deletions test/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
This module tests that the scripts are all callable.
"""

from __future__ import absolute_import

import subprocess
import unittest
import sys
Expand All @@ -16,9 +14,7 @@
from .config import *


class CanScriptTest(unittest.TestCase):

__metaclass__ = ABCMeta
class CanScriptTest(unittest.TestCase, metaclass=ABCMeta):

@classmethod
def setUpClass(cls):
Expand Down
1 change: 1 addition & 0 deletions test/test_slcan.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
# coding: utf-8

import unittest
import can

Expand Down
11 changes: 3 additions & 8 deletions test/test_socketcan.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
"""
import unittest

try:
from unittest.mock import Mock
from unittest.mock import patch
from unittest.mock import call
except ImportError:
from mock import Mock
from mock import patch
from mock import call
from unittest.mock import Mock
from unittest.mock import patch
from unittest.mock import call

import ctypes

Expand Down
4 changes: 1 addition & 3 deletions test/test_socketcan_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
Tests helpers in `can.interfaces.socketcan.socketcan_common`.
"""

from __future__ import absolute_import

import unittest

from can.interfaces.socketcan.utils import \
Expand Down Expand Up @@ -36,7 +34,7 @@ def test_find_available_interfaces(self):
result = list(find_available_interfaces())
self.assertGreaterEqual(len(result), 0)
for entry in result:
self.assertRegexpMatches(entry, r"v?can\d+")
self.assertRegex(entry, r"v?can\d+")
if TEST_INTERFACE_SOCKETCAN:
self.assertGreaterEqual(len(result), 1)
self.assertIn("vcan0", result)
Expand Down
5 changes: 1 addition & 4 deletions test/test_systec.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
# coding: utf-8

import unittest
try:
from unittest.mock import Mock, patch
except ImportError:
from mock import Mock, patch
from unittest.mock import Mock, patch

import can
from can.interfaces.systec import ucan, ucanbus
Expand Down
15 changes: 2 additions & 13 deletions test/test_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
# Web : http://www.lauszus.com
# e-mail : [email protected]

from __future__ import absolute_import

import argparse
import can
import curses
Expand All @@ -36,15 +34,8 @@
import unittest
import os
import six

from typing import Dict, Tuple, Union

try:
# noinspection PyCompatibility
from unittest.mock import Mock, patch
except ImportError:
# noinspection PyPackageRequirements
from mock import Mock, patch
from unittest.mock import Mock, patch

from can.viewer import KEY_ESC, KEY_SPACE, CanViewer, parse_args

Expand Down Expand Up @@ -337,9 +328,7 @@ def test_pack_unpack(self):
parsed_data = CanViewer.unpack_data(CANOPEN_TPDO4 + 2, data_structs, raw_data)
self.assertListEqual(parsed_data, [0xFFFFFF, 0xFFFFFFFF])

# See: http://python-future.org/compatible_idioms.html#long-integers
from past.builtins import long
self.assertTrue(all(isinstance(d, (int, long)) for d in parsed_data))
self.assertTrue(all(isinstance(d, int) for d in parsed_data))

# Make sure that the ValueError exception is raised
with self.assertRaises(ValueError):
Expand Down