pytest

How to use pytest-aiohttp fixtures with scope session

痞子三分冷 提交于 2021-01-02 06:07:10
问题 I am trying to write tests for aiohttp application. I am using pytest-aiohttp plugin. My intention is to initialize and run the application once before first test execution and tear down after all tests finished. pytest-aiohttp fixtures like 'loop', 'test_client' are very helpful but they have scope='function' which means I can not use them from my own fixture with scope='session'. Is there a way to workaround this? And if not then what would be a proper approach for achieving my goal without

How to include selenium screenshot in pytest bdd for passed tests?

时光怂恿深爱的人放手 提交于 2021-01-01 07:54:27
问题 I am writing tests in pytest bdd with selenium. I am using pytest-html to generate report. For debug purpose or just to have a proper logging, I want selenium screenshots and rest of the logs in html report. But I am unable to have selenium screenshot in passed report. Here are the things I am trying. There is a pytest-html hook wrapper in conftest.py conftest.py @pytest.mark.hookwrapper def pytest_runtest_makereport(item, call): pytest_html = item.config.pluginmanager.getplugin('html')

Generating py.test tests in Python

被刻印的时光 ゝ 提交于 2020-12-28 13:30:52
问题 Question first, then an explanation if you're interested. In the context of py.test, how do I generate a large set of test functions from a small set of test-function templates? Something like: models = [model1,model2,model3] data_sets = [data1,data2,data3] def generate_test_learn_parameter_function(model,data): def this_test(model,data): param = model.learn_parameters(data) assert((param - model.param) < 0.1 ) return this_test for model,data in zip(models,data_sets): # how can py.test can

Generating py.test tests in Python

China☆狼群 提交于 2020-12-28 13:30:33
问题 Question first, then an explanation if you're interested. In the context of py.test, how do I generate a large set of test functions from a small set of test-function templates? Something like: models = [model1,model2,model3] data_sets = [data1,data2,data3] def generate_test_learn_parameter_function(model,data): def this_test(model,data): param = model.learn_parameters(data) assert((param - model.param) < 0.1 ) return this_test for model,data in zip(models,data_sets): # how can py.test can

Generating py.test tests in Python

断了今生、忘了曾经 提交于 2020-12-28 13:26:10
问题 Question first, then an explanation if you're interested. In the context of py.test, how do I generate a large set of test functions from a small set of test-function templates? Something like: models = [model1,model2,model3] data_sets = [data1,data2,data3] def generate_test_learn_parameter_function(model,data): def this_test(model,data): param = model.learn_parameters(data) assert((param - model.param) < 0.1 ) return this_test for model,data in zip(models,data_sets): # how can py.test can

pytest: assert almost equal

你。 提交于 2020-12-24 03:07:38
问题 How to do assert almost equal with py.test for floats without resorting to something like: assert x - 0.00001 <= y <= x + 0.00001 More specifically it will be useful to know a neat solution for quickly compare pairs of float, without unpacking them: assert (1.32, 2.4) == i_return_tuple_of_two_floats() 回答1: I noticed that this question specifically asked about py.test. py.test 3.0 includes an approx() function (well, really class) that is very useful for this purpose. import pytest assert 2.2

pytest: assert almost equal

回眸只為那壹抹淺笑 提交于 2020-12-24 03:06:57
问题 How to do assert almost equal with py.test for floats without resorting to something like: assert x - 0.00001 <= y <= x + 0.00001 More specifically it will be useful to know a neat solution for quickly compare pairs of float, without unpacking them: assert (1.32, 2.4) == i_return_tuple_of_two_floats() 回答1: I noticed that this question specifically asked about py.test. py.test 3.0 includes an approx() function (well, really class) that is very useful for this purpose. import pytest assert 2.2

Unittest框架的从零到壹(一)

