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--一个开源框架, ...
随机推荐
- Mysql【第二课】
- Java精通并发-通过openjdk源码分析ObjectMonitor底层实现
在我们分析synchronized关键字底层信息时,其中谈到了Monitor对象,它是由C++来实现的,那,到底它长啥样呢?我们在编写同步代码时完全木有看到该对象的存在,所以这次打算真正来瞅一下它的真 ...
- BZOJ1485: [HNOI2009]有趣的数列(卡特兰数+快速幂)
题目链接 传送门 题面 思路 打表可以发现前六项分别为1,2,5,12,42,132,加上\(n=0\)时的1构成了卡特兰数的前几项. 看别人的题解说把每一个数扫一遍,奇数项当成入栈,偶数项当成出栈, ...
- github配置SSH proxy
在windows users 用户目录下生成config文件,如在C:\Users\your_user_name\.ssh 目录下,找到config文件,如果没有新建一个,写入如下内容: Host g ...
- Alpha冲刺(7/10)——2019.4.30
所属课程 软件工程1916|W(福州大学) 作业要求 Alpha冲刺(7/10)--2019.4.30 团队名称 待就业六人组 1.团队信息 团队名称:待就业六人组 团队描述:同舟共济扬帆起,乘风破浪 ...
- DT图库列表修改内容标题字数
dt限制标题字数的方法有几种,最简单的是一种是: {dsubstr($t[title], 42, '..')} 还有一种是列表输出,也是网上常用的方法 <!--{tag("module ...
- ActiveMQ-启动服务异常
如果报这种异常: Caused by: java.io.IOException: Failed to bind to server socket: tcp://0.0.0.0:61616?maximu ...
- Eclipse 打包运行maven项目
https://www.cnblogs.com/tangshengwei/p/6341462.html
- python(二)——if条件语句与基本数据类型
if语句 缩进要保持一致 if 1 == 1: print('hello') if 2 == 2: print('world') else: print('python') elif inp = in ...
- 复旦高等代数 I(16级)每周一题
每周一题的说明 一.本学期高代I的每周一题面向16级的同学,将定期更新(一般每周的周末公布下一周的题目); 二.欢迎16级的同学通过微信或书面方式提供解答图片或纸质文件给我,优秀的解答可以分享给大家: ...