Exemple #1
0
 def test_unlock_cities(self, board):
     for city in board.locations:
         city.lock_infection()
     board.unlock_cities()
     assume(
         list(filter(lambda city: city.locked_infection, board.locations))
         == [])
Exemple #2
0
    def test_lock_unlock_infection(self, city):
        city.lock_infection()
        assume(city.locked_infection)

        city.infect(amount=1)
        assume(city.infections == [0] * 4)

        city.unlock_infection()
        assume(not city.locked_infection)

        city.infect(amount=1)
        assume(
            city.infections == [0 if i != city.color else 1 for i in range(4)])
def test_add2():
    # AssumeContextManager()(1 == 1)
    # AssumeContextManager()(1 == 2)
    # AssumeContextManager()(1 == 1)
    # AssumeContextManager()(1 == 2)

    # assert 1==2
    # assert 1==1

    assume(1 == 1)
    assume(1 == 2)
    assume(1 == 1)
    assume(1 == 2)
    print("测试完成")
Exemple #4
0
 def test_draw(self, reduced_deck: Deck):
     for i in range(5):
         _card = reduced_deck.draw_card()
         assume(_card == reduced_deck.drawn[i])
Exemple #5
0
 def test_eq(self, city, other, result):
     assume((city == other) == result)
     city.infect(1)
     city.connections.append(other)
     assume((city == other) == result)
Exemple #6
0
 def test_initialize_board(self, board):
     assume(board.research_centers == [board.locations[0]])
     assume(len(board.locations) == 10)
     assume(len(board.locations[0].connections) > 0)
Exemple #7
0
 def test_init(self):
     board = Board()
     assume(board.locations == [])
     assume(board.research_centers == [])
     assume(board.current_speed == 0)
     assume(board.infection_speeds == [2, 2, 2, 3, 3, 4, 4])
