学习内容来自 对
https://www.cnblogs.com/yoyoketang 自动化大师悠悠的总结1.pytest运行规则:**查找当前目录及其子目录下以test_*.py或*_test.py文件,找到文件后,在文件中找到以test开头函数并执行。** 2.命令行下执行pytest或py.test或python -m pytest 3.通过pytest -q test_a.py 可以仅执行 test_a.py中以test开头的函数 4.测试类必须以Test开头,并且不能带有 init 方法 5.测试函数以test开头 6.所有的包pakege必须要有__init__.py文件 7.执行某一个py文件下用例 pytest 脚本名称.py 8.-k 按关键字匹配 pytest -k "Class and not two"将不会执行test_sample下的测试脚本和test_two这个方法 9.pytest test_mod.py::TestClass::test_method运行test_mod.py下TestClass下的test_method方法 10.pytest -x 遇到错误时停止测试 11.pytest --maxfail=2错误数量最大等于2时停止测试 12.以pytest方式运行,需要改该工程设置默认的运行器:file->Setting->Tools->Python Integrated Tools->项目名称->Default test runner->选择py.test 注意是先改项目运行方式,再写代码才能出来 13. 运行结果“.F. ” 点是代表测试通过,F是Fail的意思,1 warnings是用于pytest.main('-q test_class.py')里面参数需要传list,多个参数放list就不会有警告了
目录结构:
测试脚本如下
test_class.py
class TestClass:
def test_one(self):
print 'one'
x = "this"
assert 'h' in x
def test_two(self):
print 'two'
x = "hello"
assert hasattr(x, 'check')
if __name__ == '__main__':
pytest.main(['-q','test_class.py'])
test_sample.py
#!usr/bin/python
# -*- coding: utf-8 -*-
def func(x):
return x + 1
def test_answer():
assert func(3) == 5