pytest

Issue mocking with a decorated method call in Python 2.7

蹲街弑〆低调 提交于 2019-12-05 22:09:42
This code: import mock from functools import wraps def dec(f): @wraps(f) def f_2(*args, **kwargs): pass return f_2 class Example(object): def __init__(self): pass @dec def method_1(self, arg): pass def method_2(self, arg): self.method_1(arg) def test_example(): m = mock.create_autospec(Example) Example.method_2(m, "hello") m.method_1.assert_called_once_with("hello") produces this error with py.test def test_example(): m = mock.create_autospec(Example) > Example.method_2(m, "hello") example.py:26: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

Why would a pytest factory as fixture be used over a factory function?

南楼画角 提交于 2019-12-05 21:07:32
In the py.test docs it describes declaring factory methods as fixtures, like-so: @pytest.fixture def make_foo(): def __make_foo(name): foo = Foo() foo.name = name return foo return __make_foo What are the benefits/tradeoffs of doing this over just defining a make_foo function and using that? I don't understand why it is a fixture. Actually, the most important advantage is being able to use other fixtures, and make the dependency injection of pytest work for you. The other advantage is allowing you to pass parameters to the factory, which would have to be static in a normal fixture. Look at

pytest和unittest对比

浪子不回头ぞ 提交于 2019-12-05 20:08:30
一、用例编写规则 1.在使用unittest编写测试用例的时候,需要遵循先创建testclass,测试类继承unittest.TestCase的固定格式。 pytest不需要,框架规范更自由,可以直接写def test_XXX()即可 (1)测试文件名必须以“test_”开头或者"_test"结尾(如:test_ab.py) (2)测试方法必须以“test_”开头。 (3)测试类命名以"Test"开头。      二、用例分类执行 1、unittest默认执行全部用例,也可以通过加载testsuit,执行部分用例。 2、pytest可以通过@pytest.mark来标记类和方法,pytest.main加入参数("-m")可以只运行标记的类和方法。 三、用例前置和后置 1.unittest提供了setUp/tearDown,只能针对所有用例。 2.pytest中的fixture显然更加灵活。 可以任意自定义方法函数,只要加上@pytest.fixture()这个装饰器,那么被装饰的方法就可以被使用 四、参数化 1、unittest需依赖ddt库, 2、pytest直接使用 @pytest.mark.parametrize装饰器。 五、断言 1.unittest断言需要记很多断言格式(assertEqual、assertIn、assertTrue、assertFalse) 2

如何安装接口自动化环境(Python)?

谁说我不能喝 提交于 2019-12-05 20:01:33
环境: Windows 10 Python 3.7 JDK 1.8 Request+unnittest: 1.安装JDK (1)从官网下载JDK,开始安装,默认即可 (2)CMD下检查JDK版本信息:java -version 2.安装Python (1)安装Python 3.7 ,可勾选配置环境变量 (2)检查环境变量,path中指向python安装路径及Scripts路径,如下图 (3)CMD下检查python版本信息:python --version 3.安装开发Python代码工具:Eclipse、Pycharm等,可参考: http://www.360doc.com/content/17/1202/21/8728596_709337714.shtml (1)下载Eclipse,解压缩 (2)打开Eclipse,配置JDK:Window - Preferences - Java - Installed JREs ,导入JDK,指向JRE目录 如果使用Eclipse要运行Maven版本,JDK配置如下: (3)安装PyDev插件:https://blog.csdn.net/qq_23090489/article/details/91378551 Request+Pytest: 1.安装pytest (1)CMD下执行命令pip install pytest,安装成功 (2

How to timeout an async test in pytest with fixture?

喜你入骨 提交于 2019-12-05 19:14:02
问题 I am testing an async function that might get deadlocked. I tried to add a fixture to limit the function to only run for 5 seconds before raising a failure, but it hasn't worked so far. Setup: pipenv --python==3.6 pipenv install pytest==4.4.1 pipenv install pytest-asyncio==0.10.0 Code: import asyncio import pytest @pytest.fixture def my_fixture(): # attempt to start a timer that will stop the test somehow asyncio.ensure_future(time_limit()) yield 'eggs' async def time_limit(): await asyncio

pytest 打印调试信息

与世无争的帅哥 提交于 2019-12-05 18:19:53
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):

How to re-run Failed Test and set re-tries for python3 py.test

懵懂的女人 提交于 2019-12-05 18:13:05
I have few web service related test which sends http requests and the response is verified by py.test test cases. I usually get 1 or 2 failures out of 50 tests which are fails due to intermittent slow web server response gathering or due to network. Is there a way I can re-run or add number of retires to a py.test test case before actually marking it as a Failed one ? Something like run a test 3 times before marking it as failure and moving to the next one, If test passes in any attempt(1 or 2 or in 3) mark it as passed ? flaky describes a possible solution to your problem: https://github.com

How to customize the pytest name

岁酱吖の 提交于 2019-12-05 17:35:14
I would like to customize the output name of my pytest to include the name of my fixtures So I have def test_t1( when_creating_a_project_from_a_sales_handoff, with_a_new_customer, and_no_conflicting_data_exists, create_project): it_will_create_a_customer_with_the_releavant_information() it_will_create_a_project_that_references_the_newly_created_customer() and I'd like the displayed test name to be some version of when_creating_a_project_from_a_sales_handoff with_a_new_customer and_no_conflicting_data_exists create_project How can I do this? I tried creating @fixture def namer(request): request

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

試著忘記壹切 提交于 2019-12-05 16:43:30
问题 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. 回答1: try to decorate your test as @pytest.mark.longrun in your conftest.py def

How send a config variable to a py.test test?

旧城冷巷雨未停 提交于 2019-12-05 16:38:56
I have a test suite which needs to run with multiple backends. It isn't a simple parameterized test though since it applies to the whole suite (multiple files/modules). I can control the run via the environment, but I'm wondering if py.test has a clearer way to express this. That is, I'm looking for something like this: py.test --set-mode ALPHA Then in my test I would read this value: if py.test.mode == 'ALPHA': Using pytest_addoption : test_blah.py def test_something(mode): if mode == 'ALPHA': assert True else: assert False conftest.py import pytest def pytest_addoption(parser): parser