pythonpytestbacnetbac0bacpypes

pyTest for UDP protocol scripts


Would anyone have a pyTest tip if its possible to use with a Python BACnet library called BAC0? I have never used pyTest before just curious if there would be any boiler plate scripts and if my ideology is on track or not.

I have a BACnet server app below with 1 discoverable BACnet (UDP protocol) point for a demand response signal level. BACnet is a protocol quite common with building automation technology.

def make_bacnet_app():
    
    def check_event_status():
    
        adr_sig_object = bacnet.this_application.get_object_name('ADR-Event-Level')
        adr_sig_object.presentValue = Real(ven_client.bacnet_payload_value)            

    # create discoverable BACnet object
    _new_objects = analog_value(
            name='ADR-Event-Level',
            description='SIMPLE SIGNAL demand response level',
            presentValue=0,is_commandable=False
        )

    # create BACnet app
    bacnet = BAC0.lite()
    _new_objects.add_objects_to_application(bacnet)
    print('BACnet APP Created Success!')
    
    bacnet_sig_handle = RecurringTask(check_event_status,delay=5)
    bacnet_sig_handle.start()
    
    return bacnet


if __name__ == "__main__":
    print("Starting main loop")
    t2 = threading.Thread(
        target=lambda: make_bacnet_app())
    t2.setDaemon(True)
    t2.start()

There's some other code not shown that checks a server with requests basically I can get the BACnet point to change with my testing by sending a GET request to this server on my home test bench laboratory: http://192.168.0.100:8080/trigger/1

Basically the server and BAC0 app run on the same device for my testing purposes...

And it works with just using a BACnet scan tool my BACnet point changes what I need it to do...just curious if anyone would have some tips putting a pyTest script together?

i dont even know if this is possible but can pyTest do this:

  1. start server.py file on remote machine (else I can just run them myself thru SSH)
  2. start client BAC0 script on remote machine (else I can just run them myself thru SSH)
  3. send GET request to server
  4. perform a BACnet read request with BAC0 to my BAC0 app to verify the point reads a 0
  5. wait 100 seconds (built into my server for testing purposes)
  6. perform a BACnet read request with BAC0 to my BAC0 app to verify the point reads a 1
  7. wait 100 seconds (built into my test)

I can test my bacnet server app with a simple client script that I have run on a remote machine which I should be able to stuff into a pyTest function to assert if the reading equals a 1 or 0. The server app and client code cannot run on the same device the scripts will error out because only one BACnet instance can run on specific port for BACnet a time.

import BAC0

bacnet = BAC0.lite()
reading = bacnet.read("192.168.0.100 analogValue 0 presentValue")

print("The signal is: ",reading)

Not alot of wisdom here any tips appreciated!!!!!!!!


Solution

  • BAC0 test suite is an example https://github.com/ChristianTremblay/BAC0/tree/master/tests

    The conftest.py define a "fixture" that can be used by other tests functions (to share the bacnet network for example)

    Once your folder is done with the conftest.py and all the test_xyz.py files you simply run pytest -v and watch.

    [ref : https://www.youtube.com/watch?v=azxiv7L6CZE] [ref : https://pythontest.com/pytest-book/]