我想为我的python代码编写单元测试

时间:2019-03-25 06:07:54

标签: python mocking

我下面有一些代码,我必须为此编写一个单元测试,但是我对此确实不确定,因为该方法在函数定义中未使用任何参数,而且我真的不知道如何传递参数值。编写单元测试时的变量。 正如我们在下面的代码中看到的,我们有4个变量,其值将由邮递员请求发布:

  1. access_token
  2. data_separator
  3. fpath

我想测试该功能是否正常运行,是否能够读取文本文件,对其进行处理并发回return_value。

def b():
    """
    save uploaded document
    """
    return_value = {"status": "fail"}
    try:
        access_token = request.form["access_token"]
        decoded = decode_token(access_token)
        data_separator = request.form["data_separator"]
        fpath = _save_input_file_(decoded["AccountUId"])
        # extract_to = os.path.join(_get_client_tmp_dir_(), 'extract')
        # if not es.uncompress(fpath, extract_to):
        #     msg = 'error while attempting to uncompress! file: %s' 
        #     logger.error(msg)
        #     return json.dumps({'msg': 'failure'})
        df = None
        if os.path.isfile(fpath):
            logger.debug("saved is file. reading file into dataframe")
            df = pd.read_csv(fpath, names=[cf.COLUMN1,cf.COLUMN2], sep=data_separator)
        elif os.path.isdir(fpath):
# read dir in a for loop
            pass
        (train_x, test_x, train_y, test_y) = _determine_features_(df, decoded["AccountUId"])
        score = _train_model_and_persist_(train_x, test_x, train_y, test_y,decoded["AccountUId"])
        return_value = {"status":"success", "accuracy":score}
    except jwt.exceptions.DecodeError:
        logger.exception("An error occured")
        return_value["msg"] = "Invalid token. Error while decoding"

    return json.dumps(return_value)

0 个答案:

没有答案