Pytest单元测试框架-测试用例运行规则
1.Pytest测试用例运行规则
在pytest单元测试框架下面执行用例,需要满足以下几个特点:
1. 文件名以test_*.py开头或者*_test.py
2. 测试类、测试函数以test开头
3. 所有的包必须要有 __init__.py文件
一般在cmd命令行下面执行pytest用例有3种方法。大家可以选择使用,我推荐第一种:
pytest 文件名
py.test 文件名
python -m pytest 文件名
如果运行某个测试类下面的具体函数,可以使用:pytest 文件名::测试函数名
如果在测试过程中,遇到测试停止的方法可以加 -x参数: pytests -x 文件名
E:\untitled1>pytest -x collect.py
============================= test session starts =============================
platform win32 -- Python 3.5., pytest-5.0., py-1.8., pluggy-0.12.
rootdir: E:\untitled1
collected items collect.py .F [%] ================================== FAILURES ===================================
_____________________________ TestClass.test_two ______________________________ self = <collect.TestClass object at 0x00000000036A4A58> def test_two(self):
x = 'hello'
> assert hasattr(x, 'check')
E AssertionError: assert False
E + where False = hasattr('hello', 'check') collect.py:: AssertionError
===================== failed, passed in 0.08 seconds ======================
从结果可以看出第二个测试用例没有运行成功并且停止了。当错误数达到某个量级时,测试停止,参数为:maxfail=num 示例如下:
E:\untitled1>pytest --maxfail= collect.py
============================= test session starts =============================
platform win32 -- Python 3.5., pytest-5.0., py-1.8., pluggy-0.12.
rootdir: E:\untitled1
collected items collect.py .F ================================== FAILURES ===================================
_____________________________ TestClass.test_two ______________________________ self = <collect.TestClass object at 0x000000000368FCF8> def test_two(self):
x = 'hello'
> assert hasattr(x, 'check')
E AssertionError: assert False
E + where False = hasattr('hello', 'check') collect.py:: AssertionError
===================== failed, passed in 0.08 seconds ======================
2.在Pycharm中编写测试代码
在编写测试代码运行之前需要把切换pytest运行环境。file-->setting-->tools-->python intergrated tools--->default test runner 切换为 py.test 然后编写如下测试代码:
import pytest class TestClass: def test_one(self):
x = 'hello'
assert 'h' in x def test_two(self):
x = 'hello'
assert hasattr(x, 'check') def test_secound(self):
assert 'x' in 'ddd' if __name__ == '__main__':
pytest.main('-q test_class.py')
运行结果如下: (其中: .表示测试结果是通过的 pass E:表示errror 脚本中可能存在问题 F表示failed 测试结果不通过)
C:\Python35\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 5.0.3\helpers\pycharm\pytestrunner.py" -p pytest_teamcity E:/untitled1/test_class.py "-k TestClass and test_secound"
Testing started at : ...
============================= test session starts =============================
platform win32 -- Python 3.5., pytest-5.0., py-1.8., pluggy-0.12.
rootdir: C:\Program Files (x86)\JetBrains\PyCharm 5.0.\jre\jre\bin
collected items / deselected / selected . F
self = <test_class.TestClass object at 0x000000000365EB38> def test_secound(self):
> assert 'x' in 'ddd'
E AssertionError: assert 'x' in 'ddd' E:\untitled1\test_class.py:: AssertionError ================================== FAILURES ===================================
___________________________ TestClass.test_secound ____________________________ self = <test_class.TestClass object at 0x000000000365EB38> def test_secound(self):
> assert 'x' in 'ddd'
E AssertionError: assert 'x' in 'ddd' E:\untitled1\test_class.py:: AssertionError
=================== failed, deselected in 0.06 seconds ====================
Pytest单元测试框架-测试用例运行规则的更多相关文章
- Pytest单元测试框架-Pytest环境安装
unittest是python自带的单元测试框架,它封装好了一些校验返回的结果方法和一些用例执行前的初始化操作,使得单元测试易于开展,因为它的易用性,很多同学也拿它来做功能测试和接口测试,只需简单开发 ...
- Pytest单元测试框架:插件-allure-pytest环境搭建并在本地生成一个测试报告
之前写了allure-pytest的官方文档啃的内容,有些交流的朋友,实践起来没什么头绪,所以就有了这篇文章,也给自己填个坑 第一步:搭建Allure.JDK环境 1. 搭建JDK环境 不装jdk你会 ...
- Pytest单元测试框架之简单操作示例
前言: Pytest是第三方单元格测试框架,更加简单,灵活,而且提供了更多丰富的扩展: Pytest与UnitTest框架的区别 UnitTest测试用例执行顺序是依照ascii码执行,而Pytest ...
- Pytest单元测试框架-学习
pytest: Python的一个单元测试框架,基于UnitTest二次开发,语法上更加简洁,可以用来做Python开发项目的单元测试,UI自动化.接口自动化测试等,有很多的插件访问Pytest插件汇 ...
- pytest单元测试框架
一.安装方式 1.安装命令:pip install pytest 2.html安装插件:pip install pytest -html 二.pytest执行指定测试用例 1.思想:通过对测试用例进行 ...
- Pytest 单元测试框架
1.pytest 是 python 的第三方单元测试框架,比自带 unittest 更简洁和高效 2.安装 pytest pip install pytest 3.验证 pytest 是否安装成功 p ...
- Pytest单元测试框架——Pytest+Allure+Jenkins的应用
一.简介 pytest+allure+jenkins进行接口测试.生成测试报告.结合jenkins进行集成. pytest是python的一种单元测试框架,与python自带的unittest测试框架 ...
- Pytest单元测试框架之FixTure基本使用
前言: 在单元测试框架中,主要分为:测试固件,测试用例,测试套件,测试执行及测试报告: 测试固件不难理解,也就是我们在执行测试用例前需要做的动作和测试执行后的需要做的事情: 比如在UI自动化测试中,我 ...
- Pytest单元测试框架-allure测试报告
Allure Test Report 对于不同的编程语言,有很多很酷的测试框架.不幸的是,它们中只有少数能够提供测试执行输出的良好表示.Qameta软件测试团队正在致力于Allure--一个开源框架, ...
随机推荐
- Python +appium logger
封装日志模块 import logging def get_log(): # 配置日志参数 logger = logging.getLogger() logger.setLevel(logging.I ...
- typeScript学习随笔(一)
TypeScript学习随笔(一) 这么久了还不没好好学习哈这么火的ts,边学边练边记吧! 啥子是TypeScript TypeScript 是 JavaScript 的一个超集,支持 es6 标准 ...
- Linux TTY函数跟踪
1. 介绍 本文介绍了TTY打开.TTY读和TTY写操作的函数跟踪过程 2. 示例 下面是一个简单的Linux TTY打开和读写过程 #include <termios.h> #inclu ...
- 关于input标签checkbox属性 和checked
我们设置了type的属性为checkbox时,记住以下3个关键点 1.点勾选时或者说点击时,checked为选中,在input标签中是checked=“checked”,注意这里面无论checked= ...
- 10. vue-router命名路由
命名路由的配置规则 为了更加方便的表示路由的路径,可以给路由规则起一个别名, 即为"命名路由". const router = new VueRouter ({ routes: [ ...
- Java内存区域与内存溢出异常(jdk 6,7,8)
运行时数据区域 Java虚拟机在执行Java程序的过程中会把它关联的内存划分为若干个不同的数据区域.这些区域都有各自的用途,以及创建和销毁的时间,有的区域随着虚拟机进程的启动而存在,有些区域则依赖用户 ...
- react编写规范之组件组件的内容编写顺序
static 开头的类属性,如 defaultProps.propTypes. 构造函数,constructor. getter/setter(还不了解的同学可以暂时忽略). 组件生命周期. _ 开头 ...
- 51nod1423 最大二“货”
[传送门] 单调栈其实就是个后缀$max/min$,这道题可以维护一个单调递减的单调栈,pop元素的时候,能pop掉的元素就是第二大,当前元素为第一大.遇到第一个不能pop掉的时候当前元素就是第二大, ...
- go 学习 (二):基本语法
一.数据类型 布尔型:布尔型的值只可以是常量 true 或者 false.eg:var bo bool = true.布尔型无法参与数值运算,也无法与其他类型进行转换 数字类型:整型 int .浮点型 ...
- Windows10 Faster R-CNN(GPU版) 配置训练自己的模型
参考链接 1. 找到合适自己的版本,下载安装Anaconda 点击跳转下载安装 Anaconda,双击下载好的 .exe 文件安装,只勾选第一个把 conda 添加到 PATH 路径.