打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
pytest文档3-pycharm运行pytest

前言

上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多
写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻烦,所以很有必要学习如何在pycharm里面运行pytest用例

pycharm运行三种方式

1.以xx.py脚本方式直接执行,当写的代码里面没用到unittest和pytest框架时,并且脚本名称不是以test_开头命名的,此时pycharm会以xx.py脚本方式运行

2.当脚本命名为test_xx.py时,用到unittest框架,此时运行代码,pycharm会自动识别到以unittest方式运行

3.以pytest方式运行,需要改该工程设置默认的运行器:file->Setting->Tools->Python Integrated Tools->项目名称->Default test runner->选择py.test

备注:pytest是可以兼容unittest框架代码的

pycharm写pytest代码

1.在pycharm里面写pytest用例,先导入pytest

# D:/YOYO/test_class.py ** 作者:上海-悠悠 QQ交流群:588402570** import pytest class TestClass:        def test_one(self):            x = "this"            assert 'h' in x        def test_two(self):            x = "hello"            assert hasattr(x, 'check')        def test_three(self):            a = "hello"            b = "hello world"            assert a in b if __name__ == "__main__":    pytest.main('-q test_class.py')

运行结果

.F.                                                                      [100%] ================================== FAILURES =================================== _____________________________ TestClass.test_two ______________________________ self = <YOYO.test_class.TestClass object at 0x00000000039F9080>    def test_two(self):        x = "hello" >       assert hasattr(x, 'check') E       AssertionError: assert False E        +  where False = hasattr('hello', 'check') test_class.py:11: AssertionError ============================== warnings summary =============================== <undetermined location>  passing a string to pytest.main() is deprecated, pass a list of arguments instead. -- Docs: http://doc.pytest.org/en/latest/warnings.html 1 failed, 2 passed, 1 warnings in 0.06 seconds

2.运行结果“.F. ”  点是代表测试通过,F是Fail的意思,1 warnings是用于pytest.main('-q test_class.py’)里面参数需要传list,多个参数放list就不会有警告了

pytest.main(['-q’, 'test_class.py’])

pycharm设置pytest

1.新建一个工程后,左上角file->Setting->Tools->Python Integrated Tools->项目名称->Default test runner->选择py.test

2.改完之后,再重新建个脚本(注意是先改项目运行方式,再写代码才能出来),接下来右键运行就能出来pytest运行了

3.pytest是可以兼容unittest脚本的,之前写的unittest用例也能用pytest框架去运行

作者:上海-悠悠 QQ交流群:588402570

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Pycharm上python和unittest两种姿势傻傻分不清楚
Python自动化测试框架pytest的详解安装与运行
pytest封神之路第零步 快速入门
pytest文档1-环境准备与入门
Python 中如何实现参数化测试?
PyQt5+Pycharm安装和配置
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服