Skip to content
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

Convert tests to pytests #1060

Merged
merged 5 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Build opendbc
run: ${{ env.RUN }} "cd ../ && scons -j$(nproc) --minimal"
- name: Unit tests
run: ${{ env.RUN }} "pytest"
run: ${{ env.RUN }} "pytest -n auto"
maxime-desroches marked this conversation as resolved.
Show resolved Hide resolved

static-analysis:
name: static analysis
Expand Down
13 changes: 3 additions & 10 deletions can/tests/test_checksums.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#!/usr/bin/env python3
import unittest

from opendbc.can.parser import CANParser
from opendbc.can.packer import CANPacker
from opendbc.can.tests.test_packer_parser import can_list_to_can_capnp


class TestCanChecksums(unittest.TestCase):
class TestCanChecksums:

def test_honda_checksum(self):
"""Test checksums for Honda standard and extended CAN ids"""
Expand Down Expand Up @@ -34,9 +31,5 @@ def test_honda_checksum(self):
can_strings = [can_list_to_can_capnp(msgs), ]
parser.update_strings(can_strings)

self.assertEqual(parser.vl['LKAS_HUD']['CHECKSUM'], std)
self.assertEqual(parser.vl['LKAS_HUD_A']['CHECKSUM'], ext)


if __name__ == "__main__":
unittest.main()
assert parser.vl['LKAS_HUD']['CHECKSUM'] == std
assert parser.vl['LKAS_HUD_A']['CHECKSUM'] == ext
20 changes: 7 additions & 13 deletions can/tests/test_dbc_exceptions.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
#!/usr/bin/env python3

import unittest
import pytest

from opendbc.can.parser import CANParser, CANDefine
from opendbc.can.packer import CANPacker
from opendbc.can.tests import TEST_DBC


class TestCanParserPackerExceptions(unittest.TestCase):
class TestCanParserPackerExceptions:
def test_civic_exceptions(self):
dbc_file = "honda_civic_touring_2016_can_generated"
dbc_invalid = dbc_file + "abcdef"
msgs = [("STEERING_CONTROL", 50)]
with self.assertRaises(RuntimeError):
with pytest.raises(RuntimeError):
CANParser(dbc_invalid, msgs, 0)
with self.assertRaises(RuntimeError):
with pytest.raises(RuntimeError):
CANPacker(dbc_invalid)
with self.assertRaises(RuntimeError):
with pytest.raises(RuntimeError):
CANDefine(dbc_invalid)
with self.assertRaises(KeyError):
with pytest.raises(KeyError):
CANDefine(TEST_DBC)

parser = CANParser(dbc_file, msgs, 0)
with self.assertRaises(RuntimeError):
with pytest.raises(RuntimeError):
parser.update_strings([b''])

# Everything is supposed to work below
CANParser(dbc_file, msgs, 0)
CANParser(dbc_file, [], 0)
CANPacker(dbc_file)
CANDefine(dbc_file)


if __name__ == "__main__":
unittest.main()
15 changes: 4 additions & 11 deletions can/tests/test_dbc_parser.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#!/usr/bin/env python3
import unittest

from opendbc.can.parser import CANParser
from opendbc.can.tests import ALL_DBCS


class TestDBCParser(unittest.TestCase):
class TestDBCParser:
def test_enough_dbcs(self):
# sanity check that we're running on the real DBCs
self.assertGreater(len(ALL_DBCS), 20)
assert len(ALL_DBCS) > 20

def test_parse_all_dbcs(self):
def test_parse_all_dbcs(self, subtests):
"""
Dynamic DBC parser checks:
- Checksum and counter length, start bit, endianness
Expand All @@ -20,9 +17,5 @@ def test_parse_all_dbcs(self):
"""

for dbc in ALL_DBCS:
with self.subTest(dbc=dbc):
with subtests.test(dbc=dbc):
CANParser(dbc, [], 0)


if __name__ == "__main__":
unittest.main()
22 changes: 7 additions & 15 deletions can/tests/test_define.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
#!/usr/bin/env python3
import unittest

from opendbc.can.can_define import CANDefine
from opendbc.can.tests import ALL_DBCS


class TestCADNDefine(unittest.TestCase):
class TestCADNDefine:
def test_civic(self):

dbc_file = "honda_civic_touring_2016_can_generated"
defs = CANDefine(dbc_file)

self.assertDictEqual(defs.dv[399], defs.dv['STEER_STATUS'])
self.assertDictEqual(defs.dv[399],
{'STEER_STATUS':
assert defs.dv[399] == defs.dv['STEER_STATUS']
assert defs.dv[399] == \
maxime-desroches marked this conversation as resolved.
Show resolved Hide resolved
{'STEER_STATUS': \
maxime-desroches marked this conversation as resolved.
Show resolved Hide resolved
{7: 'PERMANENT_FAULT',
6: 'TMP_FAULT',
5: 'FAULT_1',
4: 'NO_TORQUE_ALERT_2',
3: 'LOW_SPEED_LOCKOUT',
2: 'NO_TORQUE_ALERT_1',
0: 'NORMAL'}
0: 'NORMAL'} \
maxime-desroches marked this conversation as resolved.
Show resolved Hide resolved
}
)

def test_all_dbcs(self):
def test_all_dbcs(self, subtests):
# Asserts no exceptions on all DBCs
for dbc in ALL_DBCS:
with self.subTest(dbc=dbc):
with subtests.test(dbc=dbc):
CANDefine(dbc)


if __name__ == "__main__":
unittest.main()
Loading