python - 如何将函数作为 pytest 参数化装置传递?

标签 python pytest fixtures parametrized-testing

如何在 Python 中将函数作为参数传递?我想做一些类似于下面的代码。

我的目标是传递不同的参数来测试单个函数。

def __internal_function(inputs):
    a,b,c,d = inputs

    new_value_of_b = copy.deepcopy(b)

    name = b['name']
    age = b['age']
    weight = b['weight']
    height = b['height']

    new_value_of_b2 = [name, age, weight, height]
    new_value_of_b3= new_value_of_b['weight']['unit'] = 'cm'
    del new_value_of_b['age']
    return new_value_of_b,new_value_of_b2,new_value_of_b3


@pytest.mark.parametrize('values',__internal_function,'expected_code',[100,200,300])
assert func(__internal_function)== expected_code

最佳答案

如果您使用两个变量作为参数,则正确的语法是:
@pytest.mark.parametrize(('values','expected_code'),(_internal_function,[100,200,300]))
如果其输出是可迭代的,则可以将函数用作参数。

关于python - 如何将函数作为 pytest 参数化装置传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54942019/

相关文章:

python - 使用 N 个参数运行 subprocess.popen

python - 在pytest中,如何确保在类级之前调用模块级 fixture

python - pytest 属性错误 : 'Function' object has no attribute 'get_marker'

ruby-on-rails - rails 4 : Why do fixtures make this test fail?

python - Pandas - 基于值的重复行

python - 来自不规则元组的 MultiIndex

python - Pytest 通过测试我认为它应该在 sys.exit() 调用上失败

ruby-on-rails - Rails、fixtures 和 default_scope

python - 为什么 Pytest 对 fixture 参数执行嵌套循环

java - 用于开发 GUI 的更好语言?