pytest

How can I limit the maximum running time for a unit test?

落花浮王杯 提交于 2019-11-29 05:26:20
I am currently running some unit tests that might either take a long time before failing or run indefinitely. In a successful test run they will always complete within a certain amount of time. Is it possible to create a pytest unit test that will fail if it does not complete within a certain amount of time? Mrinal Shukla you can install the pytest-timeout plugin and then mark your test functions with a timeout in seconds. @pytest.mark.timeout(300) def test_foo(): pass Look at the plugin download and usage instructions at https://pypi.python.org/pypi/pytest-timeout 来源: https://stackoverflow

pytest命令行

半世苍凉 提交于 2019-11-29 05:10:47
1.pytest -help 2.pytest -vs(-v是啰嗦模式,啥信息都显示;-s允许测试运行时输出任何符合标准的输出流信息,例如代码里面的print) 执行结果: E:\myproj\pytest_demo>pytest -vs ================================================================================== test session starts ================================================================================== platform win32 -- Python 3.6.5, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 -- e:\soft\python\python36\python.exe cachedir: .pytest_cache rootdir: E:\myproj\pytest_demo, inifile: pytest.ini, testpaths: ./testcases collected 9 items testcases/test_02.py::test_1 FAILED # FAILED指测试失败 testcases/test_02

Pytest权威教程-04断言的编写和报告

不羁岁月 提交于 2019-11-29 04:57:46
断言的编写和报告 使用assert语句进行断言 pytest允许你使用标准的Python assert 断言语句来验证测试中的期望结果和实际结果。 例如,你可以编写以下内容: # test_assert1.py文件内容 def f(): return 3 def test_function(): assert f() == 4 来断言你的函数返回一个特定的值。 如果此断言失败,你将看到函数调用的返回值: $ pytest test_assert1.py =========================== test session starts ============================ platform linux -- Python 3.x.y,pytest-3.x.y,py-1.x.y,pluggy-0.x.y rootdir: $REGENDOC_TMPDIR,inifile: collected 1 item test_assert1.py F [100%] ================================= FAILURES ================================= ______________________________ test_function _____________________________

Pytest权威教程-05Pytest fixtures:清晰 模块化 易扩展

删除回忆录丶 提交于 2019-11-29 04:57:40
Pytest fixtures:清晰 模块化 易扩展 2.0/2.3/2.4版本新函数 text fixtures的目的是为测试的重复执行提供一个可靠的固定基线。 pytest fixture比经典的xUnit setUp/tearDown方法有着显着的改进: fixtures具有明确的名称,在测试用例/类/模块或整个项目中通过声明使用的fixtures名称来使用。 fixtures以模块化方式实现,因为每个fixture名称都会触发调用fixture函数,该fixture函数本身可以使用其它的fixtures。 从简单的单元测试到复杂的函数测试,fixtures的管理允许根据配置和组件选项对fixtures和测试用例进行参数化,或者在测试用例/类/模块或整个测试会话范围内重复使用该fixture。 此外,pytest继续支持经典的xUnit风格的setup方法。 你可以根据需要混合使用两种样式,逐步从经典样式移动到新样式。 你也可以从现有的unittest.TestCase样式或基于nose的项目开始。 Fixtures作为函数参数使用 测试用例可以通过在其参数中使用fixtures名称来接收fixture对象。 每个fixture参数名称所对应的函数,可以通过使用 @pytest.fixture 注册成为一个fixture函数,来为测试用例提供一个fixture对象。

Pytest权威教程-03原有TestSuite的执行方法

为君一笑 提交于 2019-11-29 04:57:22
原有TestSuite的执行方法 Pytest可以与大多数现有的测试套件(testsuite)一起使用,但是它的加载方式方式不像nose或Python的默认单元测试框架的测试运行器(test runner)。 在使用本节之前,你需要安装pytest。 使用pytest运行已存在的测试套件(test suite) 假设你想要在某个地方为现有仓库(respsitory)做贡献代码。 在使用某种版本控制软件拉取代码和设置完 virtualenv (可选)后,你需要运行: cd <仓库名> pip install -e . # 环境所依赖的'python setup.py develop' 和 'conda develop'包 在你项目根目录中,这将为你的代码在 site-packages 中设置一个符号链接,来允许你无需安装自己的代码即可执行测试。 在开发模式下如此使用,可以避免每次要运行测试时重新安装,这比每次使用 sys.path 将测试指向本地代码更简单。 或者你可以考虑使用[tox。 译者注: 实际官方并没有写Pytest怎么执行TestSuite,执行方法可以参考个人的另一篇文章: [ https://www.jianshu.com/p/6a05ccd3ca94 来源: https://www.cnblogs.com/superhin/p/11455351.html

