class TestEndToEnd(unittest.TestCase): def setUp(self): print(self.id().split('.')[-1]) # test name self.vlab = Vlab(working_directory=setting.dir, print_uart=True) self.vlab.load(setting.fw_bin) self.uart = self.vlab.uart self.vlab.run_for_ms(500) print('Virtual device is running') def tearDown(self): self.vlab.stop() def push_button(self): print('Button on') self.vlab.BUTTON1.on() self.vlab.run_for_ms(60) print('Button off') self.vlab.BUTTON1.off() self.vlab.run_for_ms(200) ''' Black Box Test Set temperature, click a button and verify that the same temperature was printed to UART ''' def test_2_Black_Box(self): for i in range(3): self.push_button() line = self.uart.read() match = re.search('(Temperature: )(\d{2})', line) temp_uart = int(match.group(2)) self.assertTrue(20 <= temp_uart <= 40)
class TestDebouncer(unittest.TestCase): def setUp(self): # set up the device simulation self.v = Vlab(working_directory=".", print_uart=True, token="demo-token-for-ci") self.v.load("BUILD/STM32_Button_Debounce.bin") self.v.on_pin_level_event(self.pin_listener) # running for a while to get the BSP done self.v.run_for_ms(10) self.is_led_on = False self.times_pressed = 0 def tearDown(self): self.v.stop() def pin_listener(self, pin_number, pin_level): if pin_number == 5 and pin_level == 1: self.is_led_on = True self.times_pressed = self.times_pressed + 1 def test_debouncer(self): self.v.UserButton.on() self.v.run_for_ms(10) self.v.UserButton.off() self.v.run_for_ms(10) self.v.UserButton.on() self.v.run_for_ms(10) self.v.UserButton.off() self.v.run_for_ms(10) self.v.UserButton.on() self.v.UserButton.off() self.v.run_for_ms(280) self.assertEqual(self.is_led_on, True) self.assertEqual(self.times_pressed, 1)
class TestBma280(unittest.TestCase): def setUp(self): self.vlab = Vlab(working_directory=dir, print_uart=True) self.vlab.load(fw_bin) self.uart = self.vlab.uart # self.vlab.run_for_ms(500) self.vlab.start() print('Virtual device is running') def tearDown(self): self.vlab.stop() def test_should_recognize_low_g(self): self.vlab.BMA280Device.set_interrupt('low_g') self.vlab.run_for_ms(1000) self.assertRegexpMatches( self.uart.read(), "Firmware recognized bma280 interrupt: low_g") def test_should_recognize_orient_xy(self): self.vlab.BMA280Device.set_interrupt('orient_xy') self.vlab.run_for_ms(1000) self.assertRegexpMatches( self.uart.read(), "Firmware recognized bma280 interrupt: orient_xy") def test_should_recognize_slope_x(self): self.vlab.BMA280Device.set_interrupt('slope_x') self.vlab.run_for_ms(1000) self.assertRegexpMatches( self.uart.read(), "Firmware recognized bma280 interrupt: slope_x")
class TestAppButton(unittest.TestCase): def pins_listener(self, pin_number, pin_level): if pin_number == 17 and pin_level == 0: self.is_led_on = True def setUp(self): self.vlab = Vlab(working_directory=dir) self.vlab.load(fw_bin) self.vlab.on_pin_level_event(self.pins_listener) self.vlab.run_for_ms(500) print('Virtual device is running') self.is_led_on = False def tearDown(self): self.vlab.stop() def test_should_ignore_button_noise(self): self.vlab.BUTTON1.on() self.vlab.run_for_ms(1) self.vlab.BUTTON1.off() self.vlab.run_for_ms(500) self.assertFalse(self.is_led_on) def test_led_should_turn_on_on_button_push(self): self.vlab.BUTTON1.on() self.vlab.run_for_ms(60) self.vlab.BUTTON1.off() self.vlab.run_for_ms(500) self.assertTrue(self.is_led_on)
class TestEndToEnd(unittest.TestCase): def setUp(self): print(self.id().split('.')[-1]) tcp_server_dir = os.path.join(here, '..', 'tcp-server') self.gateway_process = subprocess.Popen( ['python', 'aws-temprature.py'], cwd=tcp_server_dir) sleep(2) self.vlab = Vlab(working_directory=setting.dir, print_uart=True) self.vlab.load(setting.fw_bin_aws) self.uart = self.vlab.uart self.vlab.run_for_ms(500) print('Virtual device is running') def tearDown(self): self.vlab.stop() self.gateway_process.terminate() self.gateway_process.wait() def push_button(self): print('Button on') self.vlab.BUTTON1.on() self.vlab.run_for_ms(60) print('Button off') self.vlab.BUTTON1.off() self.vlab.run_for_ms(1000) def read_from_aws(self): response = client.get_thing_shadow(thingName='my_temp_sensor') streaming_body = response["payload"] raw_data_bytes = streaming_body.read() raw_data_string = raw_data_bytes.decode('utf-8') # Python 3.x specific json_state = json.loads(raw_data_string) return json_state['state']['desired']['temprature'] ''' Integration Test Set temperature, click a button and verify that the shadow device on AWS was updated with the same temperature ''' def test_3_Integration_Test(self): for i in range(3): self.push_button() match = re.search('(Temperature: )(\d{2})', self.uart.read()) temp_uart = int(match.group(2)) success = False temp_aws = 0 for j in range(60): try: temp_aws = int(self.read_from_aws()) print('Temperature from aws: ' + str(temp_aws)) success = temp_uart == temp_aws if success: break except Exception as e: pass sleep(1) self.assertTrue(20 <= temp_aws <= 40) self.assertTrue(success)
class TestClass(unittest.TestCase): _FAIL = False _PASS = True def setUp(self): ########################## # change these arguments # ########################## self.fw = 'BUILD/STM32_Button_Debounce.elf' # TODO: YOUR FW HERE self.timeout = 10000 self.timeout_result = self._FAIL self.uart_test = True self.uart_test_value = 'mBed boot done' self.uart_test_result = self._PASS self.gpio_test = False self.gpio_test_pin = None self.gpio_test_value = None self.gpio_test_result = None self._is_pass = False # set up the device simulation self.v = Vlab(working_directory=".", print_uart=True, token="demo-token-for-ci") self.v.load(self.fw) self.v.on_pin_level_event(self.pin_listener) # running for a while to get the BSP done self.v.start(0) self.uart_data = '' self._condition_found = False self.v.stop_after_ms(self.timeout) self.v.resume() def tearDown(self): self.v.stop() def pin_listener(self, pin_number, pin_level): if self.gpio_test and self.gpio_test_pin == pin_number and self.gpio_test_value == pin_level: self._is_pass = True if self.gpio_test_result == self._PASS else False self._condition_found = True def read_from_uart(self): self.uart_data += self.v.uart.read() if self.uart_test and self.uart_test_value in self.uart_data: self._is_pass = True if self.uart_test_result == self._PASS else False self._condition_found = True def test_debouncer(self): while self.v.get_state() == 'running' and not self._condition_found: self.read_from_uart() sleep(0.1) self.assertEqual(self._is_pass, True)
class TestAppButton(unittest.TestCase): def setUp(self): print(self.id().split('.')[-1]) # test name self.vlab = Vlab(working_directory=dir) self.vlab.load(fw_bin) self.vlab.on_pin_level_event(self.pins_listener) self.vlab.run_for_ms(500) print('Virtual device is running') self.is_led_on = False def pins_listener(self, pin_number, pin_level): if pin_number == 17 and pin_level == 0: self.is_led_on = True def tearDown(self): self.vlab.stop() def print_device_time(self): print('Device time: ' + str(self.vlab.get_device_time_ms()) + ' ms') def test_led_should_turn_on_on_button_push(self): self.print_device_time() print('Button on') self.vlab.BUTTON1.on() self.vlab.run_for_ms(60) self.print_device_time() print('Button off') self.vlab.BUTTON1.off() self.vlab.run_for_ms(500) # in this case we are going to poll for the pin level self.assertEqual(self.vlab.get_pin_level(17), 0) def test_should_ignore_button_noise(self): self.print_device_time() print('Button on') self.vlab.BUTTON1.on() self.vlab.run_for_ms(1) self.print_device_time() print('Button off') self.vlab.BUTTON1.off() self.vlab.run_for_ms(500) self.assertFalse(self.is_led_on)
class TestCLI(unittest.TestCase): def setUp(self): self.vlab = Vlab(working_directory=dir, print_uart=True) self.vlab.load(fw_bin) self.uart = self.vlab.uart self.vlab.start() print('Virtual device is running') def tearDown(self): self.vlab.stop() def test_cli(self): self.vlab.uart.write(b'log\r\n') self.vlab.uart.wait_until_uart_receives(b'status :Logger status', 500) self.vlab.uart.write(b'python\r\n') self.vlab.uart.wait_until_uart_receives(b'Nice joke ;)', 500) self.vlab.uart.write(b'history\r\n') self.vlab.uart.wait_until_uart_receives(b'[ 2] history', 500)
class TestBq24160(unittest.TestCase): def setUp(self): self.vlab = Vlab(working_directory=dir) self.vlab.load(fw_bin) self.uart = self.vlab.uart self.vlab.run_for_ms(500) self.vlab.on_pin_level_event(self.pins_listener) print('Virtual device is running') self.success = False self.need_to_be_on = False self.pin_number_should_toggle = 6 def pins_listener(self, pin_number, pin_level): if self.pin_number_should_toggle == pin_number: self.success = True def tearDown(self): self.vlab.stop() def test_should_raise_interrupt_bq24160(self): print("raise watchdog interrupt") self.pin_number_should_toggle = 3 self.success = False self.vlab.bq24160.interrupt( self.vlab.bq24160.WATCHDOG_EXPIRATION_INTERRUPT) self.vlab.run_for_ms(0.5) self.assertTrue(self.success) self.pin_number_should_toggle = 7 self.success = False self.vlab.run_for_ms(1.2) self.assertTrue(self.success) def test_should_charge_discharge_bq24160(self): print("charge usb") self.pin_number_should_toggle = 6 self.success = False self.vlab.bq24160.charge("usb") self.vlab.run_for_ms(500) self.assertTrue(self.success) self.assertRegexpMatches(self.uart.read(), "Charging from usb") self.pin_number_should_toggle = 8 self.success = False print("discharge") self.vlab.bq24160.discharge() self.vlab.run_for_ms(500) self.assertTrue(self.success) self.assertRegexpMatches(self.uart.read(), "Stopped charging") self.pin_number_should_toggle = 5 self.success = False print("charge in") self.success = False self.vlab.bq24160.charge("in") self.vlab.run_for_ms(500) self.assertTrue(self.success) self.assertRegexpMatches(self.uart.read(), "Charging from in") self.pin_number_should_toggle = 8 self.success = False print("discharge") self.vlab.bq24160.discharge() self.vlab.run_for_ms(500) self.assertTrue(self.success) self.assertRegexpMatches(self.uart.read(), "Stopped charging")
import os from jumper.vlab import Vlab dir = os.path.dirname(os.path.abspath(__file__)) fw_bin = os.path.join(dir, '..', 'pca10040', 'blank', 'armgcc', '_build', 'nrf52832_xxaa.bin') print(dir) vlab = Vlab(working_directory=dir) vlab.load(fw_bin) with vlab as v: print('Virtual device is running') result = v.uart.wait_until_uart_receives('----------------')
class SerialEmulator: def __init__(self): self.master, self.slave = pty.openpty() print os.ttyname(self.slave) def write(self, data): os.write(self.master, chr(data)) def read(self): return os.read(self.master, 1) # set up the device simulation v = Vlab(working_directory=".", print_uart=True, uarts_to_print=['UART5'], platform="stm32f446", gdb_mode=False) v.load("cmake-build-simulate/Kromek.bin") v.start() def receive_uart(data): ser.write(data) ser = SerialEmulator() v.USART2.on_uart_data(receive_uart) if __name__ == '__main__': while True: read = ser.read() v.USART2.write(read)
#!/usr/bin/python2 from jumper.vlab import Vlab # set up the device simulation v = Vlab(working_directory=".", print_uart=True, platform="nrf52832") v.load("build/zephyr/zephyr.bin") v.run_for_ms(500) v.BUTTON1.on() v.run_for_ms(500) v.BUTTON1.off() v.run_for_ms(500) v.stop()