简体   繁体   English

你如何正确地将文件解析的单元测试与pytest集成?

[英]How do you properly integrate unit tests for file parsing with pytest?

I'm trying to test file parsing with pytest. 我正在尝试用pytest测试文件解析。 I have a directory tree that looks something like this for my project: 我有一个目录树,对我的项目看起来像这样:

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

My setup.py file looks something like this: 我的setup.py文件看起来像这样:

from setuptools import setup

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

My setup.cfg file looks something like this: 我的setup.cfg文件看起来像这样:

[aliases]
test=pytest

I've written several unit tests with pytest to verify that files are properly read. 我用pytest编写了几个单元测试来验证文件是否正确读取。 They work fine when I run pytest from within the " test " directory. 当我从“ test ”目录中运行pytest时,它们工作正常。 However, if I execute any of the following from my project directory, the tests fail because they cannot find data files in test_files: 但是,如果我从项目目录中执行以下任何操作,测试将失败,因为它们无法在test_files中找到数据文件:

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

The test seems to be sensitive to the directory from which pytest is executed. 测试似乎对执行pytest的目录很敏感。

How can I get pytest unit tests to discover the files in "data_files" for parsing when I call it from either the test directory or the project root directory? 当我从测试目录或项目根目录调用它时,如何通过pytest单元测试来发现“data_files”中的文件进行解析?

One solution is to define a rootdir fixture with the path to the test directory, and reference all data files relative to this. 一种解决方案是使用指向测试目录的路径定义rootdir fixture,并引用与此相关的所有数据文件。 This can be done by creating a test/conftest.py (if not already created) with some code like this: 这可以通过使用如下代码创建test/conftest.py (如果尚未创建)来完成:

import os
import pytest

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

Then use os.path.join in your tests to get absolute paths to test files: 然后在测试中使用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)
    # ...

One solution is to try multiple paths to find the files. 一种解决方案是尝试多个路径来查找文件。

#!/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] == 2)
    assert(data['cross_bridge'][1] == True)
    assert(data['favorite_color'][1] == 'green')

One way is to pass a dictionary of command name and custom command class to cmdclass argument of setup function. 一种方法是将命令名和自定义命令类的字典传递给setup函数的cmdclass参数。

Another way is like here , posted it here for quick reference. 另一种方式就像这里一样,贴在这里以供快速参考。

pytest-runner will install itself on every invocation of setup.py. pytest-runner将在每次调用setup.py时自行安装。 In some cases, this causes delays for invocations of setup.py that will never invoke pytest-runner. 在某些情况下,这会导致调用setup.py的延迟,该调用永远不会调用pytest-runner。 To help avoid this contingency, consider requiring pytest-runner only when pytest is invoked: 为了避免这种意外情况,请考虑在调用pytest时仅需要pytest-runner:

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

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

# ...

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

Make sure all the data you read in your test module is relative to the location of setup.py directory. 确保您在测试模块中读取的所有数据都与setup.py目录的位置相关。

In OP's case data file path would be test/test_files/data_file1.txt , I made a project with same structure and read the data_file1.txt with some text in it and it works for me. 在OP的情况下,数据文件路径将是test/test_files/data_file1.txt ,我做了相同结构的项目,并读取data_file1.txt在一些文本,它为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM