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
ping
is called on thePyBoard
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.The pyboard’s response is stored in
response
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:
We create and close a new
PyBoard
instance for each test. This could be done inTestCase.setUpClass()
andTestCase.tearDownClass()
insteadThe bench deisgn is defined in the test class. Instead this could be defined in an inherited class.