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, 2021 Privacy policy forktestlab@gmail.com
Privacy policy
This privacy policy describes how and when we collect, use, and share information when you provide your personal information(name, phone number, email address) on the site forktestlab.com or related sites and services.
This privacy policy does not apply to the actions of third parties that we do not own or control. You can refer to Artes Electronics privacy policy to learn more about its privacy practices.
__________

Information we collect

In order to complete your order or contact you to discuss a partnership, you must provide us with certain information, such as your name, email address, phone number(in some cases, postal address, payment information, and details of the product you are ordering). You can also provide us with additional personal information (for example, for an individual order) if you contact us directly.
__________

Why we need your information and how we use it

We rely on a number of legal grounds to collect, use, and share your information, including:
as necessary to provide our services, such as when we use your information to fulfill your order, resolve disputes, or provide customer support;
when you have given your affirmative consent, which you can revoke at any time, for example by subscribing to our mailing list.
__________

Information sharing and disclosure

Information about our customers is important to our business, so we do not share this information with third parties. Otherwise, it is possible only after receiving your personal consent.
__________

Data storage

We only store your personal information for as long as it is necessary to provide our services and as described in this privacy policy. However, we may also be required to store this information in accordance with our legal and regulatory obligations, resolve disputes, and enforce our agreements. Maximum period of storage of your data: 5 years.
__________

Transfer of personal information outside the EU

We may store and process your information through third-party hosting services in the United States and other jurisdictions. As a result, we may transfer your personal information to a jurisdiction with different data protection and government oversight laws than your jurisdiction. If we are deemed to be transferring information about you outside the EU, we rely on Privacy Shield as the legal basis for the transfer.
__________

Your right

If you live in certain territories, including the EU, you have a number of rights with respect to your personal information. While some of these rights apply in General, some rights only apply in certain limited cases. We describe these rights below:
Access. You may have the right to access and obtain a copy of the personal information we hold about you by contacting us using the contact information below.
Edit, restrict, delete. You also have the right to change, restrict or delete your personal information. No exceptional circumstances (for example, when we are required to store data for legal reasons) We will generally delete your personal information upon request.
An object. You may object to the processing of some of your data based on our legitimate interests and the receipt of marketing messages from us after you have provided your explicit consent to receive them(providing mail is such consent). In such cases, we will delete your personal information if we do not have a valid and legitimate reason to continue using this information or if it is necessary for legal reasons.
__________

How to contact us

For the purposes of EU data protection law, we, Artes Electronics, are the data controller of your personal information. If you have any questions or concerns, you can contact us at info@arteselectronics.com. In addition, you can send us an mail at: office 17, 50A Tambovskaya str., Saint Petersburg, 192007, Russia