半世苍凉 提交于 2020-12-19 16:26:59
前言 Python中有非常多的单元测试框架,如unittest、pytest、nose、doctest等,Python2.1及其以后的版本已经将unittest作为一个标准模块放入Python开发包中。并且在pytest框架中,是可以兼容运行unittest的代码的,简单来说,就是学一顶俩。并且unittest也是比较简单上手的,等unittest篇完结后,我们再开一个pytest篇。成为自动化测试大神指日可待🙊 认识断言 单元测试本质上就是通过一段代码去验证另外一段代码,那么这段验证的代码里,有一个起到关键作用的关键字,就是 assert -断言。assert(断言)用于判断一个表达式,在表达式条件为 false 的时候触发异常。下面举例说明一下用法 来看看语法格式,一般assert后面跟一个条件和一个断言失败信息 比如 assert 1 == -1,'报错就打印我出来' 断言失败就会抛出 AssertionError 异常,并且把自定义的断言失败信息打印出来 上面这条语句就等价于 if not 1 == -1: print("报错就打印我出来") raise AssertionError 看到这里的话应该很清晰了,所谓断言就是判断一个条件,条件不成立就会抛出异常,称之为断言失败,反之就是断言成功。 代码初体验 了解基本情况后,开始动手敲敲代码吧,始终贯彻代码驱动学习理念

微软最强 Python 自动化工具开源了!不用写一行代码!

三世轮回 提交于 2020-12-19 11:25:44
点击上方“ AirPython ”,选择“ 加为星标 ” 第一时间关注 Python 原创干货! 1. 前言 最近,微软开源了一款非常强大的 Python 自动化依赖库: playwrigh t-python 它支持主流的浏览器,包含:Chrome、Firefox、Safari、Microsoft Edge 等, 同时支持以无头模式 、有头模式运行 playwright-python 提供了同步、异步的 API,可以结合 Pytest 测试框架使用,并且支持浏览器端的自动化脚本录制 项目地址: https://github.com/microsoft/playwright-python 2. 准备 在实战之前,我们只需要 2 步 第 1 步, 安装 playwright-python 依赖库 # 安装依赖库 pip3 install playwright 第 2 步, 安装主流的浏览器驱动 这样,会将 Chromeium、Firefox、Webkit 浏览器驱动下载到本地 # 安装浏览器驱动 python -m playwright install 3. 实战一下 3-1 录制脚本 我们先查看录制脚本的命令说明 其中 python -m playwright codegen 录制脚本 --help 帮助文档 -o 生成自动化脚本的目录 --target 脚本语言, 包含 JS 和

推荐一款最强Python自动化神器!不用写一行代码!

☆樱花仙子☆ 提交于 2020-12-19 11:25:13
关注上方“ 测试开发技术 ”,选择星标, 干货技术,第一时间送达! 搞过自动化测试的小伙伴,相信都知道,在Web自动化测试中,有一款自动化测试神器工具: selenium 。结合标准的WebDriver API来编写Python自动化脚本,可以实现解放双手,让脚本代替人工在Web浏览器上完成指定的操作。 虽然 selenium 有完备的文档,但也需要一定的学习成本,对于一个纯小白来讲还是有些门槛的。 最近, 微软开源 了一个非常强大的自动化项目叫 playwright-python ,项目地址: https://github.com/microsoft/playwright-python 它支持主流的浏览器,包含: Chrome 、 Firefox 、 Safari 、 Microsoft Edge 等,同时支持以 无头模式 、 有头模式运行 ,并提供了 同步 、 异步 的 API,可以结合 Pytest 测试框架使用,并且支持浏览器端的自动化脚本录制。 而对于Python爱好者来说,还有一个更大的福利,这个项目是针对Python语言的纯自动化工具, 可以做到,连一行代码都不用写,就能实现自动化功能 。听起来,简直太碉堡了! 可能你会觉得有点不可思议,真的不用写一行代码吗?但它真的就是这么厉害。下面我们一起看下这个神器。 1. Playwright介绍