Python+pytest知识点回顾

pip install pytest

pytest 单元测试框架

pytest高于unittest测试框架

unittest测试类需要继承unittest.TestCase类

pytest不需要继承,可以是一个函数,也可以是一个类

unittest参数化需要依赖第三方的库

pytest不需要依赖,直接使用内部的库parametrize

unittest测试报告HTMLTestRunner

pytest 测试报告pytest-html 或者是 allure

unittest是没有插件的

pytest有很丰富的插件

unittest不支持失败重试

pytest支持失败重试

import pytest

class TestF1:

def test_01(self):

print(‘AAA‘)

def test02(self):

print(‘BBB‘)

==:内容和类型必须同时满足

in:后者包含前者

is:前后者两个值相等

def A_test():

assert 1==1

def B_test():

assert 1==2

def test_in():

assert ‘测试实战‘ in ‘Python测试实战‘

def test_is():

assert 2 is 2

if __name__ == ‘__main__‘:

pytest.main([‘-v‘])

执行pytest测试用例会显示进度条

pip install pytest-sugar

1. -v:输出详细的信息

2. -s: 输出测试函数或者测试方法里面的print()的内容

3. -k:按分类执行测试点

4. -m:进行分组

5. -x:执行失败立即停止(后面的测试函数/方法不执行)

6. --maxfail:执行失败的最大次数(如果只有一个失败的--maxfail=1,后面的代码不执行,--maxfail=2执行后面的代码)

7. --tb=no:关闭信息

8. --tb=short:只输出assert的错误信息(会提示具体的函数以及错误代码)

9. --tb=line:一行行展示所有错误信息的位置(不会提示具体函数,但是会提示断言失败的位置)

10. --lf:定位错误

11. --ff:遇到错误继续执行

12. --duration=0:测试函数执行速度

pytest代码执行的顺序:

pytest -v xx.py

pytest -v xx.py::test_001

pytest -v -s xx.py

pytest -v -k ‘login or logout‘ xx.py

pytest -v -m login xx.py

pytest -v -k ‘login or ui‘ xx.py

pytest -v -x xx.py

pytest -v --maxfail=2 xx.py

pytest -v  xx.py --tb=no

pytest -v  xx.py --tb=short

pytest -v  xx.py --tb=line

pytest -v  xx.py --tb=line

pytest -v --lf xx.py

pytest -v --ff xx.py

pytest -v --duration=0 xx.py

@pytest.mark.api

def test_001():

assert 1==1

class Login:

@pytest.mark.login

def test_login_01(self):

print(‘登录成功1‘)

@pytest.mark.login

def test_login_02(self):

print(‘登录成功2‘)

@pytest.mark.logout

def test_logout_01(self):

print(‘退出登录1‘)

@pytest.mark.logout

def test_logout_02(self):

print(‘退出登录2‘)

import requests

def test_baidu():

‘‘‘测试百度链接‘‘‘

r = requests.get(‘http://www.baidu.com‘)

assert r.status_code == 200

def test_jd():

‘‘‘测试京东链接‘‘‘

r = requests.get(‘http://www.jd.com‘)

assert r.status_code == 200

def test_taobao():

‘‘‘测试淘宝链接‘‘‘

r = requests.get(‘http://www.taobao.com‘)

assert r.status_code == 200

def test_sina():

‘‘‘测试新浪链接‘‘‘

r = requests.get(‘https://mail.sina.com.cn/‘)

assert r.status_code == 200

pytest 和 selenium 整合

pip install pytest-selenium

执行:

pytest -v xx.py --driver Chrome

import time

from selenium import webdriver

driver = webdriver.Chrome()

import pytest

def login(selenium,username=None,password=None):

‘‘‘测试公共分离‘‘‘

selenium.get(‘https://mail.sina.com.cn/‘)

time.sleep(2)

selenium.find_element_by_id(‘freename‘).send_keys(username)

time.sleep(2)

selenium.find_element_by_id(‘freepassword‘).send_keys(password)

selenium.find_element_by_class_name(‘loginBtn‘).click()

def test_login_pwd_null(selenium):

‘‘‘测试新浪邮箱用户密码为空‘‘‘

login(selenium=selenium,username=‘‘,password=‘‘)

user = selenium.find_element_by_class_name(‘loginError.tip11‘).text

assert user == ‘请输入邮箱名‘

def test_pwd_null(selenium):

‘‘‘测试新浪邮箱密码为空‘‘‘

login(selenium=selenium,username=‘[email protected]‘,password=‘‘)

pwd = selenium.find_element_by_class_name(‘loginError.tip13‘).text

assert pwd == ‘请输入密码‘

原文地址:https://www.cnblogs.com/Teachertao/p/12304019.html

时间: 2024-07-30 19:23:19

Python+pytest知识点回顾的相关文章

Python + unittest知识点回顾

postman 安装Newman 先安装node.js,把npm添加到环境变量中. npm install newman --registry=https://registry.npm.taobao.org newman run 拉钩网.postman_collection.json newman-reporter-html login登录成功后是不是代表业务流程是对的? 不是,只是代表login的接口是对的. 接口测试的维度: 1.请求参数的边界值 2.请求参数为空 3.请求参数数据类型 4.

面向对象【day07】:多态-面向对象使用场景--知识点回顾

本节内容 多态 面向对象使用场景 知识点回顾 一.多态 一.概述 多态性(polymorphisn)是允许你将父对象设置成为和一个或更多的他的子对象相等的技术,赋值之后,父对象就可以根据当前赋值给它的子对象的特性以不同的方式运作.简单的说,就是一句话:允许将子类类型的指针赋值给父类类型的指针. 那么,多态的作用是什么呢?我们知道,封装可以隐藏实现细节,使得代码模块化:继承可以扩展已存在的代码模块(类):它们的目的都是为了--代码重用.而多态则是为了实现另一个目的--接口重用!多态的作用,就是为了

面向对象知识点回顾整理

目录 面向对象知识点回顾整理 一.面向对象基础 1.类和对象 2.属性查找: 3.绑定方法: 4.对象之间的交互: 5.类的内置属性 6.三大特性:继承.多态.封装 二.面向对象高阶 元类 单例模式 面向对象知识点回顾整理 一.面向对象基础 面向对象编程的核心是对象二字,对象是属性与方法的结合体,python中一切皆对象. 优点:可扩展性强 缺点:编程的复杂度高 1.类和对象 对象:属性和方法的结合体 类:一堆属性和方法的结合体 python中是先有类再有对象,现实生活中是先有对象再有类 类名(

Spring知识点回顾(01)

Spring知识点回顾(01) 一.依赖注入 1.声明Bean的注解 @Component @Service @Repository @Controller 2.注入Bean的注解 @Autowired @Inject @Resource 二.加载Bean 1.xml方式 - applicationcontext.xml : Beans, Bean, Component-Scan 2.注解方式 - @Configuration,@ComponentScan,@Bean 用@Configurati

python PIL库回顾

之前用PIL的时候都不知道看官方文档,网上搜索结果靠前的一些介绍性的博客其实很扯淡,讲两个函数.贴三张图,文章就完了. 今天把他的文档看了看,发现一点也不简单,网上介绍的不过是冰山一角. 对于我这样一个不太懂多媒体的人,如果用到图像处理,主要也就这些操作: 读写.取通道.两幅图合并.像素点操作.各种滤镜.色彩模式转换.图像旋转.缩放.裁剪.仿射. PIL对付这些小喽啰,都是完全没有问题的. 读写 open是Image类的一个方法,返回值是一个Image对象,值得注意的是,open不能打开网络图片

面试前的准备---C#知识点回顾----03

经过一天的奔波,喜忧参半,不细表 再回看下标题,C#知识点回顾 再看下内容,数据库3NF 原谅我这个标题党 今天继续回忆 1.HTTP中Post和Get区别 这忒简单了吧,大家是不是感觉到兴奋了,长舒一口气了,终于出现了一个可以聊上10分钟的问题了. 根据HTTP规范,Get用于信息获取,而且应该是安全的和幂等的. 参数在URL后,以?分割,以&相连. 根据HTTP规范,Post表示可能修改服务器的资源请求.数据存在HTTP包中 以上最基本的得知道吧,重点可以聊的出现了,安全性. 详细请拜读:h

python 异常知识点

raise from python 在3.0 之后引入了raise from 表达式: raise exception from otherexception 当使用该语法时,第二个表达式指定了另一个异常类或实例,它会附加到引发异常的__cause__属性 注意: python3.0不再支持raise Exc,Args形式,而该形式在Python2.6中仍然可用,在Python3.0中,使用 raise Exc(Args)调用. with  as with语句格式: with expressio

python爬虫主要就是五个模块:爬虫启动入口模块,URL管理器存放已经爬虫的URL和待爬虫URL列表,html下载器,html解析器,html输出器 同时可以掌握到urllib2的使用、bs4(BeautifulSoup)页面解析器、re正则表达式、urlparse、python基础知识回顾(set集合操作)等相关内容。

本次python爬虫百步百科,里面详细分析了爬虫的步骤,对每一步代码都有详细的注释说明,可通过本案例掌握python爬虫的特点: 1.爬虫调度入口(crawler_main.py) # coding:utf-8from com.wenhy.crawler_baidu_baike import url_manager, html_downloader, html_parser, html_outputer print "爬虫百度百科调度入口" # 创建爬虫类class SpiderMai

python PIL库回顾1

1 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* BLOCKS =============================================================================*/ p, blockquote, ul, ol, dl, table, pre { margin: 15px 0; } /* HE