前言

上一篇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

  1. # D:/YOYO/test_class.py
  2. ** 作者:上海-悠悠 QQ交流群:588402570**
  3. import pytest
  4. class TestClass:
  5. def test_one(self):
  6. x = "this"
  7. assert 'h' in x
  8. def test_two(self):
  9. x = "hello"
  10. assert hasattr(x, 'check')
  11. def test_three(self):
  12. a = "hello"
  13. b = "hello world"
  14. assert a in b
  15. if __name__ == "__main__":
  16. pytest.main('-q test_class.py')

运行结果

  1. .F. [100%]
  2. ================================== FAILURES ===================================
  3. _____________________________ TestClass.test_two ______________________________
  4. self = <YOYO.test_class.TestClass object at 0x00000000039F9080>
  5. def test_two(self):
  6. x = "hello"
  7. > assert hasattr(x, 'check')
  8. E AssertionError: assert False
  9. E + where False = hasattr('hello', 'check')
  10. test_class.py:11: AssertionError
  11. ============================== warnings summary ===============================
  12. <undetermined location>
  13. passing a string to pytest.main() is deprecated, pass a list of arguments instead.
  14. -- Docs: http://doc.pytest.org/en/latest/warnings.html
  15. 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框架去运行

---------------------------------pytest结合selenium自动化完整版-------------------------

全书购买地址 https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b

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

也可以关注下我的个人公众号:yoyoketang

pytest文档3-pycharm运行pytest的更多相关文章

  1. pytest文档2-用例运行规则

    用例设计原则 文件名以test_*.py文件和*_test.py 以test_开头的函数 以Test开头的类 以test_开头的方法 所有的包pakege必须要有__init__.py文件 help帮 ...

  2. pytest(2):使用pycharm运行pytest

    pycharm运行 1.在pycharm里创建测试文件test_demo.py # Author xuejie zeng # encoding utf-8 # content of test_demo ...

  3. pytest文档18-配置文件pytest.ini

    前言 pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行. ini配置文件 pytest里面有些文件是非test文件 py ...

  4. pytest文档7-pytest-html生成html报告

    前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...

  5. pytest文档19-doctest测试框架

    前言 doctest从字面意思上看,那就是文档测试.doctest是python里面自带的一个模块,它实际上是单元测试的一种. 官方解释:doctest 模块会搜索那些看起来像交互式会话的 Pytho ...

  6. pytest文档1-环境准备与入门

    前言 首先说下为什么要学pytest,在此之前相信大家已经掌握了python里面的unittest单元测试框架,那再学一个框架肯定是需要学习时间成本的. 刚开始我的内心是拒绝的,我想我用unittes ...

  7. pytest文档55-plugins插件开发

    前言 前面一篇已经学会了使用hook函数改变pytest运行的结果,代码写在conftest.py文件,实际上就是本地的插件了. 当有一天你公司的小伙伴觉得你写的还不错,或者更多的小伙伴想要你这个功能 ...

  8. pytest文档51-内置fixture之cache使用

    前言 pytest 运行完用例之后会生成一个 .pytest_cache 的缓存文件夹,用于记录用例的ids和上一次失败的用例. 方便我们在运行用例的时候加上--lf 和 --ff 参数,快速运行上一 ...

  9. pytest文档46-关于https请求警告问题(InsecureRequestWarning: Unverified HTTPS request is being made)

    前言 使用 pytest 执行 https 请求用例的时候,控制台会出现警告:InsecureRequestWarning: Unverified HTTPS request is being mad ...

随机推荐

  1. centos6下mysql的主从复制的配置

    2015年9月17日 23:00:36 update 想要好好了解mysql复制,还是去看看<高性能MySQL>(第三版)好了,上面说的比较详细. =========== 在本地用virt ...

  2. MVC – 4.mvc初体验(1)

    1.MVC请求模式 2.MVC简单请求流程图 展开 折叠 3.返回string的mvc方法 展开 折叠 4.加载视图的方法

  3. Android Webview中解决H5的音视频不能自动播放的问题

    在开发webview的时候,当加载有声音的网页的时候,声音不会自动播放, 解决方法:在webview中调用js方法.这个方法需要在webview的setWebViewClient方法之后在onPage ...

  4. yii2 DateTimePicker显示到天

    扩展是 kartik\datetime\DateTimePicker; 关键是加入此配置  'minView'=> "month",示例如下: <?php echo D ...

  5. 解决 VUE 微信 IOS 路由跳转问题

    watch: { "$route"(){ if (/iPhone|mac|iPod|iPad/i.test(navigator.userAgent)) { location.hre ...

  6. 基于vue2.0的后管系统(配置篇)

    一些项目依赖package.json { "name": "frontend", "description": "tssp bas ...

  7. spring_150902_hibernatedaosupport

    实体类: package com.spring.model; import javax.persistence.Entity; import javax.persistence.Id; import ...

  8. java 反射 报错:Attempt to get java.lang.Integer field "..." with illegal data type conversion to int

    类: Integer id; 反射时: Field f = User.class.getDeclaredField("id"); f.setAccessible(true); in ...

  9. PHP 5.4.0之Traits

    [PHP之Traits] As of PHP 5.4.0, PHP implements a method of code reuse called Traits. 1.Traits基础 2.优先级: ...

  10. Ngnix的日志管理和用定时任务完成日志切割

    一.日志管理 先来看看ngnix的配置文件的server段 接下来我们解释一下默认格式的具体意思 #log_format main '$remote_addr(远程IP) - $remote_user ...