
霍格沃兹测试开发学社 ceshiren.com


# content of test_sample.py
def inc(x):
return x + 1
def test_answer():
assert inc(3) == 5



pip install pytest# content of test_sample.py
def inc(x):
return x + 1
def test_answer():
assert inc(3) == 5

| 类型 | 规则 |
|---|---|
| 文件 | test_开头 或者 _test 结尾 |
| 类 | Test 开头 |
| 方法/函数 | test_开头 |
注意:测试类中不可以添加__init__构造函数 |




pytest/py.test [包名]pytest 文件名.pypytest 文件名.py::类名pytest 文件名.py::类名::方法名 :
| 类型 | 规则 |
|---|---|
| setup_module/teardown_module | 模块级,整个模块的所有测试用例执行前后运行一次 |
| setup_class/teardown_class | 类级,只在类中前后运行一次 |
| setup_function/teardown_function | 函数级,在类外每个测试用例执行前后被调用 |
| setup_method/teardown_methond | 方法级,类中的每个测试用例执行前后 |
| setup/teardown | 方法级,类中的每个测试用例执行前后(重点) |

—help
-x 用例一旦失败(fail/error),就立刻停止执行
--maxfail=num 用例达到
-m 标记用例
-k 执行包含某个关键字的测试用例
-v 打印详细日志
-s 打印输出日志(一般-vs一块儿使用)
—collect-only(测试平台,pytest 自动导入功能 )
--lf(--last-failed) 只重新运行故障。--ff(--failed-first) 先运行故障然后再运行其余的测试
pytest -s test_mark_zi_09.py -m=webtest pytest -s test_mark_zi_09.py -m apptest pytest -s test_mark_zi_09.py -m "not ios" @pytest.mark.skip@pytest.mark.skipif pytest.skip(reason)@pytest.mark.xfail参数化设计方法就是将模型中的定量信息变量化,使之成为任意调整的参数。
对于变量化参数赋予不同数值,就可得到不同大小和形状的零件模型。
@pytest.mark.parametrize进行参数化和数 据驱动更灵活search_list = ['appium','selenium','pytest']
@pytest.mark.parametrize('name',search_list)
def test_search(name):
assert name in search_list
@pytest.mark.parametrize("test_input,expected",[
("3+5",8),("2+5",7),("7+5",12)
])
def test_mark_more(test_input,expected):
assert eval(test_input) == expected
@pytest.mark.parametrize("test_input,expected",[
("3+5",8),("2+5",7),("7+5",12)
],ids=['add_3+5=8','add_2+5=7','add_3+5=12'])
def test_mark_more(test_input,expected):
assert eval(test_input) == expected
比如
有几种组合形势 ?