Skip to content

Commit

Permalink
Convert tests to pytests (#1060)
Browse files Browse the repository at this point in the history
* Convert tests to pytests

* cleanup

* args

* clean

* linter

---------

Co-authored-by: Maxime Desroches <[email protected]>
  • Loading branch information
UkuLoskit and maxime-desroches committed Jul 9, 2024
1 parent de39b14 commit 5ba7870
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 119 deletions.
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 logical"

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()
37 changes: 14 additions & 23 deletions can/tests/test_define.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
#!/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':
{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'}
}
)

def test_all_dbcs(self):
assert defs.dv[399] == defs.dv['STEER_STATUS']
assert defs.dv[399] == {'STEER_STATUS':
{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'}
}

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

0 comments on commit 5ba7870

Please sign in to comment.