Exemple #8
0
    def test_demo(self, temporary_dict):
        """
        测试用例
        :param temporary_dict:
        :return:
        """

        global mysql_result_list_after, pgsql_result_list_after, \
            mongo_result_list_after, temporary_list

        temporary_dict = str(temporary_dict)
        if "None" in temporary_dict:
            temporary_dict = temporary_dict.replace("None", "''")
        temporary_dict = demjson.decode(temporary_dict)
        # 把值为None的替换成''空字符串,因为None无法拼接
        # demjson.decode()等价于json.loads()反序列化

        case_name = temporary_dict.get("case_name")
        # 用例名称
        self.test_demo.__func__.__doc__ = case_name
        # 测试报告里面的用例描述
        step = temporary_dict.get("step")
        # 步骤列表
        logger.info("**********{}>>>开始执行**********\n", case_name)

        for item in step:
            step_name = item.get("step_name")
            # 步骤名称
            mysql = item.get("mysql")
            # mysql语句
            pgsql = item.get("pgsql")
            # pgsql语句
            mongo = item.get("mongo")
            # mongo语句
            request_mode = item.get("request_mode")
            # 请求方式
            api = item.get("api")
            # 接口路径
            if type(api) != str:
                api = str(api)
            payload = item.get("body")
            # 请求体
            file = item.get("file")
            # 文件
            if payload:
                if type(payload) != str:
                    payload = str(payload)
            headers = item.get("headers")
            # 请求头
            if headers:
                if type(headers) != str:
                    headers = str(headers)
            query_string = item.get("query_string")
            # 请求参数
            if query_string:
                if type(query_string) != str:
                    query_string = str(query_string)
            expected_time = item.get("expected_time")
            # 预期的响应时间
            if expected_time:
                if type(expected_time) != float:
                    expected_time = float(expected_time)
            expected_code = item.get("expected_code")
            # 预期的响应代码
            expected_result = item.get("expected_result")
            # 预期的响应结果
            if type(expected_result) != str:
                expected_result = str(expected_result)
            regular = item.get("regular")
            # 正则

            logger.info("步骤名称为:{}", step_name)
            if environment == "formal" and mysql or \
                    environment == "formal" and pgsql or \
                    environment == "formal" and mongo:
                pytest.skip("跳过生产环境,请忽略")
            # 生产环境不能连接MySQL数据库或者PostgreSQL数据库或者Mongo数据库,因此跳过

            if self.variable_result_dict:
                # 如果变量名与提取的结果字典不为空
                if mysql:
                    if mysql[0]:
                        mysql[0] = function_dollar(
                            mysql[0], self.variable_result_dict.items())
                    # 调用替换$的方法
                    if mysql[1]:
                        mysql[1] = function_dollar(
                            mysql[1], self.variable_result_dict.items())
                    if mysql[2]:
                        mysql[2] = function_dollar(
                            mysql[2], self.variable_result_dict.items())
                if pgsql:
                    if pgsql[0]:
                        pgsql[0] = function_dollar(
                            pgsql[0], self.variable_result_dict.items())
                    # 调用替换$的方法
                    if pgsql[1]:
                        pgsql[1] = function_dollar(
                            pgsql[1], self.variable_result_dict.items())
                    if pgsql[2]:
                        pgsql[2] = function_dollar(
                            pgsql[2], self.variable_result_dict.items())
                if mongo:
                    if mongo[0]:
                        if mongo[0][2]:
                            if type(mongo[0][2]) != str:
                                mongo[0][2] = str(mongo[0][2])
                            mongo[0][2] = function_dollar(
                                mongo[0][2], self.variable_result_dict.items())
                            # 调用替换$的方法
                    if mongo[1]:
                        if mongo[1][1]:
                            if type(mongo[1][1]) != str:
                                mongo[1][1] = str(mongo[1][1])
                            mongo[1][1] = function_dollar(
                                mongo[1][1], self.variable_result_dict.items())
                    if mongo[2]:
                        if mongo[2][1]:
                            if type(mongo[2][1]) != str:
                                mongo[2][1] = str(mongo[2][1])
                            mongo[2][1] = function_dollar(
                                mongo[2][1], self.variable_result_dict.items())
                if api:
                    api = function_dollar(api,
                                          self.variable_result_dict.items())
                if payload:
                    payload = function_dollar(
                        payload, self.variable_result_dict.items())
                if headers:
                    headers = function_dollar(
                        headers, self.variable_result_dict.items())
                if query_string:
                    query_string = function_dollar(
                        query_string, self.variable_result_dict.items())
                if expected_result:
                    expected_result = function_dollar(
                        expected_result, self.variable_result_dict.items())
            else:
                pass

            if mysql:
                db = ConnectMySQL()
                # 实例化一个MySQL操作对象
                if mysql[0]:
                    mysql[0] = function_rn(mysql[0])
                    # 调用替换RN随机数字的方法
                    mysql[0] = function_rl(mysql[0])
                    # 调用替换RL随机字母的方法
                    mysql[0] = function_mp(mysql[0])
                    # 调用替换MP随机手机号码的方法
                    mysql[0] = function_rd(mysql[0])
                    # 调用替换RD随机日期时间的方法
                    if "INSERT" in mysql[0] or "insert" in mysql[0]:
                        db.insert_mysql(mysql[0])
                        # 调用插入mysql的方法
                        sleep(2)
                        # 等待2秒钟
                    if "UPDATE" in mysql[0] or "update" in mysql[0]:
                        db.update_mysql(mysql[0])
                        # 调用更新mysql的方法
                        sleep(2)
                    if "DELETE" in mysql[0] or "delete" in mysql[0]:
                        db.delete_mysql(mysql[0])
                        # 调用删除mysql的方法
                        sleep(2)
                if mysql[1]:
                    if "SELECT" in mysql[1] or "select" in mysql[1]:
                        mysql_result_tuple = db.query_mysql(mysql[1])
                        # mysql查询结果元祖
                        if mysql_result_tuple:
                            mysql_result_list = list(
                                chain.from_iterable(mysql_result_tuple))
                            # 把二维元祖转换为一维列表
                            mysql_result_list = data_conversion_string(
                                mysql_result_list)
                            # 调用数据类型转换的方法
                            logger.info("发起请求之前mysql查询的结果列表为:{}",
                                        mysql_result_list)
                            if api:
                                api = function_sql(api, mysql_result_list)
                                # 调用替换MySQL查询结果的方法
                            if payload:
                                payload = function_sql(payload,
                                                       mysql_result_list)
                            if headers:
                                headers = function_sql(headers,
                                                       mysql_result_list)
                            if query_string:
                                query_string = function_sql(
                                    query_string, mysql_result_list)
                            if expected_result:
                                expected_result = function_sql(
                                    expected_result, mysql_result_list)

            if pgsql:
                pgsql_db = ConnectPostgreSQL()
                # 实例化一个PostgreSQL操作对象
                if pgsql[0]:
                    pgsql[0] = function_rn(pgsql[0])
                    # 调用替换RN随机数字的方法
                    pgsql[0] = function_rl(pgsql[0])
                    # 调用替换RL随机字母的方法
                    pgsql[0] = function_mp(pgsql[0])
                    # 调用替换MP随机手机号码的方法
                    pgsql[0] = function_rd(pgsql[0])
                    # 调用替换RD随机日期时间的方法
                    if "INSERT" in pgsql[0] or "insert" in pgsql[0]:
                        pgsql_db.insert_postgresql(pgsql[0])
                        # 调用插入pgsql的方法
                        sleep(2)
                        # 等待2秒钟
                    if "UPDATE" in pgsql[0] or "update" in pgsql[0]:
                        pgsql_db.update_postgresql(pgsql[0])
                        # 调用更新pgsql的方法
                        sleep(2)
                    if "DELETE" in pgsql[0] or "delete" in pgsql[0]:
                        pgsql_db.delete_postgresql(pgsql[0])
                        # 调用删除pgsql的方法
                        sleep(2)
                if pgsql[1]:
                    if "SELECT" in pgsql[1] or "select" in pgsql[1]:
                        pgsql_result_tuple = pgsql_db.query_postgresql(
                            pgsql[1])
                        # PostgreSQL查询结果元祖
                        if pgsql_result_tuple:
                            pgsql_result_list = list(
                                chain.from_iterable(pgsql_result_tuple))
                            # 把二维元祖转换为一维列表
                            logger.info("发起请求之前PostgreSQL查询的结果列表为:{}",
                                        pgsql_result_list)
                            if api:
                                api = function_pgsql(api, pgsql_result_list)
                                # 调用替换PostgreSQL查询结果的方法
                            if payload:
                                payload = function_pgsql(
                                    payload, pgsql_result_list)
                            if headers:
                                headers = function_pgsql(
                                    headers, pgsql_result_list)
                            if query_string:
                                query_string = function_pgsql(
                                    query_string, pgsql_result_list)
                            if expected_result:
                                expected_result = function_pgsql(
                                    expected_result, pgsql_result_list)

            if mongo:
                mongo_db = ConnectMongo()
                # 实例化一个Mongo操作对象
                if mongo[0]:
                    if mongo[0][2]:
                        if type(mongo[0][2]) != str:
                            mongo[0][2] = str(mongo[0][2])
                        mongo[0][2] = function_rn(mongo[0][2])
                        mongo[0][2] = function_rl(mongo[0][2])
                        mongo[0][2] = function_mp(mongo[0][2])
                        mongo[0][2] = function_rd(mongo[0][2])
                        if type(mongo[0][2]) != dict:
                            mongo[0][2] = demjson.decode(mongo[0][2])
                    if mongo[0][1] == "insert":
                        if mongo[0][2]:
                            if type(mongo[0][2]) == dict:
                                mongo_db.insert_mongo_one(
                                    mongo[0][0], mongo[0][2])
                                # 调用插入Mongo(一条数据)的方法
                            if type(mongo[0][2]) == list:
                                mongo_db.insert_mongo_many(
                                    mongo[0][0], mongo[0][2])
                                # 调用插入Mongo(多条数据)的方法
                        sleep(2)
                    if mongo[0][1] == "remove":
                        if mongo[0][2]:
                            mongo_db.delete_mongo_one(mongo[0][0], mongo[0][2])
                            # 调用删除Mongo(一条数据)的方法
                        sleep(2)
                    if mongo[0][1] == "update":
                        if mongo[0][2]:
                            mongo_db.update_mongo_one(mongo[0][0],
                                                      *mongo[0][2])
                            # 调用更新Mongo(一条数据)的方法
                        sleep(2)
                if mongo[1]:
                    mongo_result_dict = mongo_db.query_mongo_one(
                        mongo[1][0], *mongo[1][1])
                    # 调用查询Mongo(一条数据)的方法
                    if mongo_result_dict:
                        mongo_result_list = list(mongo_result_dict.values())
                        # 把字典转换成列表
                        logger.info("发起请求之前mongo查询的结果列表为:{}",
                                    mongo_result_list)
                        if api:
                            api = function_mongo(api, mongo_result_list)
                            # 调用替换Mongo查询结果的方法
                        if payload:
                            payload = function_mongo(payload,
                                                     mongo_result_list)
                        if headers:
                            headers = function_mongo(headers,
                                                     mongo_result_list)
                        if query_string:
                            query_string = function_mongo(
                                query_string, mongo_result_list)
                        if expected_result:
                            expected_result = function_mongo(
                                expected_result, mongo_result_list)

            if api:
                api = function_rn(api)
                api = function_rl(api)
            if payload:
                payload = function_rn(payload)
                payload = function_rl(payload)
                payload = function_mp(payload)
                payload = function_rd(payload)
                payload = demjson.decode(payload)
            if headers:
                headers = function_rn(headers)
                headers = function_rl(headers)
                headers = function_mp(headers)
                headers = function_rd(headers)
                headers = demjson.decode(headers)
            if query_string:
                query_string = function_rn(query_string)
                query_string = function_rl(query_string)
                query_string = function_mp(query_string)
                query_string = function_rd(query_string)
                query_string = demjson.decode(query_string)
            if expected_result:
                expected_result = demjson.decode(expected_result)

            url = service_domain + api
            # 拼接完整地址

            logger.info("请求方式为:{}", request_mode)
            logger.info("请求地址为:{}", url)
            if payload:
                logger.info("请求体为:{}", json.dumps(payload, ensure_ascii=False))
            if headers:
                logger.info("请求头为:{}", json.dumps(headers, ensure_ascii=False))
            if query_string:
                logger.info("请求参数为:{}",
                            json.dumps(query_string, ensure_ascii=False))
            if expected_time:
                logger.info("预期的响应时间为:{}秒", expected_time)
            logger.info("预期的响应代码为:{}", expected_code)
            logger.info("预期的响应结果为:{}",
                        json.dumps(expected_result, ensure_ascii=False))

            self.test_case_data_list.append(
                (case_name, step_name, request_mode, url,
                 json.dumps(payload, ensure_ascii=False),
                 json.dumps(headers, ensure_ascii=False),
                 json.dumps(query_string,
                            ensure_ascii=False), expected_time, expected_code,
                 json.dumps(expected_result, ensure_ascii=False)))
            # 把用例数据添加到测试用例数据列表

            try:
                if file:
                    # 如果file字段不为空
                    files = {
                        file[0]: (file[1], open(yaml_path + "/" + file[1],
                                                'rb'), file[2], {
                                                    'Expires': '0'
                                                })
                    }
                    response = requests.request(request_mode,
                                                url,
                                                files=files,
                                                data=json.dumps(payload),
                                                headers=headers,
                                                params=query_string,
                                                timeout=(15, 20))
                    # 上传文件
                else:
                    response = requests.request(request_mode,
                                                url,
                                                data=json.dumps(payload),
                                                headers=headers,
                                                params=query_string,
                                                timeout=(15, 20))
                    # 发起HTTP请求
                    # json.dumps()序列化把字典转换成字符串,json.loads()反序列化把字符串转换成字典
                    # data请求体为字符串,headers请求头与params请求参数为字典
            except Exception as e:
                logger.error("HTTP请求发生错误:{}", e)
                raise e

            try:
                actual_time = response.elapsed.total_seconds()
                logger.info("实际的响应时间为:{}秒", actual_time)
            except Exception as e:
                logger.error("获取实际的响应时间发生错误:{}", e)
                raise e
            try:
                actual_code = response.status_code
                logger.info("实际的响应代码为:{}", actual_code)
            except Exception as e:
                logger.error("获取实际的响应代码发生错误:{}", e)
                raise e
            try:
                actual_headers = response.headers
                logger.info("实际的响应头为:{}", actual_headers)
            except Exception as e:
                logger.error("获取实际的响应头发生错误:{}", e)
                raise e
            try:
                actual_result_text = response.text
                logger.info("实际的响应结果为:{}", actual_result_text)
            except Exception as e:
                logger.error("获取实际的响应结果发生错误:{}", e)
                raise e

            if influxdb_switch:
                influx_db = ConnectInflux()
                # 实例化一个InfluxDB操作对象
                influx_db.insert_influx_one(
                    environment, case_name, step_name, request_mode, url,
                    expected_time, actual_time, expected_code, actual_code,
                    json.dumps(expected_result,
                               ensure_ascii=False), actual_result_text)
                # 往InfluxDB插入一条数据

            if mysql:
                if mysql[2]:
                    if "SELECT" in mysql[2] or "select" in mysql[2]:
                        db_after = ConnectMySQL()
                        mysql_result_tuple_after = db_after.query_mysql(
                            mysql[2])
                        if mysql_result_tuple_after:
                            mysql_result_list_after = list(
                                chain.from_iterable(mysql_result_tuple_after))
                            mysql_result_list_after = data_conversion_string(
                                mysql_result_list_after)
                            logger.info("发起请求之后mysql查询的结果列表为:{}",
                                        mysql_result_list_after)
                            mysql_result_list_after = list(
                                map(str, mysql_result_list_after))
                            # 把列表里面的元素类型全部改为str
            if pgsql:
                if pgsql[2]:
                    if "SELECT" in pgsql[2] or "select" in pgsql[2]:
                        pgsql_db_after = ConnectPostgreSQL()
                        pgsql_result_tuple_after = pgsql_db_after.query_postgresql(
                            pgsql[2])
                        if pgsql_result_tuple_after:
                            pgsql_result_list_after = list(
                                chain.from_iterable(pgsql_result_tuple_after))
                            logger.info("发起请求之后PostgreSQL查询的结果列表为:{}",
                                        pgsql_result_list_after)
                            pgsql_result_list_after = list(
                                map(str, pgsql_result_list_after))
                            # 把列表里面的元素类型全部改为str
            if mongo:
                if mongo[2]:
                    mongo_db_after = ConnectMongo()
                    mongo_result_dict_after = mongo_db_after.query_mongo_one(
                        mongo[2][0], *mongo[2][1])
                    if mongo_result_dict_after:
                        mongo_result_list_after = list(
                            mongo_result_dict_after.values())
                        logger.info("发起请求之后mongo查询的结果列表为:{}",
                                    mongo_result_list_after)
                        mongo_result_list_after = list(
                            map(str, mongo_result_list_after))
                        # 把列表里面的元素类型全部转为str

            if regular:
                # 如果正则不为空
                extract_list = []
                # 定义一个提取结果列表
                for i in regular["expression"]:
                    regular_result_list = re.findall(i, actual_result_text)
                    # re.findall(正则表达式, 实际的响应结果)返回一个符合规则的list,取第1个
                    if regular_result_list:
                        regular_result = regular_result_list[0]
                        extract_list.append(regular_result)
                        # 把提取结果添加到提取结果列表里面
                variable_result_dict_temporary = dict(
                    zip(regular["variable"], extract_list))
                # 把变量列表与提取结果列表转为一个临时字典
                for key, value in variable_result_dict_temporary.items():
                    self.variable_result_dict[key] = value
                # 把临时字典合并到变量名与提取的结果字典,已去重
            else:
                pass

            if self.variable_result_dict:
                for key in list(self.variable_result_dict.keys()):
                    if not self.variable_result_dict[key]:
                        del self.variable_result_dict[key]
                # 删除变量名与提取的结果字典中为空的键值对

            expected_result = re.sub(
                "{|}|\'|\"|\\[|\\]| ", "",
                json.dumps(expected_result, ensure_ascii=False))
            # 去除大括号{、}、单引号'、双引号"、中括号[、]与空格
            if actual_result_text:
                actual_result_text = re.sub("{|}|\'|\"|\\[|\\]| ", "",
                                            actual_result_text)
            expected_result_list = re.split(":|,", expected_result)
            # 把文本转为列表,并去除:与,
            actual_result_list = re.split(":|,", actual_result_text)
            logger.info("切割之后预期的响应结果列表为:{}", expected_result_list)
            if actual_result_list:
                logger.info("切割之后实际的响应结果列表为:{}", actual_result_list)

            if expected_code == actual_code:
                # 如果预期的响应代码等于实际的响应代码
                if set(expected_result_list) <= set(actual_result_list):
                    logger.info("{}>>>预期的响应结果与实际的响应结果断言成功", step_name)
                else:
                    logger.error("{}>>>预期的响应结果与实际的响应结果断言失败!!!", step_name)
                assume(set(expected_result_list) <= set(actual_result_list))
                if mysql:
                    if mysql[2]:
                        if set(mysql_result_list_after) <= set(
                                actual_result_list):
                            logger.info("{}>>>发起请求之后mysql查询结果与实际的响应结果断言成功",
                                        step_name)
                        else:
                            logger.error("{}>>>发起请求之后mysql查询结果与实际的响应结果断言失败!!!",
                                         step_name)
                        assume(
                            set(mysql_result_list_after) <= set(
                                actual_result_list))
                if pgsql:
                    if pgsql[2]:
                        if set(pgsql_result_list_after) <= set(
                                actual_result_list):
                            logger.info("{}>>>发起请求之后pgsql查询结果与实际的响应结果断言成功",
                                        step_name)
                        else:
                            logger.error("{}>>>发起请求之后pgsql查询结果与实际的响应结果断言失败!!!",
                                         step_name)
                        assume(
                            set(pgsql_result_list_after) <= set(
                                actual_result_list))
                if mongo:
                    if mongo[2]:
                        if set(mongo_result_list_after) <= set(
                                actual_result_list):
                            logger.info("{}>>>发起请求之后mongo查询结果与实际的响应结果断言成功",
                                        step_name)
                        else:
                            logger.error("{}>>>发起请求之后mongo查询结果与实际的响应结果断言失败!!!",
                                         step_name)
                        assume(
                            set(mongo_result_list_after) <= set(
                                actual_result_list))
                if expected_time:
                    if actual_time <= expected_time:
                        logger.info("{}>>>实际的响应时间与预期的响应时间断言成功", step_name)
                    else:
                        logger.error("{}>>>实际的响应时间大于预期的响应时间!!!", step_name)
                    assume(actual_time <= expected_time)
                logger.info("##########步骤分隔符##########\n")
                # 多重断言
                # 预期的响应结果与实际的响应结果是被包含关系
                # 发起请求之后mysql查询结果与实际的响应结果是被包含关系
                # 发起请求之后pgsql查询结果与实际的响应结果是被包含关系
                # 发起请求之后mongo查询结果与实际的响应结果是被包含关系
                # 实际的响应时间应该小于或者等于预期的响应时间
            else:
                logger.error("{}>>>执行失败!!!", step_name)
                logger.error("预期的响应代码与实际的响应代码不相等:{}!={}", expected_code,
                             actual_code)
                logger.info("##########步骤分隔符##########\n")

            assume(expected_code == actual_code)
        logger.info("**********{}>>>执行结束**********\n", case_name)