pytest

pytest文档33-Hooks函数获取用例执行结果(pytest_runtest_makereport)

馋奶兔 提交于 2020-08-11 14:52:26
前言 pytest提供的很多钩子(Hooks)方法方便我们对测试用例框架进行二次开发,可以根据自己的需求进行改造。 先学习下pytest_runtest_makereport这个钩子方法,可以更清晰的了解用例的执行过程,并获取到每个用例的执行结果。 pytest_runtest_makereport 先看下相关的源码,在 _pytest/runner.py 下,可以导入之后,点进去查看 from _pytest import runner # 对应源码 def pytest_runtest_makereport(item, call): """ return a :py:class:`_pytest.runner.TestReport` object for the given :py:class:`pytest.Item` and :py:class:`_pytest.runner.CallInfo`. """ 这里item是测试用例,call是测试步骤,具体执行过程如下: 先执行when='setup' 返回setup 的执行结果 然后执行when='call' 返回call 的执行结果 最后执行when='teardown'返回teardown 的执行结果 运行案例 conftest.py 写 pytest_runtest_makereport 内容,打印运行过程和运行结果

pytest文档18-配置文件pytest.ini

两盒软妹~` 提交于 2020-08-11 14:39:17
前言 pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行。 ini配置文件 pytest里面有些文件是非test文件 pytest.ini pytest的主配置文件,可以改变pytest的默认行为 conftest.py 测试用例的一些fixture配置 _ init _.py 识别该文件夹为python的package包 tox.ini 与pytest.ini类似,用tox工具时候才有用 setup.cfg 也是ini格式文件,影响setup.py的行为 ini文件基本格式 # 保存为pytest.ini文件 [pytest] addopts = -rsxX xfail_strict = true 使用pytest --help指令可以查看pytest.ini的设置选项 [pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found: markers (linelist) markers for test functions empty_parameter_set_mark (string) default marker for empty parametersets norecursedirs (args)

Pytest08-pytest工具与插件

半腔热情 提交于 2020-08-11 11:08:10
8.pytest工具与插件 8.1 converage.py-代码覆盖率     测试覆盖率是指项目代码被测试用例覆盖的百分比 。使用覆盖率工具可以知道,系统哪些部分的功能没有被测试覆盖。converage.py是Python推荐的覆盖率工具。 8.1.1 覆盖率工具安装     在pytest中可以使用pytest-cov插件,其安装方法如下所示: pip install -U pytest-cov 8.1.2 常用用法 1.运行pytest-cov     使用覆盖工具,可使用选项 --cov=src ,示例代码路径如下所示: Lesson05 |——src | |—— sample | |——————| __init__.py | |——————| calculator.py | |——————| ListSample.py |——test | |——————| __init__.py | |——————| conftest.py | |——————| test_calculator.py     各代码详细内容如下所示: calculator.py def calculator(x,y,operator): if not isinstance(x,(int,)) or not isinstance(y,(int,)) or not isinstance(operator,

软件测试面试题(2)

一笑奈何 提交于 2020-08-11 09:04:59
  经过前面总结的面试题,看到留言和私信都觉得还不错,这里安静在总结一些亲身经历的面试题 1、启动多个app同时运行用例怎么做?代码如何实现? 通过python进行对启动命令行appium进行封装,然后通过多线程的方法进行启动appium进行执行多台手机操作。具体代码: appium---多线程启动app(多台设备启动app) 2、unittest如何操作它的执行顺序 unittest本身执行是无序的,我们可以通过进行创建名称是进行判断执行顺序,也可以通过unittest中的TestSuite来进行添加执行的用例。具体操作: unittest---unittest多种加载用例方法 3、unittest中能否进行更改执行规则?不已test开头的方式? 我们如果仔细阅读过unittest的方法就可以发现,其实是可以进行在源码中修改的。 4、postman中的断言如何操作? postman的断言是通过javaScript来编写的,postman中有个Tests,我们可以在里面进行添加断言,也可以通过javaScript代码进行自己编写断言。具体操作: postman---postman增加断言 5、unittest的弊端? unittest目前不支持用例失败重跑,需要进行二次开发 6、通过学生,班级,科目,分数,学期这些你如何创建数据表? 这里可能就考察数据库的能力和业务逻辑流程了

测试用例 setup 和 和 teardown

我的未来我决定 提交于 2020-08-11 06:51:41
前言   学过unittest的都知道里面用前置和后置setup呾teardown非常好用,在每次用例开始前呾结束后都去执行一次。当然迓有更高级一点的 setupClass 呾 teardownClass,需配合@classmethod 装饰器一起使用,在做 selenium 自动化的时候,它的效率尤为突然,可以叧启动一次浏览器执行多个用例。pytest 框架也有类似于 setup 和 teardown 的用法,并且不止这四个。简单演式几种。 用例运行级别   1、模块级(setup_module/teardown_module)开始于模块始末,全局的 函数级(setup_function/teardown_function)叧对函数用例生效(不在类中) 类级(setup_class/teardown_class)叧在类中前后运行一次(在类中)   setup/teardown 和unittest 里面的 setup/teardown 是一样的功能,setup_class 和 teardown_class 等价于 unittest 里面的setupClass 呾 teardownClass 从结果看出,运行的优先级:setup_class》setup_method》setup 》 用例》teardown》teardown_method》teardown_class  

Pytest单元测试框架——Pytest+Allure+Jenkins的应用

一个人想着一个人 提交于 2020-08-11 02:36:30
一、简介   pytest+allure+jenkins进行接口测试、生成测试报告、结合jenkins进行集成。   pytest是python的一种单元测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,效率更高   allure-pytest是python的一个第三方库。用于连接pytest和allure,使它们可以配合在一起使用。   allure-pytest基于pytest的原始执行结果生成适用于allure的json格式结果。该json格式结果可以用于后续适用allure生成html结果。 二、安装     1、安装pytest,命令行或终端中输入 1 pip install pytest   2、安装allure-pytest,安装成功 1 pip install allure-pytest   allure-pytest安装成功后截图如下。   3、下载安装JDK   官方下载: https://www.oracle.com/java/technologies/javase-jdk11-downloads.html   安装与配置不作阐述请谅解     4、下载安装Jenkins   官方下载: https://www.jenkins.io/   安装与配置不作阐述请谅解 三、下载Allure并配置  

pytest+requests+Python3.7+yaml+Allure+Jenkins+docker实现接口自动化测试

自作多情 提交于 2020-08-10 22:15:32
接口自动化测试框架(用例自动生成) 项目说明 本框架是一套基于 pytest+requests+Python3.7+yaml+Allure+Jenkins+docker 而设计的数据驱动接口自动化测试框架, pytest 作为执行器,本框架无需你使用代码编写用例,那你可能会担心万一有接口之间相互依赖,或者说需要登入的token等之类的接口,该如何编写用例呢,在这里告诉你们本框架已经完美解决此问题,所有的一切将在yaml中进行!!本框架实现了在yaml中进行 接口用例编写,接口依赖关联,接口断言,自定义测试用例运行顺序 ,还有很重要的一点,实现了类 jmeter函数助手 的功能,譬如生成MD5、SHA1、随机定长字符串、时间戳等,只需要你在yaml中使用特殊的写法 $Function(arg)$ ,就能够使用这些函数啦,此外在测试执行过程中,还可以 对失败用例进行多次重试 ,其重试次数和重试时间间隔可自定义;而且可以根据实际需要扩展接口协议,目前已支持 http接口和webservice接口 。 技术栈 requests suds-py3 Allure pytest pytest-html yaml logging Jenkins docker 函数助手 环境部署 命令行窗口执行pip install -r requirements.txt 安装工程所依赖的库文件 解压allure

How can I use test-data/external variable in 'pytest.dependency'?

有些话、适合烂在心里 提交于 2020-08-10 19:10:08
问题 Below pytest code works fine, which increments value . import pytest pytest.value = 1 def test_1(): pytest.value +=1 print(pytest.value) def test_2(): pytest.value +=1 print(pytest.value) def test_3(): pytest.value +=1 print(pytest.value) Output: Prints 2 3 4 I don't want to execute test_2 , when value=2 Is it possible by pytest.dependency() ? If yes, how can i use variable value in pytest.dependency ? If not pytest.dependency , Any alternative ? or any better way of handling such scenarios ?

Why is module not found only when doing unit testing with pytest? [duplicate]

人走茶凉 提交于 2020-08-10 18:53:26
问题 This question already has answers here : PATH issue with pytest 'ImportError: No module named YadaYadaYada' (19 answers) Closed 4 days ago . I'm having a problem running unit tests. I have a project structure like this: Given this directory who-said-what/ | |_ wave_encoder.py | |_ tests/ |_ test_wave_encoder.py where test_wave_encoder.py looks like this: from wave_encoder import * class TestEncoder(): def test_plot_no_fit1(self): encoder = WaveEncoder() self.assertRaises(ValueError, encoder

Python接口自动化测试框架: pytest+allure+jsonpath+requests+excel实现的接口自动化测试框架(已开源)

邮差的信 提交于 2020-08-10 08:49:08
目录 废话 功能 运行机制 已知问题 环境与依赖 目录结构 执行顺序 config.ymal展示 EXcel用例展示 脚本一览 运行结果 致谢 源码地址 废话 和几个朋友聊天然后出来的产物希望能帮到大家学习接口自动化测试,欢迎大家交流指出不合适的地方,源码在文末 功能 实现:get/post请求(上传文件)::理论上其他delete/put等请求也实现了 发送邮件 生成allure测试报告 压缩测试报告文件 数据依赖 运行机制 通过读取配置文件,获取到host地址、提取token的jsonpath表达式,提取实际响应结果用来与预期结果比对的jsonpath表达式。 读取excel用例文件数据,组成一个符合pytest参数化的用例数据,根据每列进行数据处理(token操作、数据依赖) token,写,需要使用一个正常登录的接口,并且接口中要返回token数据,才可以提取,token,读为该请求将携带有token的header,token 无数据的将不携带token 数据依赖处理,从excel中读取出来的格式{"用例编号":["jsonpath表达式1", "jsonpath表达式2"]},通过用例编号来获取对应case的实际响应结果(实际响应结果在发送请求后,回写到excel中),通过jsonpath表达式提取对应的依赖参数字段,以及对应的值