当前位置: 首页 > news >正文

【pytest】单元测试文件的写法

前言

可怜的宾馆,可怜得像被12月的冷雨淋湿的一条三只腿的黑狗。——《舞舞舞》

在这里插入图片描述


\;\\\;\\\;

目录

    • 前言
    • test_1或s_test格式
    • 非测试文件
    • @pytest.fixture()装饰器
    • pytest+selenium

test_1或s_test格式

要么 test_前缀 在前,要么 _test后缀 在后!

#test_1.py
def test_1():name='aa'assert 'bb'==namedef test_2():name='a'assert 'bcb'==namedef test_3():a = 1assert a == 2def test_4():a = 4assert a == 2assert 'a' in 'abc'assert 'a' not in 'abc'assert 'a' is not Trueassert 'a' is False

右击可以单独运行某个函数,看看哪个错了!

class TestTint:def test_5(self):a = 1assert a == 2def test_6(self):a = 1assert a == 2def test_7(self):a = 1assert a == 2

在这里插入图片描述

\;\\\;\\\;

非测试文件

如果是按pytest格式的文件名,但是内容不是测试的话,那么会出现(没有发现测试)

#test_calc.py
a = 1
b = 2
print(a + b)

在这里插入图片描述
\;\\\;\\\;

@pytest.fixture()装饰器

import pytest@pytest.fixture(scope='function')
def fixture1():print('前置步骤1')return 4@pytest.fixture(scope='function')
def fixture2():print('前置步骤2')return 2@pytest.fixture(scope='function',autouse=True)
def fixture3():print('前置步骤3')return 2def test_1(fixture1,fixture2):assert fixture1 == 2assert fixture2 == 2def test_2(fixture3):assert fixture3 == 2if __name__ == '__main__':pytest.main()

可以在测试函数的位置,右击运行test_1或test_2函数

\;\\\;\\\;

pytest+selenium

关于selenium使用的edge驱动器,版本要和电脑上装的edge版本一致!

pytest类要以Test为前缀

#test_f.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
import pytest# 前置方法
@pytest.fixture(scope='class')
def driver():driver = webdriver.Edge(executable_path=r"C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Scripts\msedgedriver.exe")#C:\ProgramData\anaconda3\Scripts\msedgedriver.exereturn driverclass TestSpider:# fixture函数作为形参def test_baidu(self, driver):driver.get('https://www.baidu.com/')title = driver.titleurl = driver.current_urltext = driver.find_element(By.CSS_SELECTOR, "a[href*='news.baidu.com']").textbutton = driver.find_element(By.ID, 'su').get_attribute('value')# 检测assert title == '百度一下,你就知道'assert url == 'https://www.baidu.com/'assert text == '新闻'assert button == '百度一下'# sleep(3)
# driver.close()if __name__ == '__main__':pytest.main()

运行命令 pytest test_f.py

http://www.lryc.cn/news/258270.html

相关文章:

  • arcgis for js 添加自定义叠加图片到地图坐标点上
  • 记录 | linux下互换键盘的Ctrl和CapsLock键
  • 【公网远程手机Android服务器】安卓Termux搭建Web服务器
  • 【银行测试】金融项目+测试方法范围分析,功能/接口/性能/安全...
  • Java网络编程——安全网络通信
  • 云原生数据库是什么?它的作用是啥?
  • 使用ansible批量初始化服务器
  • 国标GB28181安防视频云平台EasyCVR出现持续重启现象,是什么问题?该如何解决?
  • 【APP安卓测试工具】adb(Android Debug Bridge)
  • 图像融合领域的挑战
  • Tomcat配置成服务、开机自启动
  • IntelliJ IDEA创建一个spark的项目
  • 【数据库】数据库多种锁模式,共享锁、排它锁,更新锁,增量锁,死锁消除与性能优化
  • 串口通信(1)-硬件知识
  • 越南语翻译,人工翻译哪个值得信赖?
  • 攻防世界题目练习——Web引导模式(五)(持续更新)
  • attack vector
  • 好看的早上问候语早安图片,今天最新唯美温馨祝福语
  • 人体关键点检测2:Pytorch实现人体关键点检测(人体姿势估计)含训练代码
  • 聚类分析 | Matlab实现基于谱聚类(Spectral Cluster)的数据聚类可视化
  • 【回眸】Tessy 单元测试软件使用指南(三)怎么打桩和指针测试
  • 关系型数据库-SQLite介绍
  • 使用shell脚本将一台虚拟机上面数据分发到其他虚拟机上面xsync
  • OpenGL学习(二)绘制三维图形 固定管线
  • 微信小程序游戏:移动游戏市场的新兴力量
  • Netflix Mac(奈飞客户端)激活版软件介绍
  • 【Docker】进阶之路:(十)Docker日志管理
  • Lcss算法介绍与应用演示
  • 【SpringBoot】从入门到精通的快速开发指南
  • 每日一练【长度最小的子数组】