示例#1
0
class RechargeTest(unittest.TestCase):
    do_excel = do_excel.DoExcel(contants.case_file, "recharge")
    cases = do_excel.get_cases()

    @classmethod
    def setUpClass(cls):
        logger.debug("准备测试前置")
        cls.http_request = http_request.HttpRequest2()

    @data(*cases)
    def test_recharge(self, case):
        logger.debug("开始测试:{}".format(case.title))
        resp = self.http_request.request(case.method, case.url, case.data)
        actual_code = resp.json()['code']
        # print(type(actual_code))
        try:
            self.assertEqual(str(case.exspected), actual_code)  # 判断预期结果和实际结果是否相等
            # 断言出错,就不执行后面的语句
            self.do_excel.write_result(case.case_id + 1, resp.text, "PASS")
        except AssertionError as e:
            self.do_excel.write_result(case.case_id + 1, resp.text, "Fail")
            logger.error("报错了:{}".format(e))
            raise e  # 抛出异常,但不影响后面用例的执行
        else:
            logger.debug("结束测试:{}".format(case.title))

    @classmethod
    def tearDownClass(cls):
        logger.debug("测试后置处理")
        cls.http_request.close()
        # pass
示例#2
0
class BooksTest(unittest.TestCase):
    excel = do_excel.DoExcel(contants.case_file, 'shop_list')  # shop_list接口
    cases = excel.get_case()

    @classmethod
    def setUpClass(cls):
        logger.info('准备测试前置')
        cls.http_requets = HttpRequest2()  # 请求类

    @data(*cases)
    def test_books(self, case):
        logger.info('开始测试:{0}'.format(case.title))
        resp = self.http_requets.http_request(case.method, case.url, case.data)
        results = jsonpath.jsonpath(json.loads(resp.text), "$..listName")
        try:
            self.assertIn(case.expected, results)
            self.excel.writer_result(case.case_id + 1, resp.text, 'PASS')
        except AssertionError as e:
            self.excel.writer_result(case.case_id + 1, resp.text, 'FAIL')
            logger.error("报错了,{0}".format(e))
            raise e
        logger.info('测试结束:{0}'.format(case.title))

    @classmethod
    def tearDownClass(cls):
        logger.info('测试后置处理')
        cls.http_requets.close()
class ChapterModuleTest(unittest.TestCase):
    excel = do_excel.DoExcel(contants.case_file, 'chapterModule')
    cases = excel.get_case()

    @classmethod
    def setUpClass(cls):
        logger.info('准备测试前置')
        cls.http_request = HttpRequest2()

    @data(*cases)
    def test_chapter_module(self, case):
        logger.info("开始测试{}".format(case.title))
        resp = self.http_request.http_request(case.method, case.url, case.data)
        results = json.loads(resp.text)
        try:
            self.assertEqual(eval(case.expected), results)
            self.excel.writer_result(case.case_id + 1, resp.text, 'PASS')
        except ValueError as e:
            self.excel.writer_result(case.case_id + 1, resp.text, 'FAIL')
            logger.error("报错了,{0}".format(e))
            raise e
        logger.info('测试结束:{0}'.format(case.title))

    @classmethod
    def tearDownClass(cls):
        logger.info('测试后置处理')
        cls.http_request.close()
示例#4
0
class AddTest(unittest.TestCase):
    do_excel = do_excel.DoExcel(contants.case_file, "add")
    cases = do_excel.get_cases()

    @classmethod
    def setUpClass(cls):
        logger.info("准备测试前置")
        cls.http_request = http_request.HttpRequest2()
        cls.do_mysql = do_mysql.DoMySql()

    @data(*cases)
    def test_add(self, case):
        logger.info("开始测试:{0}".format(case.title))
        # case.data中带有参数,需要替换一下
        case.data = context.replace(case.data)
        # print(case.data)
        resp = self.http_request.request(case.method, case.url, case.data)
        actual_code = resp.json()['code']
        try:
            self.assertEqual(str(case.exspected),
                             actual_code)  # 判断预期结果和实际结果是否相等
            # 断言出错,就不执行后面的语句
            self.do_excel.write_result(case.case_id + 1, resp.text, "PASS")
        except AssertionError as e:
            self.do_excel.write_result(case.case_id + 1, resp.text, "Fail")
            logger.error("报错了:{}".format(e))
            raise e  # 抛出异常,但不影响后面用例的执行
        else:
            logger.info("结束测试:{}".format(case.title))

    @classmethod
    def tearDownClass(cls):
        logger.info("测试后置处理")
        cls.http_request.close()
        cls.do_mysql.close()
