Ping

This is the simplest example of a upytester test-case. It tests that the configured pyboard is connected, and responding to serial commands.

This example’s implementation is far from the best design for a bench test, but it is a good starting point to explain how upytester may be set up to be useful for your peojct.

Bench Setup

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

Test Case

Setup

TestCase.setUp() is called before each test. For this test, we use the get_device() method to return a PyBoard instance to send commands to, and assign it to self.pyb_a.

    def setUp(self):
        self.pyb_a = upytester.project.get_device('pyb_a')

Test

The test is done in 3 parts:

    def test_ping(self):
        """
        Send a ping request over serial to the pyboard identified as ``pyb_a``.
        """
        receiver = self.pyb_a.ping(value=100)
        response = receiver()
        self.assertEqual(response['value'], 101)  # ping sends back value + 1
  1. ping is called on the PyBoard instance. This command is serialized and set to to the pyboard via USB. It also returns a callable that can be used to receive a response from the pyboard.

  2. The pyboard’s response is stored in response

  3. The value of the responce is used to evaluate the test’s verdict.

Note: this could have all been done in one line.

self.assertEqual(self.pyb_a.ping(value=100)()['value'], 101)

Tear Down

To compliment setUp(), tearDown() is called after each test.

The serial link to the pyboard is closed by calling close()

    def tearDown(self):
        self.pyb_a.close()

Improvements

The following improvements can be found in the next example: