2017-09-02 50 views
1

我试图用pytest测试文件解析。我有一个看起来像这样为我的项目的目录树:如何正确整合用pytest进行文件解析的单元测试?

project 
    project/ 
     cool_code.py 
    setup.py 
    setup.cfg 
    test/ 
     test_read_files.py 
     test_files/ 
      data_file1.txt 
      data_file2.txt 

我的setup.py文件看起来是这样的:

from setuptools import setup 

setup(
    name   = 'project', 
    description = 'The coolest project ever!', 
    setup_requires = ['pytest-runner'], 
    tests_require = ['pytest'], 
    ) 

我setup.cfg文件看起来是这样的:

[aliases] 
test=pytest 

我用pytest编写了几个单元测试来验证文件是否正确读取。当我从“test”目录中运行pytest时,它们工作正常。但是,如果我执行从我的项目目录下面的任何,测试失败,因为他们不能在test_files寻找数据文件:

>> py.test 
>> python setup.py pytest 

测试似乎是从其中执行pytest目录敏感。

如何从测试目录或项目根目录中调用pytest单元测试以发现“data_files”中的文件进行解析?

+0

这取决于你怎么写你的setup.py所以你可以请你发布setup.py的内容。 – suvy

+0

这个问题用我正在使用的setup.py文件设置进行了更新。 –

+0

更新了答案,它工作吗? – suvy

回答

1

一种解决方案是限定rootdir夹具与所述路径到测试目录,并引用所有数据相对于该文件。这可以通过创建一个test/conftest.py来完成(如果尚未创建),还有一些像这样的代码:

import os 
import pytest 

@pytest.fixture 
def rootdir(): 
    return os.path.dirname(os.path.abspath(__file__)) 

然后使用你的测试os.path.join获得绝对路径来测试文件:

import os 

def test_read_favorite_color(rootdir): 
    test_file = os.path.join(rootdir, 'test_files/favorite_color.csv') 
    data = read_favorite_color(test_file) 
    # ... 
+0

我认为这是比我建议的更好的方法。 –

-1

一种方法是将命令名和自定义命令类的字典传递给cmdclass参数setup函数。

另一种方式就像here,贴在这里以供快速参考。

pytest-runner会在每次调用setup.py时自行安装。在某些情况下,这会导致不会调用pytest-runner的setup.py的调用延迟。为了避免这种意外,考虑要求pytest亚军被调用pytest只有当:

pytest = {'pytest', 'test', 'ptr'}.intersection(sys.argv) 

pytest_runner = ['pytest-runner'] if needs_pytest else [] 

# ... 

setup(
    #... 
    setup_requires=[ 
     #... (other setup requirements) 
    ] + pytest_runner, 
) 

确保你的测试模块中读取的数据是相对于setup.py目录的位置。

在OP的情况下,数据文件路径将是test/test_files/data_file1.txt, 我做了相同结构的项目,并读取data_file1.txt在一些文本,它为我工作。

+1

谢谢你的帮助。我不确定我是否明白如何解决这个问题。你能详细说明为什么这将允许测试查看子目录中的文件吗? –

+0

哦,我意识到我可能误解了这个问题,你的意思是pytest中看不到数据文件? – suvy

+0

是的,测试需要一个驻留在test_files中的文件名,并测试我的解析函数是否返回期望值。我的测试无法在根目录中执行时找到给定的文件名。 –

0

一种解决方法是尝试多个路径来查找文件。

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

from coolprogram import * 
import os 


def test_file_locations(): 
    """Possible locations where test data could be found.""" 

    return(['./test_files', 
      './tests/test_files', 
      ]) 


def find_file(filename): 
    """ Searches for a data file to use in tests """ 

    for location in test_file_locations(): 
     filepath = os.path.join(location, filename) 
     if os.path.exists(filepath): 
      return(filepath) 
    raise IOError('Could not find test file.') 


def test_read_favorite_color(): 
    """ Test that favorite color is read properly """ 

    filename = 'favorite_color.csv' 
    test_file = find_file(filename) 

    data = read_favorite_color(test_file) 
    assert(data['first_name'][1] == 'King') 
    assert(data['last_name'][1] == 'Arthur') 
    assert(data['correct_answers'][1] == 3) 
    assert(data['cross_bridge'][1] == True) 
    assert(data['favorite_color'][1] == 'green') 
相关问题