Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Unit Test For Actual Faraday Radio #38

Open
kb1lqc opened this issue Feb 4, 2018 · 6 comments
Open

Implement Unit Test For Actual Faraday Radio #38

kb1lqc opened this issue Feb 4, 2018 · 6 comments

Comments

@kb1lqc
Copy link
Member

kb1lqc commented Feb 4, 2018

We need a test which is normally skipped, but could be turned on by command line or similar means. This will help work with the radio as we develop the firmware.

Pytest "skipping" might work well:
https://pytest.readthedocs.io/en/reorganize-docs/new-docs/user/skipping.html

Needs

  • During a unit test, connect to an actual serial port (let's use /dev/ttyUSB0 for now)
  • Send a packet such as a ping or just some string value
  • Consider having the test send a long duration of repetative packets (i.e. one ever seconds for 60 seconds)
@lqdev
Copy link
Contributor

lqdev commented Feb 5, 2018

Any specific modules in mind for connecting to the serial port? If not, this one looks pretty good

pyserial

@kb1lqc
Copy link
Member Author

kb1lqc commented Feb 5, 2018 via email

@lqdev
Copy link
Contributor

lqdev commented Feb 5, 2018

Gotcha

@lqdev
Copy link
Contributor

lqdev commented Feb 6, 2018

So here's a start. This is all in one file. Close to intended results or completely off mark?

from serial.tools.list_ports import grep
import pytest

def increment(x):
    '''
    Function to be tested. Increments x by 1
    x: int - any integer
    '''
    return x + 1

def is_port_available(port='/dev/ttyUSB0'):
  '''
  Checks whether specified port is available.
  port:string -  i.e. 'COM1'. Default is /dev/ttyUSB0
  '''
  is_port_available = grep(port)
  
  try:
    next(is_port_available)
    available = True
  except:
    available = False

  return available 

@pytest.mark.skipif(is_port_available,reason="hardware connected")
def test_skip_inc():
    '''
    Function to be skipped.
    '''
    assert increment(3) == 5

def test_inc():
    '''
    Evaluated function
    '''
    assert increment(3) == 4

The output is the following.

C:\Temp\LocalDev\Python\farada>python -m pytest test_simple2.py
============================= test session starts =============================
platform win32 -- Python 3.4.3, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: C:\Temp\LocalDev\Python\farada, inifile:
collected 2 items

test_simple2.py s.                                                       [100%]

===================== 1 passed, 1 skipped in 0.06 seconds =====================

Things that still need to be worked out:

  • Command line input/parametrization for is_port_available function. Samples

@kb1lqc
Copy link
Member Author

kb1lqc commented Feb 7, 2018

Thanks @lqdev yes other than the increment this could be useful. If the port is directly checked for then no command line options necessary. It would take this basic idea and include the serial/TUN checks performed with loopback in the other tests. However we would need to know what the hardware would do (loopback, or just normal radio transmissions).

There's probably a test that can be written into the firmware to help with this. For now just moving the basic versions of the serial/TUN test into a non-loopback format over the serial connection should be helpful, doesn't even have to assert any checks at the moment.

kb1lqc added a commit to kb1lqc/faradayio that referenced this issue Mar 1, 2018
Working towards detecting hardware and performing additional unit
testing, @lqdev made a great suggestion to detect the presence of a
serial port. I found it useful enough to include in the `faradayio`
library itself.
kb1lqc added a commit to kb1lqc/faradayio that referenced this issue Mar 1, 2018
Per @lqdev on FaradayRF#38 this code is modified from his original suggestion to
work with `faradayio`. It will skip the test if `/dev/ttyUSB0` is not
present which only occurs if hardware is not connected to the USB serial
port.
@kb1lqc
Copy link
Member Author

kb1lqc commented Mar 1, 2018

@lqdev I've implemented a version of your suggested code! Instead of limiting it to test code I see this USB detection code useful enough that I implemented it in the faradayio module. This is boilerplate test code but I am able to successfully skip the test if a Faraday (or any USB serial device) is not attached to /dev/ttyUSB0!

Connected

(.venv) bryce@bryce-ubuntu:~/Documents/git/faradayio$ sudo .venv/bin/pytest -vk tunHardwareSendLoop
=============================== test session starts ================================
platform linux -- Python 3.5.2, pytest-3.3.2, py-1.5.2, pluggy-0.6.0 -- /home/bryce/Documents/git/faradayio/.venv/bin/python3
cachedir: .cache
rootdir: /home/bryce/Documents/git/faradayio, inifile:
plugins: cov-2.5.1
collected 38 items                                                                 

tests/test_tun.py::test_tunHardwareSendLoop PASSED                           [100%]

=============================== 37 tests deselected ================================
===================== 1 passed, 37 deselected in 0.35 seconds ======================

Unconnected

(.venv) bryce@bryce-ubuntu:~/Documents/git/faradayio$ sudo .venv/bin/pytest -vk tunHardwareSendLoop
=============================== test session starts ================================
platform linux -- Python 3.5.2, pytest-3.3.2, py-1.5.2, pluggy-0.6.0 -- /home/bryce/Documents/git/faradayio/.venv/bin/python3
cachedir: .cache
rootdir: /home/bryce/Documents/git/faradayio, inifile:
plugins: cov-2.5.1
collected 38 items                                                                 

tests/test_tun.py::test_tunHardwareSendLoop SKIPPED                          [100%]

=============================== 37 tests deselected ================================
===================== 1 skipped, 37 deselected in 0.34 seconds =====================

See my two commits above for the code that implemented these tests.

kb1lqc added a commit to kb1lqc/faradayio that referenced this issue Mar 1, 2018
* Basically implemented `serialToTUN()` in `tunHardwareSendLoop`
kb1lqc added a commit to kb1lqc/faradayio that referenced this issue Mar 1, 2018
Will put FaradayRF#38 on hold until we have more time to investigate this. I
don't see a reason to have this hold up the next release.
@kb1lqc kb1lqc removed this from the Version 0.0.2 milestone Mar 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants