【发布时间】:2020-04-13 14:02:14
【问题描述】:
我编写了一个 Python 函数,它为数据集生成文件路径。我想为该函数添加一个文档测试。但是,由于每台机器都有不同的相对文件路径,我不确定如何编写测试以便它可以在任何机器上通过。
import os
def get_dataset_file_path(date, filename):
"""Produces a filepath for the dataset.
:parameter date (string): The date folder name. Ex: "2020-02-05"
:parameter filename (string): The csv filename.
:returns filepath (string): The filepath for the dataset.
Example:
project_root
├── README.md
├── data
│ └── 2020-04-13
│ ├── README.md
│ ├── data_description.txt
│ ├── test.csv
│ └── train.csv
├── docs
├── requirements.yml
└── results
└── 2020-04-13
└── runall.py
The function is called from the 'runall.py' file.
>>> get_data_file_path('2020-04-13', 'train.csv')
'~/project_root/data/2020-04-13/train.csv'
"""
basepath = os.path.abspath('')
filepath = os.path.abspath(os.path.join(basepath, "..", "..")) + "/data/" + date + "/" + filename
return filepath
【问题讨论】:
-
有不同的测试理念:这个问题可能过于笼统,无法具体回答。你想测试一个可以专门编码的特定事物吗?