pytest

Pytest实战API测试框架

可紊 提交于 2019-12-04 03:33:29
https://www.jianshu.com/p/40a0b396465c?utm_campaign=hugo&utm_medium=reader_share&utm_content=note&utm_source=weixin-timeline&from=timeline&isappinstalled=0 功能规划 数据库断言 pymysql -> 封装 环境清理 数据库操作 -> Fixtures 并发执行 pytest-xdist 多进程并行 复合断言 pytest-check 用例重跑 pytest-rerunfailures 环境切换 pytest-base-url 数据分离 pyyaml 配置分离 pytest.ini 报告生成 pytest-html, allure-pytest 用例等级 pytest-level 限制用例超时时间 pytest-timeout 发送报告邮件 通过自定Fixture及Hooks实现 安装相应的包 pip安装时可以通过 -i https://pypi.doubanio.com/simple/ ,指定使用豆瓣的源, 下载稍微快一点 pip install requests pymysql pyyaml pytest pyetst-xdist pytest-check pytest-rerunfailures pytest-base

Pytest实战Web测试框架

本小妞迷上赌 提交于 2019-12-04 03:32:44
https://www.jianshu.com/p/9a03984612c1?utm_campaign=hugo&utm_medium=reader_share&utm_content=note&utm_source=weixin-timeline&from=timeline&isappinstalled=0 项目结构 用例层(测试用例) | Fixtures层(业务流程) | PageObject层 | Utils实用方法层 使用pytest-selenium 基础使用 # test_baidu.py def test_baidu(selenium): selenium.get('https://www.baidu.com') selenium.find_element_by_id('kw').send_keys('简书 韩志超') selenium.find_element_by_id('su').click() 运行 $ pytest test_baidu.py --driver=chrome 或配置到pytest.ini中 [pytest] addopts = --driver=chrome 使用chrome options # conftest.py import pytest @pytest.fixture def chrome_options(chrome

Default skip test unless command line parameter present in py.test

