pytest

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

元气小坏坏 提交于 2020-12-19 11:00:36
搞过自动化测试的小伙伴,相信都知道,在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介绍 Playwright是一个强大的Python库,仅用一个API即可自动执行 Chromium

Pytest capture not working - caplog and capsys are empty

纵饮孤独 提交于 2020-12-15 06:55:13
问题 I am trying to create unit tests for a logger, but - even though the events are logged - the caplog and capsys remain empty. What am I doing wrong? 回答1: It turned out that my logger had property propagate set to False . As a result, the events were not forwarded to LogCaptureHandler added by pytest to the root logger. Both caplog and capsys depend on LogCaptureHandler receiving the propagated events. 来源: https://stackoverflow.com/questions/61702794/pytest-capture-not-working-caplog-and-capsys

How to remove a library with monkeypatch or mock in pytest?

ぃ、小莉子 提交于 2020-12-15 06:38:22
问题 If my library has a contrib extra that has dependencies in it (say requests ) that I want users to have to install to have access to a CLI API, but I install the contrib extra during my tests in CI how do I use pytest's MonkeyPatch to remove the dependencies during tests to ensure my detection is correct? For example, if the contrib extra will additionally install requests and so I want users to have to do $ python -m pip install mylib[contrib] to then be able to at the command line have a

How to remove a library with monkeypatch or mock in pytest?

久未见 提交于 2020-12-15 06:37:51
问题 If my library has a contrib extra that has dependencies in it (say requests ) that I want users to have to install to have access to a CLI API, but I install the contrib extra during my tests in CI how do I use pytest's MonkeyPatch to remove the dependencies during tests to ensure my detection is correct? For example, if the contrib extra will additionally install requests and so I want users to have to do $ python -m pip install mylib[contrib] to then be able to at the command line have a

Fatal Python Error when running pytest with QT

女生的网名这么多〃 提交于 2020-12-13 05:02:50
问题 I'm new to pytest and I'm having trouble trying to run one of my test files. I'm getting the following output: ================================================== test session starts ================================================== platform linux -- Python 3.7.6, pytest-5.3.5, py-1.8.1, pluggy-0.13.1 -- /home/jarel/anaconda3/bin/python cachedir: .pytest_cache hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('/home/jarel/workspace/spyder/rpgland/.hypothesis/examples')

What happens when a python mock has both a return value and a list of side effects?

一笑奈何 提交于 2020-12-13 04:16:34
问题 I'm having trouble understanding what's happening in some test code. It looks like this: import pytest from unittest.mock import MagicMock from my_module import MyClass confusing_mock = MagicMock( return_value=b"", side_effect=[ ConnectionError(), b"another_return_value?", b"another_another_return_value?" ]) mocked_class = MyClass() monkeypatch.setattr(mocked_class, "method_to_call_thrice", confusing_mock) I know that: side_effect is a function to be called whenever the mock is called but if

What happens when a python mock has both a return value and a list of side effects?

回眸只為那壹抹淺笑 提交于 2020-12-13 04:16:25
问题 I'm having trouble understanding what's happening in some test code. It looks like this: import pytest from unittest.mock import MagicMock from my_module import MyClass confusing_mock = MagicMock( return_value=b"", side_effect=[ ConnectionError(), b"another_return_value?", b"another_another_return_value?" ]) mocked_class = MyClass() monkeypatch.setattr(mocked_class, "method_to_call_thrice", confusing_mock) I know that: side_effect is a function to be called whenever the mock is called but if

How to mock function call inside other function using pytest?

╄→гoц情女王★ 提交于 2020-12-13 03:16:43
问题 def publish_book(publisher): # some calculations factor = 4 print('xyz') db_entry(factor) # db entry call which I want to mock print('abc') def update(): publish_book('xyz') @pytest.mark.django_db def test_update(mocker): # in here I'm unable to mock nested function call pass I have db_entry() function call in publish_book(). how can we mock db_entry() function call inside publish_book.I want to perform other calculations of publish_book(), but only skip(mock) the db_entry() call. 回答1: You

How to mock function call inside other function using pytest?

六月ゝ 毕业季﹏ 提交于 2020-12-13 03:14:30
问题 def publish_book(publisher): # some calculations factor = 4 print('xyz') db_entry(factor) # db entry call which I want to mock print('abc') def update(): publish_book('xyz') @pytest.mark.django_db def test_update(mocker): # in here I'm unable to mock nested function call pass I have db_entry() function call in publish_book(). how can we mock db_entry() function call inside publish_book.I want to perform other calculations of publish_book(), but only skip(mock) the db_entry() call. 回答1: You

python笔记41-虚拟环境virtualenv

血红的双手。 提交于 2020-12-12 19:55:44
前言 如果你是一个python初学者,我是不建议你搞python虚拟环境的,我看到很多python的初学者同学,使用最新版的pycharm,新建一个工程时候默认就是venu虚拟环境。 然后在使用cmd里面pip安装第三方包的时候,在工程里面死活导入不成功,搞的开始怀疑人生。(你给他讲这是虚拟环境venu,他会一脸懵逼!) 那么什么情况下会用到虚拟环境呢? 如果你已经使用python写了一个项目的自动化测试代码了,然后你开始写第二个项目,第三个项目。。。。当你写的项目越来越多时候, 你会发现python使用pip安装的第三方包会越来越多,并且你会在安装第三方包的时候踩坑,比如不同的python版本匹配不同的pytest包,或者django1和django2差异又很大。 这时候你会发现我需要同时安装2个版本的包,对比差异性,或者同时开发多个项目,很显然一套环境已经不能满足我们的需求了。 于是需要有多套python环境,也就是需要学会使用虚拟环境了!virtualenv在windows和linux上安装是有一些差异的。 windows安装virtualenv virtualenv 是用来创建 Python 的虚拟环境的库,虚拟环境能够独立于真实环境存在,并且可以同时有多个互相独立的 Python 虚拟环境, 每个虚拟环境都可以营造一个干净的开发环境,对于项目的依赖