Pytest创建临时CSV文件来读取

时间:2018-04-20 20:43:40

标签: python-3.x csv pytest

我有一些代码根据传入的id号解析某些特定值的csv。为了测试,我想使用pytest为CSV创建虚拟数据文件。

@pytest.fixture
def client():
    return Client('FOO, BAR', 98763, 1986, '08/19/91', 'MALE')

@pytest.fixture(scope='session')
def file_name(tmpdir_factory):
    fn = tmpdir_factory.mktemp('data'.join('csvNg.csv'))
    headers = ['NAME','PN','MRN','DOB','GENDER']
    with open(fn,'w') as csvfile:
        writer = csv.Dict(csvfile,fieldnames=headers)
        writer.writeheader()
        writer.writerow({'NAME': 'FOO, BAR', 'PN': 98763, 'MRN': 1986, 
            'DOB': '08/19/91', 'GENDER': 'MALE' })
    return str(fn)

def test_locate_ngen():
    FOOBAR = client()
    assert locate_ngen(1986,file_name) == FOOBAR

运行时遇到的当前错误:

  

@ pytest.fixture(scope =' session')E IsADirectoryError:[Errno 21] Is   目录:   ' /private/var/folders/mn/xg5s_2rd5tbd9hs_ckgwzvtmkqnwq5/T/pytest-of-ronswanson/pytest-4/cdatasdatavdataNdatagdata.datacdatasdatav0'

1 个答案:

答案 0 :(得分:0)

fn = tmpdir_factory.mktemp('data').join('csvNg.csv')