Hardware tests automation with Fork

Install ForkAPI Python package
One of the purposes of Fork is a hardware tests automation. Using Fork Web API you can write your own code using any programming language to test hardware. But for those who use Python we have developed the 'forkapi' Python package that makes communication with Fork really simple. It could be downloaded here: Fork API
How to install for beginners:
  1. Clone the repository
  2. Navigate to the repository root directory within console
  3. Run 'pip install -U .' or run 'install.cmd'

Note: if you installed python for all users you need to run these commands as Administrator also.
2. Use Pytest for testing
Even with 'forkapi' package coding your own test routine from scratch could be frustrating, so here we describe how to integrate Fork into 'pytest'. It is a professional open source tool for testing:

https://github.com/pytest-dev/pytest
Please note, pytest 5.1.1 might not work with python 3.7.4. So install Python <= 3.7.3 instead. There is an example of simple test measures voltage at Fork's AI0 input:

‘test_foo.py’:
    # -*- coding: utf-8 -*-
    from forkapi import fork
    def test_voltage0():
      """Voltage Test"""
            limit_min = 3.0
            limit_max = 3.6
            # initialize Fork instance
            f = fork.Fork('192.168.0.109')
            # measure voltage at AI0
            measured = f.aiGet(0)
            # compare measured value with the limits
            # assert raises an AssertionError in case if the expression returns False
            assert limit_min <= measured <= limit_max
Now if you run 'pytest' in the directory containing this file you should get something like this
Writing more functions gives you more test cases:

‘test_foo.py’ (continued):
    
    def test_voltage1():
        
       """Another voltage Test"""
            
             limit_min = -3.6
             limit_max = -3.3
                
             f = fork.Fork('192.168.0.109')
                
            # measure voltage at AI1
            measured = f.aiGet(1)
                
            # compare measured value with the limits
            assert limit_min <= measured <= limit_max
            
    def test_voltage2():
        
       """Yet another voltage Test"""
            
            limit_min = 2
            limit_max = 4
                
            f = fork.Fork('192.168.0.109')
                
           # measure voltage at AI2
           measured = f.aiGet(2)
                
           # compare measured value with the limits
           assert limit_min <= measured <= limit_max
You can see that the third test case failed because the measured voltage at AI2 was out of the limits. While running tests we have to communicate with Fork frequently. It is more correct to initialize connection to Fork once at session start, not in every test case. To do so you can exploit such pytest feature as fixtures (https://docs.pytest.org/en/latest/fixture.html). Let's write sesion-wide fixture and place it in the 'conftest.py' file, so it will be shared among all your test modules.

‘conftest.py’:
    # -*- coding: utf-8 -*-
    from forkapi import fork
    import pytest
    @pytest.fixture(scope='session')
    def stand():
        stand_ip = '192.168.0.109'
        f = fork.Fork(stand_ip)
        f.connect()
        return f
Then rewrite your test functions so they use our 'stand' fixture.

‘test_foo.py’:
   # -*- coding: utf-8 -*-
   # from forkapi import fork
   def test_voltage0(stand):
       """Voltage Test"""
            limit_min = 3.0
            limit_max = 3.6
            measured = stand.aiGet(0)
            assert limit_min <= measured <= limit_max
Often you'll need some parameters to be configured in '.ini' file, not the code. For example, an IP address of Fork. In this case it is possible to use atndard pytest's 'pytestconfig' fixture to access options configured in the 'pytets.ini' file (it shall be in the directory with your 'test_' files). But before use you must register your custom options in the 'conftest.py'.

‘conftest.py’:
   # -*- coding: utf-8 -*-
   from forkapi import fork
   import pytest
      def pytest_addoption(parser):
       """Adding extra options to INI file"""
             parser.addini('fork', help='FORK IP address')
Then you can access IP address configured in 'pytest.ini' like this: 'conftest.py':

...

    @pytest.fixture(scope='session')
    def stand(pytestconfig):
        
      stand_ip = pytestconfig.getini('fork')
            
      f = fork.Fork(stand_ip)
      f.connect()
            
      return f
Note, your options must be set under the '[pytest]' section.

‘pytest.ini’:
      [pytest]
      fork = 192.168.0.109
More improvements of test setup
Pytest is scalable system. There are plenty of plugins available in the repository. Let's improve our test experience using some of them. Note: if you installed python for all users you need to run pytest as Administrator once in order to work after any plugin installation
Generate HTML reports
For building nice looking html test reports install 'pytest-html' plugin:

pip install pytest-html
Add '--html=myreport.html' to pytest command line options to generate a report. See the official documentation for the details (https://github.com/pytest-dev/pytest-html).

addr = 0x4
    data = b'\xE3
    res = dev1.I2CWriteRead(addr,3,data)
Define an order of your tests
By default pytest executes tests in that order how it found them. If you want to explicitly define test order then you should install 'pytest-odering' plugin:

pip install pytest-odering
Now you can add a special decorator to your test functions to make them run in order:

‘test_foo.py’:
  # run this test the first
  @pytest.mark.run(order=1)
  def test_voltage2(stand):
    """Yet another voltage Test"""
         limit_min = 2
         limit_max = 4
        # measure voltage at AI2
        measured = stand.aiGet(2)
        # compare measured value with the limits
        assert limit_min <= measured <= limit_max
The same setup as described above, but the third test become the first.
Working example
Here you can find an example of test setup in which one Fork tests another one. You can use it as a starting point for building your own test setups: Fork test stand
Subscribe news
© All Rights Reserved, 2022 Privacy policy forktestlab@gmail.com