示例#5
0
class ChapterListTest(unittest.TestCase):
    excel = do_excel.DoExcel(contants.case_file, 'chapterList')
    cases = excel.get_case()

    @classmethod
    def setUpClass(cls):
        logger.info("准备测试前置")
        cls.http_request = HttpRequest2()

    @data(*cases)
    def test_chapter_list(self, case):
        logger.info('开始测试{}'.format(case.title))
        resp = self.http_request.http_request(case.method, case.url, case.data)
        # print('case.data:', case.data)
        # print('resp:', resp)
        # print('type(resp):', type(resp))
        results = json.loads(resp.text)
        # print(results["success"])
        try:
            self.assertEqual(ast.literal_eval(case.expected),
                             results["success"])
            self.assertTrue(results["data"][0]["nodule"])
            self.excel.writer_result(case.case_id + 1, resp.text, "PASS")
        except AssertionError as e:
            logger.error('报错了{}'.format(e))
            self.excel.writer_result(case.case_id + 1, resp.text, "FAIL")
            raise e
        logger.info('测试结束'.format(case.title))

    @classmethod
    def tearDownClass(cls):
        logger.info('测试后置处理')
        cls.http_request.close()
示例#6
0
class PlanTasksTest(unittest.TestCase):
    excel = do_excel.DoExcel(contants.case_file, 'shop_add')  # 测试shop_add接口
    cases = excel.get_case()

    @classmethod
    def setUpClass(cls):
        logger.info('准备测试前置')
        cls.http_request = HttpRequest2()
        cls.mysql = do_mysql.DoMysql()

    @data(*cases)
    def test_plan_tasks(self, case):
        logger.info('开始测试{}'.format(case.title))
        case.data = context.replace(case.data)
        resp = self.http_request.http_request(case.method, case.url, case.data)
        results = json.loads(resp.text)
        try:
            sql = f"SELECT plan_id FROM alading_jdcs.al_en_plan WHERE goodsId = '{goodsId}';"
            saleprice = self.mysql.fetch_one(sql)['salePrice']  # 查询所属商品的售价
            self.assertEqual(results["data"]["shelves"]["salePrice"], saleprice)
        except ValueError as e:
            self.excel.writer_result(case.case_id + 1, resp.text, "FAIL")
            logger.error('报错了{}'.format(e))
            raise e
        logger.info('测试结束:{}'.format(case.title))

    @classmethod
    def tearDownClass(cls):
        logger.info('测试后置处理')
        cls.http_request.close()
示例#7
0
class BooklistTest(unittest.TestCase):
    excel = do_excel.DoExcel(contants.case_file, 'bookList')  # 测试bookList接口,读取excel里Sheet"bookList"
    cases = excel.get_case()  # 获取excel数据

    @classmethod
    def setUpClass(cls):
        logger.info('准备测试前置')
        cls.http_request = HttpRequest2()

    @data(*cases)
    def test_allbooklist(self, case):
        logger.info('开始测试:{0}'.format(case.title))
        resp = self.http_request.http_request(case.method, case.url, case.data)  # 请求数据,传入excel里的method,url,data参数
        # print(resp.text)
        # print('type(resp.text):', type(resp.text))
        # print('type(case.expexted:', type(case.expected))
        results = jsonpath.jsonpath(json.loads(resp.text), "$..bookName")  # 运用了jsonpath去获取包含bookName的值(列表)
        try:
            self.assertIn(case.expected, results)  # 断言:是否在返回值里
            self.excel.writer_result(case.case_id + 1, resp.text, "PASS")  # 写入测试结果及”PASS“
        except AssertionError as e:
            self.excel.writer_result(case.case_id + 1, resp.text, "FAIL")
            logger.error("报错了,{0}".format(e))
            raise e
        logger.info('结束测试:{0}'.format(case.title))

    @classmethod
    def tearDownClass(cls):
        logger.info('测试后置处理')
        cls.http_request.close()
