小编d_j*_*d_j的帖子

在pytest中使用参数化关键字参数

我正在学习pytest,我正在尝试使用pytest.mark.parametrize作为关键字参数.

这是没有pytest.mark.parametrize的简单示例:

G = 10
H = 2

def f(g=G, h=H):
    return 5 * g + h

def test_f1():
    assert f(h=4) == 54
    assert f(g=20) == 102
Run Code Online (Sandbox Code Playgroud)

这是我使用pytest.mark.parametrize的不成功路径之一.它不起作用,但它有助于理解我想要实现的目标:

import pytest

G = 10
H = 2

def f(g=G, h=H):
    return 5 * g + h  

@pytest.mark.parametrize("arg, expected", [
    ("h=4", 54),
    ("g=20", 102),
    ])

def test_f2(arg, expected):
    assert f(eval(arg)) == expected
Run Code Online (Sandbox Code Playgroud)

python pytest

5
推荐指数
2
解决办法
2761
查看次数

如何自动更改所有文档测试的 pytest 临时目录

我想在每个 doctest 的开头更改为 pytest 临时目录,我想知道是否有一种方法可以自动执行此操作,而无需启动每个 doctest:

>>> tmp = getfixture('tmpdir')                                                                                            
>>> old = tmp.chdir()                                                                                                   
Run Code Online (Sandbox Code Playgroud)

python testing doctest pytest

4
推荐指数
1
解决办法
1274
查看次数

标签 统计

pytest ×2

python ×2

doctest ×1

testing ×1