Using Binho with Pytest
We highly encourage everyone to use our new Python package which is packed with features. This library is still supported, but is not recommended for new design.
The Binho Nova Multi-Protocol USB Host Adapter pairs elegantly with pytest to implement an incredibly robust and efficient hardware and firmware testing platform.
We'll leave discussion of the features of pytest for their own very-thorough documentation, and just hit on a few tips to make it fast and easy to get started using it with your host adatper.
In order for the serial connection with your Binho Nova to persist through the entire test plan, you'll want to use a fixture. This is as easy as creating a conftest.py file in the test directory. Additionally, allow it accept command line parameters for device IDs, as this makes it easy to use multiple adapters on the same host computer.
Here's an example of a conftest.py file which creates two fixtures, one for each Binho Nova used in the testing:
1
from binhoHostAdapter import binhoHostAdapter
2
from binhoHostAdapter import binhoUtilities
3
4
import pytest
5
6
7
def pytest_addoption(parser):
8
parser.addoption(
9
"--dutID", action="store", default="0xc59bb495504e5336362e3120ff032d2c", help="dutID: the GUID of the device to perform the test on"
10
)
11
parser.addoption(
12
"--fixtureID", action="store", default="0xa917ec725150464a39202020ff172123", help="fixtureID: the GUID of the test fixture device"
13
)
14
15
@pytest.fixture(scope="session")
16
def dut(request):
17
18
# enter the deviceID of target test device
19
binho_test_deviceID = request.config.getoption('--dutID')
20
21
utilities = binhoUtilities.binhoUtilities()
22
23
binhoTesterCommPorts = utilities.getPortByDeviceID(binho_test_deviceID)
24
binhoTesterCommPort = 0
25
26
if len(binhoTesterCommPorts) == 0:
27
print("ERROR: No Binho Tester Device found!")
28
exit(1)
29
elif len(binhoTesterCommPorts) > 1:
30
print("ERROR: More than one Binho Tester Device found!")
31
exit(1)
32
else:
33
binhoTesterCommPort = binhoTesterCommPorts[0]
34
print("Found Binho Tester Device on " + binhoTesterCommPort)
35
36
37
print("Opening " + binhoTesterCommPort + "...")
38
print
39
40
# create the binhoHostAdapter object
41
testDevice = binhoHostAdapter.binhoHostAdapter(binhoTesterCommPort)
42
43
print("Connecting to binho host adapter tester...")
44
print
45
46
def teardown():
47
#print('--test device teardown')
48
testDevice.close()
49
50
request.addfinalizer(teardown)
51
return testDevice
52
53
@pytest.fixture(scope="session")
54
def testFixture(request):
55
56
# enter the deviceID of target test device
57
binho_test_deviceID = request.config.getoption('--fixtureID')
58
59
utilities = binhoUtilities.binhoUtilities()
60
61
binhoTesterCommPorts = utilities.getPortByDeviceID(binho_test_deviceID)
62
binhoTesterCommPort = 0
63
64
if len(binhoTesterCommPorts) == 0:
65
print("ERROR: No Binho Tester Device found!")
66
exit(1)
67
elif len(binhoTesterCommPorts) > 1:
68
print("ERROR: More than one Binho Tester Device found!")
69
exit(1)
70
else:
71
binhoTesterCommPort = binhoTesterCommPorts[0]
72
print("Found Binho Tester Device on " + binhoTesterCommPort)
73
74
75
print("Opening " + binhoTesterCommPort + "...")
76
print
77
78
# create the binhoHostAdapter object
79
testDevice = binhoHostAdapter.binhoHostAdapter(binhoTesterCommPort)
80
81
print("Connecting to binho host adapter tester...")
82
print
83
84
def teardown():
85
#print('--test device teardown')
86
testDevice.close()
87
88
request.addfinalizer(teardown)
89
return testDevice
90
Copied!
In this fashion, the serial connections with the devices will be opened up at the beginning of the testing. Then, using these devices from within the test cases is simple, just pass them into each function that uses them as a parameter. Here's an example function to demonstrate this:
1
def test_TestDevicesCommCheck(self, dut, testFixture):
2
3
response = dut.ping()
4
assert response == '-OK'
5
6
response = testFixture.ping()
7
assert response == '-OK'
8
9
response = dut.setLEDColor('YELLOW')
10
assert response == '-OK'
11
12
response = testFixture.setLEDColor('YELLOW')
13
assert response == '-OK'
Copied!
Writing automated hardware and firmware tests has never been easier!
Last modified 8mo ago
Copy link