青春壹個敷衍的年華 提交于 2019-12-04 02:14:50
I have a long run test, which lasts 2 days, which I don't want to include in a usual test run. I also don't want to type command line parameters, that would deselect it and other tests at every usual test run. I would prefer to select a default-deselected test, when I actually need it. I tried renaming the test from test_longrun to longrun and use the command py.test mytests.py::longrun but that does not work. try to decorate your test as @pytest.mark.longrun in your conftest.py def pytest_addoption(parser): parser.addoption('--longrun', action='store_true', dest="longrun", default=False, help

Python推荐一整套开发工具

為{幸葍}努か 提交于 2019-12-04 01:10:42
原文: https://sourcery.ai/blog/python-best-practices/ 在开始一个新的Python项目时,很容易不做规划直接进入编码环节。花费少量时间,用最好的工具设置项目,将节省大量时间并带来更快乐的编码体验。 在理想的世界中,所有开发人员使用的依赖库都是相同的,代码将被完美地格式化,禁止常见错误,并且测试将涵盖所有内容。此外,每次提交代码时都会确保符合这些要求。 在本文中,我将介绍如何设置一个这样的理想项目。你可以跟随我的步骤操作,也可以直接开始安装pipx和pipenv,然后生成新项目。 让我们创建一个新的项目目录: mkdir best_practicescd best_practices Python命令行工具与pipx Pipx是一个方便的实用程序,允许快速安装python命令行工具。我们将用它来安装 pipenv 和 cookiecutter 。 python3 -m pip install --user pipxpython3 -m pipx ensurepath 使用 pipenv 进行依赖管理 Pipenv自动为您的项目创建和管理virtualenv,以及在安装/卸载软件包时从Pipfile添加/删除软件包。它还生成了非常重要的Pipfile.lock文件,用于生成确定性构建。 知道你和你的同事正在使用相同的库版本

python之pytest测试sqlalchemy代码

我的未来我决定 提交于 2019-12-03 23:45:29
注释:运行环境linux+python3.7.3+pytest5.2.2+postgresql+flask-sqlalchemy2.4.1 公司之前用的NoSQL作数据管理,最近让我把数据库使用关系型数据库翻译一下,老大决定使用postgresql并采用ORM管理数据库,数据库翻译完,老大说为了保证数据稳定迁移,让我用pytest写一下测试用例,第一次写啊,遇到各种坑,第一就是测试项目的目录搭建,因为之前会一点unittest,unittest实现自动化测试可以写在同一个类中进行测试,当时不知道怎么搭合适,各种实验,最终采用下边的目录结构,很好的做到解耦。第二坑就是转化过来的数据表各种外键约束,直接使用pytest运行测试他会自动的搜索test开头的测试用例,这样没有顺序,我某些表中用到的外键这张表输入还没有插入,就会测试失败,后来又使用pytest按照表的外键关系一个个的添加,等价于手动排序 1:搭建测试项目结构 (目录结构)这里每个文件都是包,是因为你在使用pytest执行用例的时候他会自动搜索包中test开头的文件 .├── manage.py # 项目启动文件├── moduls # 模型类存放包│ ├── __init__.py│ └── moduls.py          # 模型类py文件└── test # 单元测试包 ├── conftest.py #

Why python's monkeypatch doesn't work when importing a class instead of a module?

耗尽温柔 提交于 2019-12-03 23:23:09
I was having issues while using the code of the accepted answer here . The code works depending on how I do the import of datetime. Why is that? Is it possible to mock it so it works both ways? I am using Python 3.4 . The following code illustrates the problem: import pytest from datetime import datetime mockdate = datetime(2000, 1, 1, 0, 0, 0) @pytest.fixture(autouse=True) def patch_datetime_now(monkeypatch): class mydatetime: @classmethod def now(cls): return mockdate monkeypatch.setattr('datetime.datetime', mydatetime) def test_doesnt_work(): assert datetime.now() == mockdate def test_works

How to run py.test against different versions of python?

陌路散爱 提交于 2019-12-03 22:20:46
Is it possible to run py.test with different versions of python without plugins (like xdist ) or tox ? Jack O'Connor The simplest way to do it is by running the pytest module directly with -m , for example: python2.6 -m pytest Note that you have to have pytest installed for that version of Python. In addition, you need to install all pytest plugins that you are using for that version of Python too. hpk42 You can create a standalone pytest script with py.test --genscript=mypytest and then do pythonXY mypytest to run tests with a particular python version. You do not need to install pytest for

10、pytest -- skip和xfail标记

自作多情 提交于 2019-12-03 20:07:30
目录 1. 跳过测试用例的执行 1.1. @pytest.mark.skip 装饰器 1.2. pytest.skip 方法 1.3. @pytest.mark.skipif 装饰器 1.4. pytest.importorskip 方法 1.5. 跳过测试类 1.6. 跳过测试模块 1.7. 跳过指定文件或目录 1.8. 总结 2. 标记用例为预期失败的 2.1. 去使能 xfail 标记 3. 结合 pytest.param 方法 往期索引: https://www.cnblogs.com/luizyao/p/11771740.html 实际工作中,测试用例的执行可能会依赖于一些外部条件,例如:只能运行在某个特定的操作系统( Windows ),或者我们本身期望它们测试失败,例如:被某个已知的 Bug 所阻塞;如果我们能为这些用例提前打上标记,那么 pytest 就相应地预处理它们,并提供一个更加准确的测试报告; 在这种场景下,常用的标记有: skip :只有当某些条件得到满足时,才执行测试用例,否则跳过整个测试用例的执行;例如,在非 Windows 平台上跳过只支持 Windows 系统的用例; xfail :因为一个确切的原因,我们知道这个用例会失败;例如,对某个未实现的功能的测试,或者阻塞于某个已知 Bug 的测试; pytest 默认不显示 skip 和 xfail

How to test a Django model with pytest?

一世执手 提交于 2019-12-03 17:47:31
问题 I am getting started with pytest. I have configured pytest, anyway I couldn't found a resource on Django specific testing with pytest. How do I test a model with pytest_django ? I have already asked a question on unittesting, how do I efficiently test this Django model? I want know how the same tests can be written with py.test? adding below the model and the tests written in unittest. the model under test is, class User(AbstractBaseUser, PermissionsMixin): username = models.CharField(max

Chaining tests and passing an object from one test to another

匆匆过客 提交于 2019-12-03 16:07:53
I'm trying to pass the result of one test to another in pytest - or more specifically, reuse an object created by the first test in the second test. This is how I currently do it. @pytest.fixture(scope="module") def result_holder: return [] def test_creation(result_holder): object = create_object() assert object.status == 'created' # test that creation works as expected result_holder.append(object.id) # I need this value for the next test # ideally this test should only run if the previous test was successful def test_deletion(result_holder): previous_id = result_holder.pop() object = get