在structure的项目中测试@pytest.fixture(scope="module")
时
├── main.py
├── pytest.ini
├── src
│ └── tasks
│ ├── __init__.py
│ └── conftest.py
└── tests
└── func
└── test_authors.py
pytest.ini
包含
[pytest]
xfail_strict=true
当夹具包含在测试文件- tests/func/test_authors.py
中时,测试可以正常工作
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
中
- but now if I run `pytest tests/test_authors.py`
- I get an error - `E fixture 'author_file_json' not found`
我该如何解决这个问题呢?
发布于 2018-08-01 07:50:47
将conftest.py
文件放在与您的测试文件目录(func
)相同级别的位置,或者项目工作区中您的测试用例文件的父目录之一(在您的情况下是tests
或root dir of your project
)。您当前将其放在pytest不会扫描的同级目录tasks
中。
https://stackoverflow.com/questions/51610771
复制相似问题