【发布时间】:2018-12-03 11:29:58
【问题描述】:
我有一个当前需要 3 个参数的 shell 脚本。我通过带有 shell 脚本文件名、运行 python 脚本的目录以及测试数据目录的名称的 shell 脚本运行它。我希望能够编写一个执行以下命令的单元测试,但前提是我要更改日期,这取决于可用的数据,它会通过或失败。
main_config.sh
yamldir=$1
for yaml in $(ls ${yamldir}/*.yaml | grep -v "export_config.yaml"); do
if [ "$yaml" != "export_config.yaml" ]; then
echo "Running export for $yaml file...";
python valid.py -p ${yamldir}/export_config.yaml -e $yaml -d ${endDate}
wait
fi
done
这是在命令行上执行的内容
./main_config.sh /Users/name/Desktop/yaml/ 2018-12-23
这将失败并在终端上输出,因为没有名为 2012-12-23 的目录:
./main_config.sh /yaml/ 2018-12-23
Running export for apa.yaml file...
apa.json does not exist
如果目录存在,这将通过并在终端上输出:
Running export for apa.yaml file...
File Name: apa.json Exists
File Size: 234 Bytes
Writing to file
我的python脚本脚本如下:
def main(get_config):
cfg = get_config()[0] # export_config.yaml
data = get_config()[1] # export_apa.yaml
date = get_config()[2] # data folder - YYYY-MM-DD
# Conditional Logic
def get_config():
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--parameter-file", action="store", required=True)
parser.add_argument("-e", "--export-data-file", action="store", required=True)
parser.add_argument("-d", "--export-date", action="store", required=False)
args = parser.parse_args()
return [funcs.read_config(args.parameter_file), funcs.read_config(args.export_data_file), args.export_date]
if __name__ == "__main__":
logging.getLogger().setLevel(logging.INFO)
main(get_config)
【问题讨论】:
-
你能说得更具体点吗?编写测试时遇到什么麻烦?
-
更多的是编写测试的方法。逻辑。我一直在阅读,但这不是测试功能,而是集成测试。 @JETM
标签: python python-2.7 shell unit-testing pytest