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='tao@sina.com',password='')
    pwd = selenium.find_element_by_class_name('loginError.tip13').text
    assert pwd == '请输入密码'

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

  1. Python + unittest知识点回顾

    postman 安装Newman 先安装node.js,把npm添加到环境变量中. npm install newman --registry=https://registry.npm.taobao. ...

  2. Python + Requests 知识点回顾

    http://www.downza.cn/soft/11145.html PS下载地址 http://www.bejson.com/jsonviewernew/ JSON的在线视图 import re ...

  3. 零基础Python知识点回顾(一)

    如果你是小白,建议只要安装官网的python-3.7.0-amd64.exe 然后在电脑cmd命令提示符  输入检查是否已经安装pip,一般安装了python都会有的. >pip         ...

  4. django知识点回顾与补充

    一.django知识点回顾 1.Cookie操作 - 客户端本地存储的键值对 2.Session操作 - 3.URL路由 - /index -> view.函数 4.MTV 5.View中返回方 ...

  5. Python+Pytest+Allure+Git+Jenkins接口自动化框架

    Python+Pytest+Allure+Git+Jenkins接口自动化框架 一.接口基础 接口测试是对系统和组件之间的接口进行测试,主要是效验数据的交换,传递和控制管理过程,以及相互逻辑依赖关系. ...

  6. 最全Python基础知识点梳理

    本文主要介绍一些平时经常会用到的python基础知识点,用于加深印象,也算是对于学习这门语言的一个总结与回顾.python的详细语法介绍可以查看官方编程手册,也有一些在线网站可以学习 python语言 ...

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

    经过一天的奔波,喜忧参半,不细表 再回看下标题,C#知识点回顾 再看下内容,数据库3NF 原谅我这个标题党 今天继续回忆 1.HTTP中Post和Get区别 这忒简单了吧,大家是不是感觉到兴奋了,长舒 ...

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

    本次python爬虫百步百科,里面详细分析了爬虫的步骤,对每一步代码都有详细的注释说明,可通过本案例掌握python爬虫的特点: 1.爬虫调度入口(crawler_main.py) # coding: ...

  9. Spring知识点回顾(08)spring aware

    Spring知识点回顾(08)spring aware BeanNameAware 获得容器中的bean名称 BeanFactoryAware 获得当前的bean factory Applicatio ...

随机推荐

  1. 孟老板 BaseAdapter封装 (二) Healer,footer

    BaseAdapter封装(一) 简单封装 BaseAdapter封装(二) Header,footer BaseAdapter封装(三) 空数据占位图 BaseAdapter封装(四) PageHe ...

  2. Spring IOC(控制反转)思想笔记

    Spring IOC(控制反转)思想笔记 IOC控制反转基本理念就是将程序控制权从程序员手中交给用户自定义,从而避免了因为用户一个小需求的变化使得程序员需要改动大量代码. 案例 如果按照之前javaw ...

  3. 基于Typescript的Vue项目配置国际化

    基于Typescript的Vue项目配置国际化 简介 使用vue-i18n插件对基于Typescript的vue项目配置国际化,切换多种语言, 配合element-ui或者其他UI库 本文以配置中英文 ...

  4. Spring Boot WebFlux-导读

    背景 大家都知道,Spring Framework 是 Java/Spring 应用程序跨平台开发框架,也是 Java EE(Java Enterprise Edition) 轻量级框架,其 Spri ...

  5. 一文带你.Net混合锁和lock语句

    本文主要讲解.Net基于Monitor.Enter和lock实现互斥锁 Monitor.Enter实现 相比前面的锁来说,混合锁的性能更高,任何引用类型的对象都可以做为锁对象,不需要事先创建指定类型的 ...

  6. .NET Core/.NET5/.NET6 开源项目汇总5:权限管理系统项目

    系列目录     [已更新最新开发文章,点击查看详细] 企业管理系统一般包含后台管理UI.组织机构管理.权限管理.日志.数据访问.表单.工作流等常用必备功能.下面收集的几款优秀开源的管理系统,值得大家 ...

  7. Luat Demo | 一文读懂,如何使用Cat.1开发板实现Camera功能

    让万物互联更简单,合宙通信高效便捷的二次开发方式Luat,为广大客户提供了丰富实用的Luat Demo示例,便于项目开发灵活应用. 本期采用合宙全新推出的VSCode插件LuatIDE,为大家演示如何 ...

  8. 用transform和rem哪个好

    个人觉得电脑端的用transform好,毕竟电脑端的项目基本都会固定屏幕比列,16:9.28:9.32:9的 一个固定的设计稿就能很好的适配. 移动端用rem比较好,移动端的屏幕比列太杂,使用rem自 ...

  9. MySQL 中存储时间的最佳实践

    平时开发中经常需要记录时间,比如用于记录某条记录的创建时间以及修改时间.在数据库中存储时间的方式有很多种,比如 MySQL 本身就提供了日期类型,比如 DATETIME,TIMESTAMEP 等,我们 ...

  10. Simpleperf分析之Android系统篇

    [译]Simpleperf分析之Android系统篇 译者按: Simpleperf是用于Native的CPU性能分析工具,主要用来分析代码执行耗时.本文是主文档的一部分,系统篇. 原文见aosp仓库 ...