import unittest from test.helpers.amper import Amper class Qamper(unittest.TestCase): params = None __resultlist = None # resultlist is a python list of python dictionaries # varlist: undercurrent, overcurrent def __init__(self, testname, testfunc, varlist): self.params = varlist super(Qamper, self).__init__(testfunc) if "undercurrent" in varlist: self._undercurrent = varlist["undercurrent"] else: raise Exception('undercurrent param inside Qamp must be defined') if "overcurrent" in varlist: self._overcurrent = varlist["overcurrent"] else: raise Exception('overcurrent param inside Qamp must be defined') self._testMethodDoc = testname self.__resultlist = [] def execute(self): amp = Amper() # open serial connection if not amp.open(): self.fail("Error: can not open a serial port") # check if the amperimeter is connected and working # 2 ATTEMTS if not amp.hello(): if not amp.hello(): self.fail("Error: can not communicate") # get current value (in Amperes) self.current = amp.getCurrent() # save result in a file with open('/mnt/station_ramdisk/amper.txt', 'w') as outfile: n = outfile.write("Current: {} A".format(self.current)) outfile.close() self.__resultlist.append( { "description": "Amperimeter values", "filepath": "/mnt/station_ramdisk/amper.txt", "mimetype": "text/plain" } ) # close serial connection amp.close() # Check current range if float(self.current) > float(self._overcurrent): # Overcurrent detected self.fail("failed: Overcurrent detected ( {} )".format(self.current)) elif float(self.current) < float(self._undercurrent): # Undercurrent detected self.fail("failed: Undercurrent detected ( {} )".format(self.current)) def getresults(self): return self.__resultlist def gettextresult(self): return "{} A".format(self.current)