示例#8
0
class BooksTest(unittest.TestCase):
    excel = do_excel.DoExcel(contants.case_file, 'books')  # books接口
    cases = excel.get_case()

    @classmethod
    def setUpClass(cls):
        logger.info('准备测试前置')
        cls.http_requets = HttpRequest2()  # 请求类

    @data(*cases)
    def test_books(self, case):
        logger.info('开始测试:{0}'.format(case.title))
        resp = self.http_requets.http_request(case.method, case.url, case.data)
        result = eval(resp.text.replace(
            'true', 'True'))  # 将json返回值的true替换为python可读的True
        # print('case.expected:', case.expected)
        # print(type(case.expected))
        # print('result:', result)
        # print('type(result):', type(result))
        try:
            # self.assertEqual(case.expected, result)
            self.assertEqual(eval(case.expected), result["success"])  # 断言
            self.excel.writer_result(case.case_id + 1, resp.text, 'PASS')
        except AssertionError as e:
            self.excel.writer_result(case.case_id + 1, resp.text, 'FAIL')
            logger.error("报错了,{0}".format(e))
            raise e
        logger.info('测试结束:{0}'.format(case.title))

    @classmethod
    def tearDownClass(cls):
        logger.info('测试后置处理')
        cls.http_requets.close()
示例#9
0
class LoginTest(unittest.TestCase):
    do_excel = do_excel.DoExcel(contants.case_file, "login")
    cases = do_excel.get_cases()

    @classmethod
    def setUpClass(cls):
        logger.debug("准备测试前置")
        cls.http_request = http_request.HttpRequest2()

    @data(*cases)
    def test_login(self, case):
        # 日志输出
        logger.debug("开始测试:{}".format(case.title))
        # print(case.title)
        resp = self.http_request.request(case.method, case.url, case.data)
        actual = resp.text
        try:
            self.assertEqual(case.exspected, actual)  # 判断预期结果和实际结果是否相等
            # 断言出错,就不执行后面的语句
            self.do_excel.write_result(case.case_id + 1, actual, "PASS")
        except AssertionError as e:
            self.do_excel.write_result(case.case_id + 1, actual, "Fail")
            logger.error("报错了:{0}".format(e))
            raise e  # 抛出异常,但不影响后面用例的执行
        else:
            logger.debug("结束测试:{0}".format(case.title))
            # print("测试用例:[{}],执行成功!".format(case.title))

    @classmethod
    def tearDownClass(cls):
        logger.debug("测试后置处理")
        cls.http_request.close()
