Clean up test code

Separate/consolidate board initialization code, and a few functions
that are shared across tests. Update the Makefile accordingly.

Resolves #18
This commit is contained in:
2020-06-06 16:50:00 -07:00
parent 573504547c
commit 0ee9d39e81
19 changed files with 327 additions and 420 deletions

View File

@@ -103,28 +103,28 @@ def context_factory():
def test_meta_pass(context_factory, logger):
serial_dev, jlink = context_factory("Test/pass.bin")
serial_dev, jlink = context_factory("Test/Apps/pass.bin")
text = serial_dev.read_until(TEST_PASS_TEXT)
print("Got serial output: {}".format(text))
assert text.endswith(TEST_PASS_TEXT)
def test_meta_fail(context_factory, logger):
serial_dev, jlink = context_factory("Test/fail.bin")
serial_dev, jlink = context_factory("Test/Apps/fail.bin")
text = serial_dev.read_until(TEST_PASS_TEXT)
print("Got serial output: {}".format(text))
assert not text.endswith(TEST_PASS_TEXT)
def test_meta_timeout(context_factory, logger):
serial_dev, jlink = context_factory("Test/timeout.bin")
serial_dev, jlink = context_factory("Test/Apps/timeout.bin")
text = serial_dev.read_until(TEST_PASS_TEXT)
assert not text.endswith(TEST_PASS_TEXT)
def test_meta_nostart(context_factory, logger):
with pytest.raises(AssertionError):
serial_dev, jlink = context_factory("Test/no_start.bin")
serial_dev, jlink = context_factory("Test/Apps/no_start.bin")
def test_watch(context_factory, logger):
@@ -133,22 +133,68 @@ def test_watch(context_factory, logger):
def test_set_time(context_factory, logger):
serial_dev, jlink = context_factory("Test/set_time.bin")
serial_dev, jlink = context_factory("Test/Apps/set_time.bin")
text = serial_dev.read_until(TEST_PASS_TEXT)
print("Text:", text.decode())
assert text.endswith(TEST_PASS_TEXT)
def test_periodic_alarms(context_factory, logger):
serial_dev, jlink = context_factory("Test/periodic_alarms.bin")
serial_dev, jlink = context_factory("Test/Apps/periodic_alarms.bin")
serial_dev.timeout = 6
text = serial_dev.read_until(TEST_PASS_TEXT)
print("Text:", text.decode())
assert text.endswith(TEST_PASS_TEXT)
def test_button_slow(context_factory, logger):
serial_dev, jlink = context_factory("Test/Apps/button.bin")
serial_dev.timeout = 0.3
ASSERTED = True
serial_dev.dtr = not ASSERTED
while (line := serial_dev.readline()) is not None and len(line) > 0:
pass
for _ in range(5):
serial_dev.dtr = ASSERTED
press_line = serial_dev.readline()
serial_dev.dtr = not ASSERTED
release_line = serial_dev.readline()
assert press_line == b"up:pressed\r\n"
assert release_line == b"up:released\r\n"
def test_button_fast(context_factory, logger):
serial_dev, jlink = context_factory("Test/Apps/button.bin")
serial_dev.timeout = 0.3
ASSERTED = True
serial_dev.dtr = not ASSERTED
time.sleep(0.3)
serial_dev.timeout = 0
while (line := serial_dev.readline()) is not None and len(line) > 0:
pass
for _ in range(25):
serial_dev.dtr = ASSERTED
time.sleep(0.075)
serial_dev.dtr = not ASSERTED
time.sleep(0.075)
serial_dev.timeout = 0.3
for _ in range(25):
press_line = serial_dev.readline()
release_line = serial_dev.readline()
assert press_line == b"up:pressed\r\n"
assert release_line == b"up:released\r\n"
serial_dev.timeout = 0
assert serial_dev.readline() == b""
def test_clock(context_factory, logger):
serial_dev, jlink = context_factory("Test/clock.bin")
serial_dev, jlink = context_factory("Test/Apps/clock.bin")
EXPECTED_RUNTIME = 10
TOLERANCE = 0.2
@@ -180,7 +226,7 @@ def test_clock(context_factory, logger):
def test_wakeup_irq(context_factory, logger):
serial_dev, jlink = context_factory("Test/wakeup_irq.bin")
serial_dev, jlink = context_factory("Test/Apps/wakeup_irq.bin")
serial_dev.timeout = 65
pattern = re.compile("Requested=(\\d*) Actual=(\\d*) Wakeups=(\\d*)")
@@ -207,7 +253,7 @@ def test_wakeup_irq(context_factory, logger):
def test_stop(context_factory, logger):
serial_dev, jlink = context_factory("Test/stop.bin")
serial_dev, jlink = context_factory("Test/Apps/stop.bin")
serial_dev.timeout = 70
pattern = re.compile("Requested=(\\d*) Actual=(\\d*)")
@@ -279,7 +325,7 @@ def measure_frequency(
def test_lptim(context_factory, logger):
serial_dev, jlink = context_factory("Test/lptim.bin")
serial_dev, jlink = context_factory("Test/Apps/lptim.bin")
state0_periods, state1_periods = measure_frequency(1, "D0")
num_periods = min(len(state0_periods), len(state1_periods))
periods = [state0_periods[i] + state1_periods[i] for i in range(num_periods)]
@@ -297,52 +343,6 @@ def test_lptim(context_factory, logger):
assert max_f < 51
def test_button_slow(context_factory, logger):
serial_dev, jlink = context_factory("Test/button.bin")
serial_dev.timeout = 0.3
ASSERTED = True
serial_dev.dtr = not ASSERTED
while (line := serial_dev.readline()) is not None and len(line) > 0:
pass
for _ in range(5):
serial_dev.dtr = ASSERTED
press_line = serial_dev.readline()
serial_dev.dtr = not ASSERTED
release_line = serial_dev.readline()
assert press_line == b"up:pressed\r\n"
assert release_line == b"up:released\r\n"
def test_button_fast(context_factory, logger):
serial_dev, jlink = context_factory("Test/button.bin")
serial_dev.timeout = 0.3
ASSERTED = True
serial_dev.dtr = not ASSERTED
time.sleep(0.3)
serial_dev.timeout = 0
while (line := serial_dev.readline()) is not None and len(line) > 0:
pass
for _ in range(25):
serial_dev.dtr = ASSERTED
time.sleep(0.075)
serial_dev.dtr = not ASSERTED
time.sleep(0.075)
serial_dev.timeout = 0.3
for _ in range(25):
press_line = serial_dev.readline()
release_line = serial_dev.readline()
assert press_line == b"up:pressed\r\n"
assert release_line == b"up:released\r\n"
serial_dev.timeout = 0
assert serial_dev.readline() == b""
def main():
pytest.main(sys.argv)