前言

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

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框架去运行

pytest_03_pycharm运行pytest (转:上海悠悠)的更多相关文章

  1. pytest文档3-pycharm运行pytest

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

  2. 《2019测试面试题-上海悠悠.pdf》

    前言 面试测试岗位一般会有笔试题,笔试题考SQL和编程基础比较多,小编最近收集了多家公司的测试面试题,整理成一个文档供大家参考. 2018年下半年一直不太平,听到最多的就是"p2p暴雷&qu ...

  3. 不能在Python Console中运行pytest

    在Python Console中运行pytest发现报错了 这是为什么?因为Python Console已经是进入python之后的环境,就像在python自带的IDLE中运行pytest pytes ...

  4. 运行pytest,报错"AttributeError: 'module' object has no attribute 'xxx'"

    最近学习pytest被此问题困扰,敲脑壳,实在是不该.百度解决方法一大堆,我的问题怎么也解决不了,来看一下,我是怎么解决的,各位大佬勿喷,只是自己做笔记用,谢谢. 报错信息如下: 网上解决方法是这样的 ...

  5. pytest——pycharm中右击运行(run)没有问题,在terminal中运行pytest报错:E ModuleNotFoundError: No module named

    参考了这个解决办法:https://blog.csdn.net/qq_36829091/article/details/82180866 我的是Windows,linux的和Windows的解决办法有 ...

  6. pycharm运行pytest

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

  7. pycharm运行Pytest,有没有将Pytest写入Python代码中的区别

    初学pytest. 将pytest写进Python代码中 不同运行方式都可正常运行     =======================**********************========= ...

  8. Pytest学习笔记(三) 在代码中运行pytest

    前面介绍的是在cmd中执行pytest,平常我们一般都是通过编译器(如pycharm)来编写用例的,写完用例后,需要调试看看是否能运行,如果每次都切换到cmd中执行,太麻烦. 因此,这一节来说下怎么在 ...

  9. 用pycharm运行pytest

    安装pytest 1. 在pycharm中建项目,建文件,文件名字要以test_开头 2.在文件中插入pytest模块 import pytest #引用pytest模块 3.定义test函数,以及断 ...

随机推荐

  1. linux命令之------部分细节点

    创建文件夹/文件命令以及清除模式 mkdir +文件夹名字 touch +文件名字 rm -fr 删除文件,文件夹 -f强制删除 -r是递归 查询linux下所有启动的线程: ps -ef|grep ...

  2. gethostname、gethostbyname

    gethostname():返回本地主机的标准主机名 原型: #include<unistd.h> int gethostname(char *name, size_t len); 参数说 ...

  3. #Ubuntu 14.04 系统下载

    http://mirrors.aliyun.com/ubuntu-releases/14.04/

  4. mac jq for json format

    mac jq #1.安装 brew install jq #2.创建文件 echo '{"name": "Ruby"}' > ./test.json #3 ...

  5. C#中得到每周,每月,每季,每年的年初末日期

    DateTime表示时间上的一刻,通常以日期和当天的时间表示.借用这个结构,我们可以实现较丰富的功能,本文给出得到每周每天的方法,及得到本月第一天,本月最后一天,本季第一天,本季最后一天,本年第一天及 ...

  6. CRM 负责人 选择

    CRM 负责人 参与 - 搜狗搜索https://www.sogou.com/sgo?query=CRM+%E8%B4%9F%E8%B4%A3%E4%BA%BA+%E5%8F%82%E4%B8%8E& ...

  7. hue创建的hdfs数据在hdfs无法删除的问题。

    在linux时删除时出现: rmr: Permission denied: user=root, access=ALL, inode="/user/root/.Trash/191128080 ...

  8. MQTT研究之EMQ:【EMQX使用中的一些问题记录(2)】

    我的测试环境: Linux: CentOS7 EMQX:V3.2.3 题外话: 这里主要介绍Websocket的支持问题. 对ws的支持比较正常,但是对wss的支持,调了较长的时间,没有成功. Jav ...

  9. Scrapy中的Request和Response

    Request Request 部分源码: # 部分代码 class Request(object_ref): def __init__(self, url, callback=None, metho ...

  10. Oracle系列十四 序列、索引和同义词

    序列 : 提供有规律的数值.索引  : 提高查询的效率同义词  :给对象起别名 序列: 可供多个用户用来产生唯一数值的数据库对象 自动提供唯一的数值 共享对象 主要用于提供主键值 将序列值装入内存可以 ...