示例#10
0
class InvestTest(unittest.TestCase):
    """
        投资测试思路:1.管理员登录;2.管理员加标;3,管理员审核;4.投资人登录;5.投资人正常投资;6.投资数据用例的设计
        """
    do_excel = do_excel.DoExcel(contants.case_file, "invest")
    cases = do_excel.get_cases()

    @classmethod
    def setUpClass(cls):
        logger.debug("准备测试前置")
        cls.http_request = http_request.HttpRequest2()
        cls.do_mysql = do_mysql.DoMySql()

    @data(*cases)
    def test_invest(self, case):
        logger.debug("开始测试:{}".format(case.title))
        # case.data中带有参数,需要替换一下
        case.data = context.replace(case.data)
        # print(case.data)
        if case.check_sql is not None:
            sql2 = eval(case.check_sql)["sql2"]  # 获得SQL语句
            # print(sql2)
            count = self.do_mysql.fetch_one(sql2)
            print(count)
            before = count["COUNT(*)"]  # 加标之前,用户的添标数量
            # print("before:", before)
        resp = self.http_request.request(case.method, case.url, case.data)
        actual_code = resp.json()['code']
        try:
            self.assertEqual(str(case.exspected),
                             actual_code)  # 判断预期结果和实际结果是否相等
            # 断言出错,就不执行后面的语句
            self.do_excel.write_result(case.case_id + 1, resp.text, "PASS")
            # 判断加标成功后,查询数据库,找到loan_id
            if resp.json()['msg'] == '加标成功' and case.check_sql is not None:
                # 判断1:数据库中增加一条数据
                sql2 = eval(case.check_sql)["sql2"]
                after = self.do_mysql.fetch_one(sql2)["COUNT(*)"]
                self.assertEqual(before + 1, after)  # 判断加标后的数量是否+1
                # 取值:loan_id
                sql1 = eval(case.check_sql)["sql1"]
                loan_id = self.do_mysql.fetch_one(sql1)["id"]
                setattr(context.Context, "loan_id", str(loan_id))  # 反射
        except AssertionError as e:
            self.do_excel.write_result(case.case_id + 1, resp.text, "Fail")
            logger.error("报错了:{}".format(e))
            raise e  # 抛出异常,但不影响后面用例的执行
            # print(e)#有异常后,打印出来,但是用例还是显示绿色对勾
        else:
            logger.debug("结束测试:{}".format(case.title))
示例#11
0
class CreateTest(unittest.TestCase):
    excel = do_excel.DoExcel(contants.case_file, 'create')  # 测试create接口
    cases = excel.get_case()

    @classmethod
    def setUpClass(cls):
        logger.info('准备测试前置')
        cls.http_request = HttpRequest2()

    @data(*cases)
    def test_create(self, case):
        logger.info('开始测试{}'.format(case.title))
        case.data = context.replace(case.data)
        resp = self.http_request.http_request(case.method, case.url, case.data)
        # print('resp', resp)
        results = json.loads(resp.text)
        # print('results:', results)
        # print('type(results):', type(results))
        # print('case.expected:', case.expected)
        # print('type(case.expected):', type(case.expected))
        try:
            self.assertTrue(results["data"]["homeworkId"])
            sql_data = mysql.fetch_one(case.check_sql)
            # print("type(sql_data):", type(sql_data))
            # self.assertEqual(ast.literal_eval(case.expected), results["success"])
            # 断言,结果只有一个"data"跟"success",data里的homeworkID是动态的,所以获取success里的value来做断言比较好
            # self.assertEqual(ast.literal_eval(case.expected), results["success"])  # 个人偏向用ast.literal_eval来替代eval
            self.excel.writer_result(case.case_id + 1, resp.text, 'PASS',
                                     str(sql_data))
            # if results["data"]["homeworkId"]:
            #     sql_data = mysql.fetch_one(case.check_sql)
            #     self.excel.writer_result(case.case_id + 1, sql_data, )
        except AssertionError as e:
            logger.error('报错了:', e)
            self.excel.writer_result(case.case_id + 1, resp.text, 'FAIL')
            raise e
        logger.info('测试结束:{0}'.format(case.title))

    @classmethod
    def tearDownClass(cls):
        logger.info('测试后置处理')
        cls.http_request.close()