Python接口测试课程(第四天)-接口测试框架实现

我怕爱的太早我们不能终老 提交于 2019-11-29 04:56:26
目录 Python接口测试课程(第一天)-Python基础 Python接口测试课程(第二天)-接口测试快速实践 Python接口测试课程(第三天)-接口安全验证,参数化及断言 Python接口测试课程(第四天)-接口测试框架实现 更多学习资料请加添加作者微信:lockingfree获取 第四天: Python接口测试框架 什么是框架 目前主流接口测试方案 工具派 Java派 Python派 接口平台 框架类型 录制回放 数据驱动 行为驱动 框架的分层与规划 框架分层 表示层: (用户界面) 业务逻辑层: (读取数据,配置并组装发送请求)+执行控制层(pytest) 数据层: (配置读取/数据读取/数据库连接/其他(log/email) 框架规划 case: 测试用例目录 user: (用户模块) test_user.py: 测试用例 case.py: 用例公共方法 data: 数据文件目录 test_user_data.xlsx: 测试用例数据文件 conf: 配置文件目录 default.conf: 默认配置文件 report: pytest生成的报告保存路径 log: log保存路径,按天生成log common: 公共方法目录 config.py: 配置文件读取 data.py: 数据文件读取 db.py: 数据库连接 log.py: 日志配置 send_email.py:

How to add a screenshot to allure report with python?

£可爱£侵袭症+ 提交于 2019-11-29 04:26:48
I have this code: # coding: utf-8 from selenium import webdriver import pytest import allure @pytest.yield_fixture(scope='session') def driver(): _driver = webdriver.PhantomJS() yield _driver _driver.quit() def test_ya(driver): with allure.step('open ya.ru and take screenshot'): driver.get('http://ya.ru/') allure.attach('screenshot', driver.get_screenshot_as_png(), type='png') and I try to take a screenshot and save it to allure report, after execution I have: > with self._attachfile("%s-attachment.%s" % (uuid.uuid4(), attach_type.extension)) as f: if isinstance(body, text_type): E

pytest -> How to use fixture return value in test method under a class

不问归期 提交于 2019-11-29 03:49:18
问题 I have a fixture that returns a value like this: import pytest @pytest.yield_fixture(scope="module") def oneTimeSetUp(browser): print("Running one time setUp") if browser == 'firefox': driver = webdriver.Firefox() print("Running tests on FF") else: driver = webdriver.Chrome() print("Running tests on chrome") yield driver print("Running one time tearDown") This fixture gets the browser value from another fixture which is reading the command line option. Then I have a test class where I have

How and where does py.test find fixtures

 ̄綄美尐妖づ 提交于 2019-11-29 02:14:12
问题 Where and how does py.test look for fixtures? I have the same code in 2 files in the same folder. When I delete conftest.py, cmdopt cannot be found running test_conf.py (also in same folder. Why is sonoftest.py not searched? # content of test_sample.py def test_answer(cmdopt): if cmdopt == "type1": print ("first") elif cmdopt == "type2": print ("second") assert 0 # to see what was printed content of conftest.py import pytest def pytest_addoption(parser): parser.addoption("--cmdopt", action=

pytest 打印调试信息

元气小坏坏 提交于 2019-11-29 01:43:26
pytest_lean2.py #coding=utf-8 import pytest import os import sys import time import json sys.path.append("/".join(os.path.dirname(os.path.abspath(__file__)).split("/")[:-1])+"/lib") import requests sys.path.append("/".join(os.path.dirname(os.path.abspath(__file__)).split("/")[:-1])) from util.getinfolib import getinfo import logging,sys log = logging.getLogger(__name__) class TestUM: ''' setup_class**********> setup_method##########>> setup----------> teardown----------> teardown_method##########>> teardown_class**********> ''' def setup(self): print ("setup---------->") def teardown(self):