class TestBattery(unittest.TestCase): list1 = ddtreadexcel().getdata() passed = [] failed = [] null = [] def getDeviceTime(self, DeviceId): url = "http://140.246.137.19:8002/locman/binding/queryDevicesByPage" # header = "Content-Type: application/json;charset=utf-8" #不能这么写 header = { "Content-Type": "application/json", "charset": "utf-8", "Token": "token-0b58be380c3f409f8c95711046519602" } body = '{"pageNum":1,"pageSize":12,"accessSecret":"aecde01f-9ae2-4876-84b7-c08ea25a4788","bingStatus":"","deviceTypeId":"", ' \ '"deviceId":' + DeviceId + ', "facilityId":"","factoryId":"","whole":"false","startTime":"","endTime":"","onLineState":""}' # print(body) j = json.loads(body) #str转json resp = requests.post(url, headers=header, json=j) resp1 = resp.json() # devTime = resp1["value"]["list"][0]["lastReportTime"] return resp1 @data(*list1) @unpack def test(self, **dict): DeviceId = str(int(dict.get('设备编号'))) if (self.getDeviceTime(DeviceId)["value"]["total"] != 0): lastReportTime = self.getDeviceTime( DeviceId)["value"]["list"][0]["lastReportTime"] # localtime = time.strftime("%Y-%m-%d") localtime = '2021-01-02' if (localtime == lastReportTime.split(" ")[0]): print(DeviceId + " " + lastReportTime + "验证通过!") self.passed.append(DeviceId) else: print(DeviceId + " " + lastReportTime + "验证不通过!") self.failed.append(DeviceId) else: print(DeviceId + " 设备编号不存在!") self.null.append(DeviceId) @classmethod def tearDownClass(self): print("失败数:" + str(len(self.failed))) print("通过数:" + str(len(self.passed))) print("空设备:" + str(len(self.null)) + '\n')
class TestLocmanLogin(object): cases = ddtreadexcel().getdata3() # 指定功能的用例的两个字段 # 新建方法 登录方法 def locman_login(self, url, body): # headers,data 定义,调用post并返回响应对象 headers = {"Content-Type": "application/json"} resp = requests.post(url, headers=headers, json=body) return resp.json() # 返回的是个对象,必须.json()显示 @pytest.mark.parametrize('body,expect', cases) def test_login(self, body, expect): result = TestLocmanLogin().locman_login( "http://182.43.224.122:8002/interGateway/v3/user/authentication", body) actualcode = result['resultStatus']['resultCode'] actualmsg = result['resultStatus']['resultMessage'] expectcode = expect['resultCode'] expectmsg = expect['resultMessage'] assert actualcode == expectcode assert actualmsg == expectmsg
from ddt_excel.DDTReadExcel import ddtreadexcel import json # 新建类 登录接口对象 class LocmanLogin(object): # 新建方法 登录方法 def locman_login(self, url, body): # headers,data 定义,调用post并返回响应对象 headers = {"Content-Type": "application/json"} resp = requests.post(url, headers=headers, json=body) return resp.json() # 返回的是个对象,必须.json()显示 if __name__ == '__main__': cases = ddtreadexcel().getdata2() # print("*******", cases, type(cases)) # list for data in cases: body = data['请求参数'] # print("XXXXX", body, type(body)) # 目前的body是str类型,需转换为json格式 j = json.loads(body) # 将str类型的body转换为json类型 # print("jsonXXXXX", j, type(j)) result = LocmanLogin().locman_login( "http://182.43.224.122:8002/interGateway/v3/user/authentication", j) # print("result&&&", result) actualAssert = result['resultStatus'] # print("actualAssert---", actualAssert, type(actualAssert)) actualCode = result['resultStatus']['resultCode'] # 必须是json格式才能这样获取
""" 必须在运行模式中用纯python运行,才能生成allure报告 用unittest和pytest模式运行都不能生成allure报告。 """ import pytest import allure import os import unittest from ddt import ddt, data, unpack from ddt_excel.DDTReadExcel import ddtreadexcel from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # as起别名 from selenium.webdriver.common.by import By list1 = ddtreadexcel().getdata() @pytest.mark.parametrize('a,b', [(1, 1), (3, 2), (2, 2)]) # 测试方法在class外面 def test3(a, b): assert a == b # @pytest.mark.parametrize('x,y', [(1, 1), (2, 2), (2, 2)]) # 测试方法在class里面,但不能和unittest.TestCase混用 @allure.feature('Locman登录') # 用feature说明产品需求,可以理解为JIRA中的Epic @ddt class TestDemo3(unittest.TestCase): # @pytest.mark.skipif(reason='本次不执行') # 对前置后置无效 # def setUp(self): # self.driver = webdriver.Chrome()