示例#12
0
class PlanTasksTest(unittest.TestCase):
    excel = do_excel.DoExcel(contants.case_file, 'add')  # 测试add接口
    cases = excel.get_case()

    @classmethod
    def setUpClass(cls):
        logger.info('准备测试前置')
        cls.http_request = HttpRequest2()

    @data(*cases)
    def test_plan_tasks(self, case):
        logger.info('开始测试{}'.format(case.title))
        case.data = context.replace(case.data)
        print("case.data:", case.data)
        resp = self.http_request.http_request(case.method, case.url, case.data)
        results = json.loads(resp.text)
        print('返回结果:', results)
        try:
            self.assertEqual(ast.literal_eval(case.expected), results["success"])
            self.excel.writer_result(case.case_id + 1, resp.text, "PASS")

            # 判断添加作业成功后,查询数据库,取到homeworkId/也可以在返回结果中取到homeworkId
            try:
                if resp.json()["data"]["homeworkId"]:
                    homeworkId = results["data"]["homeworkId"]
                    print('homeworkId:', homeworkId)
                    # 保存到类属性里面
                    setattr(Context, "homeworkId", homeworkId)
            except KeyError as e:
                pass
        except ValueError as e:
            self.excel.writer_result(case.case_id + 1, resp.text, "FAIL")
            logger.error('报错了{}'.format(e))
            raise e
        logger.info('测试结束:{}'.format(case.title))

    @classmethod
    def tearDownClass(cls):
        logger.info('测试后置处理')
        cls.http_request.close()
示例#13
0
class PlanTasksTest(unittest.TestCase):
    excel = do_excel.DoExcel(contants.case_file, 'planTasks')  # 测试planTasks接口
    cases = excel.get_case()

    @classmethod
    def setUpClass(cls):
        logger.info('准备测试前置')
        cls.http_request = HttpRequest2()

    @data(*cases)
    def test_plan_tasks(self, case):
        logger.info('开始测试{}'.format(case.title))
        resp = self.http_request.http_request(case.method, case.url, case.data)
        results = json.loads(resp.text)
        print('返回结果:', results)
        try:
            self.assertEqual(ast.literal_eval(case.expected),
                             results["success"])
            self.excel.writer_result(case.case_id + 1, resp.text, "PASS")

            # 判断有查询到作业时,取到planID 和 textbookID
            planId = results["data"]["list"][0]['planId']
            logger.info("planID:{}".format(planId))
            print("planID:", planId)
            textbookId = results["data"]["list"][0]['textbookId']
            logger.info("textbookId:{}".format(textbookId))
            print("textbookId:", textbookId)
            setattr(Context, 'planId', str(planId))  # 将planId写入到类属性里
            setattr(Context, 'textbookId',
                    str(textbookId))  # 将textbookID写入到类属性里
        except ValueError as e:
            self.excel.writer_result(case.case_id + 1, resp.text, "FAIL")
            logger.error('报错了{}'.format(e))
            raise e
        logger.info('测试结束:{}'.format(case.title))

    @classmethod
    def tearDownClass(cls):
        logger.info('测试后置处理')
        cls.http_request.close()
示例#14
0
class LoginTest(unittest.TestCase):
    excel = do_excel.DoExcel(file_adress.data_path, 'login')
    cases = excel.get_cases()

    @classmethod
    def setUpClass(cls):
        cls.http_request = HttpRequest()

    @data(*cases)
    def test_login(self, case):
        logger.info("测试的title{}".format(case.title))
        res = self.http_request.request(case.method, case.url, case.data)
        try:
            self.assertEqual(case.expected, res.json()['code'])
            self.excel.write_result(case.case_id + 1, res.text, 'PASS')
        except AssertionError as e:
            self.excel.write_result(case.case_id + 1, res.text, 'Fail')
            logger.error("测试报错了{}", format(e))
            raise e

    @classmethod
    def tearDown(cls):
        cls.http_request.close()
