首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Pytest:在测试文件中具有模块作用域的装置可以工作,但在conftest.py中抛出错误

Pytest:在测试文件中具有模块作用域的装置可以工作,但在conftest.py中抛出错误
EN

Stack Overflow用户
提问于 2018-07-31 18:03:39
回答 1查看 1.3K关注 0票数 1

在structure的项目中测试@pytest.fixture(scope="module")

代码语言:javascript
复制
├── main.py
├── pytest.ini
├── src
│   └── tasks
│       ├── __init__.py
│       └── conftest.py
└── tests
    └── func
        └── test_authors.py

pytest.ini包含

代码语言:javascript
复制
[pytest]
xfail_strict=true

当夹具包含在测试文件- tests/func/test_authors.py中时,测试可以正常工作

代码语言:javascript
复制
import json, pytest


@pytest.fixture(scope='module')
def author_file_json(tmpdir_factory):
    python_author_data = {
        'Ned': {'City': 'Boston'},
        'Brian': {'City': 'Portland'},
        'Luciano': {'City': 'Sau Paulo'}
    }

    file = tmpdir_factory.mktemp('data').join('author_file.json')
    print('file:{}'.format(str(file)))

    with file.open('w') as f:
        json.dump(python_author_data, f)
    return file


def test_brian_in_portland(author_file_json):
    with author_file_json.open() as f:
        authors = json.load(f)
    assert authors['Brian']['City'] == 'Portland'

如果我将fixture author_file_json添加到conftest.py

  • 并运行pytest --fixtures
    • ,它将显示在trace

代码语言:javascript
复制
- but now if I run `pytest tests/test_authors.py` 
    - I get an error - `E fixture 'author_file_json' not found`

我该如何解决这个问题呢?

EN

回答 1

Stack Overflow用户

发布于 2018-08-01 07:50:47

conftest.py文件放在与您的测试文件目录(func)相同级别的位置,或者项目工作区中您的测试用例文件的父目录之一(在您的情况下是testsroot dir of your project )。您当前将其放在pytest不会扫描的同级目录tasks中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51610771

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档