1、安装pythest
pip install -U pytest
2、pycharm运行pytest
pycharm运行三种模式
a、以xx.py 脚本方式直接执行,当写的代码里面没用到unittest和pytest框架时,并且脚本名称不是以test_开头命名的,
此时pycharm会以xx.py脚本方式运行。
菜单名称为“Run ‘xx’”
b.当脚本命名为test_xx.py 时,用到unitteest框架,此时运行代码,pycharm会自动识别到以unitesst方式运行
菜单名称为“Run ‘Unittests in test_xx’”
c.以pytest方式运行,需要修改该工程设置默认的运行器,如下图
菜单名称为:"Run ‘py.test in Testcase’"
3、用例设计原则
以test_开头的函数
以Test开头的类
以test_开头的方法
4、代码实例及运行结果
Testcase1.py
import pytest
class Testcase001:
def test_001(self):
x="hello"
assert "h" in x
def test_002(self):
x="this"
assert hasattr(x,"check")
def test_003(self):
a=10
assert a<20
if __name__ == "__main__":
pytest.main(["-q","Testcase1.py"])
运行结果:
ps.在运行时发现,如果鼠标定位在test_002位置,仅执行test_001和test_002测试用例。