python - 如何将 pytest.mark.parametrize 与固定装置一起使用
问题描述
我想用夹具参数化测试。像这样的东西:
def count_lines(path):
with open(path, 'r') as fh:
return len(fh.readlines())
@pytest.fixture
def data_path():
return '/path/to/test/data'
@pytest.fixture
def simple_file(data_path):
return os.path.join(data_path, 'simpletestfile.ext')
@pytest.fixture
def other_file(data_path):
return os.path.join(data_path, 'othertestfile.ext')
@pytest.mark.parametrize('given, expected', [
(simple_file, 3),
(other_file, 9),
])
def test_count_lines(given, expected):
observed = count_lines(given)
assert observed == expected
上面的代码失败了,因为测试没有解析sample_file
andother_file
固定装置,而是接收了使测试在open()
语句中失败的函数。
为了使它工作,我必须做这样的事情:
@pytest.mark.parametrize('given, expected', [
(simple_file(data_path()), 3),
(other_file(data_path()), 9),
])
但这似乎违背了 py.test 的目的。
另一种选择是
class TestCountLines(object):
TESTDATA = [
{
'id': 'simple case',
'input_file': 'simpletestfile.ext',
'expected': 3,
},
{
'id': 'other case',
'input_file': 'othertestfile.ext',
'expected': 9,
},
]
@pytest.fixture(params=TESTDATA, ids=lambda x: x['id'])
def staged_testdata(self, request, data_path):
datafile = os.path.join(data_path, request.param['input_file'])
expected = request.param['expected']
yield datafile, expected
def test_count_lines(self, staged_testdata):
given, expected = staged_testdata
observed = count_lines(given)
assert observed == expected
然而我发现上面的代码很奇怪,因为staged_testdata
夹具产生了两件事,然后我需要在测试之前解包测试数据。那么,如何使用夹具参数化测试?
解决方案
推荐阅读
- r - R中水平条上的绘图文本
- php - 如何使用 Audience Network 系统用户令牌
- python-3.x - django.utils.datastructures.MultiValueDictKeyError REST API 发布请求
- ios - XCode 不构建我的项目
- html - HTML 中的重叠元素
- angular - 来自单例/全局类的 ionic/angular html 模板
- c - 移动数组中的整个块
- java - 有人可以为我提供一些关于 Java 中 OpenLog 的指导吗?
- javascript - 在 Angular 5 (CLI) 中,如何将数据从 index.js 传递到组件?
- android - 程序类型已经存在:com.bumptech.glide.BitmapOptions