Switch Evaluation

The test_switch_pressed test will pass if the USR button is being pressed, and fail otherwise.

This example introduces the use of a container class (Switch) for the pyboard’s USR switch to make test code more readable.

Bench Setup

The only requirement for this test is a pyboard connected via USB.

The USR switch on the pyboard is used for evaluation. The value of the switch is made accessible via the Switch class:

class Switch(object):
    def __init__(self, device):
        self.device = device

    @property
    def value(self):
        return self.device.get_switch()()['value']

An instance of Switch is then created in the BenchTest’s overridden setUpClass() method, referencing the pyb_a pyboard as the relevant device:

    @classmethod
    def setUpClass(cls):
        cls.pyb_a = upytester.project.get_device('pyb_a')
        cls.switch = Switch(device=cls.pyb_a)

Test Case

Setting up the BenchTest like this alows the test-code to be very short and unambiguous:

class SwitchTest(BenchTest):
    def test_switch_pressed(self):
        """
        Turn red LED on for 500ms asynchronously
        """
        self.assertTrue(self.switch.value)