示例#15
0
class RegisterTest(unittest.TestCase):
    do_excel = do_excel.DoExcel(contants.case_file, "register")
    cases = do_excel.get_cases()
    do_mysql = do_mysql.DoMySql()

    @classmethod
    def setUpClass(cls):
        logger.debug("准备测试前置")
        cls.http_request = http_request.HttpRequest2()

    @data(*cases)
    def test_register(self, case):
        logger.debug("开始测试:{0}".format(case.title))
        # 加入数据库的查询
        # 如何将数据库中查询到的数据,更新到测试用例中呢?测试数据做参数化,作为标识
        if case.data.find("register_mobile") > -1:
            sql = 'select max(MobilePhone) phone from future.member'
            result = self.do_mysql.fetch_one(sql)
            max_phone = result["phone"]  # 获取最大的手机号,str
            max_phone = int(max_phone) + 1
            # 使用字符串的replace方法,指定替换
            case.data = case.data.replace("register_mobile", str(max_phone))
            # print(case.data)

        resp = self.http_request.request(case.method, case.url, case.data)
        actual = resp.text
        try:
            self.assertEqual(case.exspected, actual)  # 判断预期结果和实际结果是否相等
            # 断言出错,就不执行后面的语句
            self.do_excel.write_result(case.case_id + 1, actual, "PASS")
        except AssertionError as e:
            self.do_excel.write_result(case.case_id + 1, actual, "Fail")
            logger.error('\033[0;31m报错了:{}\033[0m'.format(e))  #颜色不起作用
            raise e  # 抛出异常,但不影响后面用例的执行
        else:
            logger.debug("结束测试:{}".format(case.title))
示例#16
0
class PlanTasksTest(unittest.TestCase):
    excel = do_excel.DoExcel(contants.case_file, 'playPlan')  # 测试add接口
    cases = excel.get_case()

    @classmethod
    def setUpClass(cls):
        logger.info('准备测试前置')
        cls.http_request = HttpRequest2()
        cls.mysql = do_mysql.DoMysql()

    @data(*cases)
    def test_plan_tasks(self, case):
        logger.info('开始测试{}'.format(case.title))
        case.data = context.replace(case.data)
        print("case.data:", case.data)
        resp = self.http_request.http_request(case.method, case.url, case.data)
        results = json.loads(resp.text)
        print('返回结果:', results)
        try:
            self.assertEqual(ast.literal_eval(case.expected),
                             results["success"])
            self.excel.writer_result(case.case_id + 1, resp.text, "PASS")

            # 判断添加作业成功后,查询数据库,取到homeworkId/也可以在返回结果中取到homeworkId
            try:
                if resp.json()["data"]["homeworkId"]:
                    homeworkId = results["data"]["homeworkId"]
                    print('homeworkId:', homeworkId)
                    # 保存到类属性里面
                    setattr(Context, "homeworkId", homeworkId)
            except KeyError:
                pass
            # 判断添加到我的作业簿后,查询到planID并将planId保存到类属性中
            try:
                if resp.json()["message"]:
                    logger.info('查询数据库中的planId')
                    homeworkid = getattr(Context, "homeworkId")
                    print('homeworkId:', homeworkid)
                    sql = f"SELECT plan_id FROM alading_jdcs.al_en_plan WHERE homework_id = '{homeworkid}';"
                    planId = self.mysql.fetch_one(sql)[
                        'plan_id']  # 我设置的MySQL查询是以字典形式查询的,返回结果为字典
                    print('planId:', planId)
                    # 保存到类属性中去
                    setattr(Context, "planId", planId)
            except KeyError:
                pass
        except ValueError as e:
            self.excel.writer_result(case.case_id + 1, resp.text, "FAIL")
            logger.error('报错了{}'.format(e))
            raise e
        logger.info('测试结束:{}'.format(case.title))

    @classmethod
    def tearDownClass(cls):
        logger.info('测试后置处理')
        logger.info('暂停150S,等待小闹闹完成作业')
        homeworkid = getattr(Context, "homeworkId")
        print("homework:", homeworkid)
        complete = 0
        while complete == 0:
            sql = f"SELECT complete FROM alading_jdcs.al_en_plan WHERE homework_id = '{homeworkid}';"
            complete = cls.mysql.fetch_one(sql)['complete']
            print("complete:", complete)
            time.sleep(25)
        # time.sleep(150)  # 作业需要100s才能完成
        cls.http_request.close()