class TestAdd(unittest.TestCase): def setUp(self): c = Config() ip = c.get('ip') port = c.get('port') self.client = TCPClient(ip, port) def tearDown(self): self.client.close() def test_add(self): data = { 'action': 'add', 'params': {'a': 1, 'b': 2} } res = self.client.send(data, dtype='json') self.assertEqual(je.extract('jkresult', res), 3) self.assertEqual(je.extract('action', res), 'add') def test_wrong_action(self): data = { 'action': 'sub', 'params': {'a': 1, 'b': 2} } res = self.client.send(data, dtype='json') self.assertEqual(je.extract('code', res), -2) self.assertEqual(je.extract('message', res), 'Wrong Action') def test_wrong_data(self): data = 'xxxxx' res = self.client.send(data) self.assertEqual(je.extract('code', res), -1) self.assertEqual(je.extract('message', res), 'Data Error')
class TestAdd(unittest.TestCase): def setUp(self): c = Config() ip = c.get('ip') port = c.get('port') self.client = TCPClient(ip, port) def tearDown(self): self.client.close() def test_add(self): data = { 'action': 'add', 'params': {'a': 1, 'b': 2} } res = self.client.send(data, dtype='json') self.assertEqual(je.extract('result', res), 3) self.assertEqual(je.extract('action', res), 'add') def test_wrong_action(self): data = { 'action': 'sub', 'params': {'a': 1, 'b': 2} } res = self.client.send(data, dtype='json') self.assertEqual(je.extract('code', res), -2) self.assertEqual(je.extract('message', res), 'Wrong Action') def test_wrong_data(self): data = 'xxxxx' res = self.client.send(data) self.assertEqual(je.extract('code', res), -1) self.assertEqual(je.extract('message', res), 'Data Error')
class SocketTest(Test): def __init__(self, name, test, ip='127.0.0.1', port=3030, desc='', setup=None, teardown=None): super(SocketTest, self).__init__(name=name, test=test, desc=desc, setup=setup, teardown=teardown) self.ip = ip self.port = port self.client = TCPClient(domain=self.ip, port=self.port) def tearDown(self): self.client.close() def test_case(self): for step in self.test: step_data = step.get('data', '') # step data step_resource = step.get('resource') if step_resource: # use excel as resource file rfile = step_resource.get('file') if os.path.exists(rfile): rfile = rfile elif os.path.exists(BASE_DIR + '\\data\\' + rfile): rfile = BASE_DIR + '\\data\\' + rfile else: raise DataFileNotAvailableException('File not found: %s' % rfile) rsheet = step_resource.get('sheet', 0) rstart = step_resource.get('start', 0) rend = step_resource.get('end') rdata = ExcelReader(rfile, rsheet).data rstart = rstart - 1 if rstart > 0 else 0 if rend and len(rdata) >= rend: rdata = rdata[rstart:rend] else: rdata = rdata[rstart:] # debug logger.debug('resource: %s' % str(step_resource)) logger.debug('excel data: %s' % str(rdata)) for num, line in enumerate(rdata): with self.subTest(msg='SubTest_%d' % (num + 1), data=line): # SubTest logger.debug('---------- SubTest %d ----------' % (num + 1)) # debug sub_data = step_data if step_data: # 用正则匹配里面的$resource.xxx$出来 pattern = re.compile('\$resource\.(.*?)\$') for r in pattern.findall(step_data): r1 = line.get(r) if r1: sub_data = pattern.sub(r1, sub_data, 1) logger.debug('test data: %s' % sub_data) # debug # test res = self.client.send(sub_data) # validate step_validators = step.get('validators') if step_validators: for step_validator in step_validators: for vtype, vvalue in step_validator.items(): asserts = [] if vtype in self.validators: if isinstance(vvalue, list): for vv in vvalue: if '$resource' in vv: asserts.append( line.get(vv[10:])) elif '$res' in vv: asserts.append(res) else: asserts.append(vv) elif '$resource' in vvalue: asserts = [ line.get(vvalue[10:]), res ] else: asserts = [vvalue, res] logger.debug( 'assert %s %s %s' % (asserts[0], vtype, '{}...'.format( str(asserts[1]).replace( ' ', '').replace( '\n', '')[:50]))) self.validators[vtype](asserts[0], asserts[1]) else: # debug if step_data: logger.debug('test data: %s' % step_data) # test res = self.client.send(step_data) # validate step_validators = step.get('validators') if step_validators: for step_validator in step_validators: for vtype, vvalue in step_validator.items(): asserts = [] if vtype in self.validators: if isinstance(vvalue, list): for vv in vvalue: if '$resource' in vv: asserts.append(line.get(vv[10:])) elif '$res' in vv: asserts.append(res) else: asserts.append(vv) else: if '$resource' in vvalue: asserts = [line.get(vvalue[10:]), res] logger.debug( 'assert %s %s %s' % (asserts[0], vtype, '{}...'.format( str(asserts[1]).replace( ' ', '').replace('\n', '')[:50]))) self.validators[vtype](asserts[0], asserts[1])