pytest

Checking for valid enum types from protobufs

六眼飞鱼酱① 提交于 2019-12-23 03:51:51
问题 In my protobuf file called skill.proto, I have: message Cooking { enum VegeType { CAULIFLOWER = 0; CUCUMBER = 1; TOMATO = 2 } required VegeType type = 1; } In another file (eg: name.py) I want to check that the enum within the file is a valid type #if (myCookingStyle.type != skill_pb2.Cooking.VegeTypes): print "Error: invalid cooking type" How do I check that myCookingStyle.type is a valid enum type? ie: how do I do that commented line NB: I want to avoid doing hard coding of the checking for

Pytest: how to display generated report after test run?

老子叫甜甜 提交于 2019-12-23 03:25:20
问题 I am using pytest in combination with the pytest-html plugin which generates an HTML report after the test has run. I am using an auto-wired session fixture to automatically open the generated HTML report in a browser: @pytest.fixture(scope="session", autouse=True) def session_wrapper(request): print('Session wrapper init...') yield # open report in browser on Mac or Windows, skip headless boxes if platform.system() in ['Darwin', 'Windows']: html_report_path = os.path.join(request.config

Python 进阶之源码分析:如何将一个类方法变为多个方法?

[亡魂溺海] 提交于 2019-12-22 14:03:01
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 前一篇文章《 Python 中如何实现参数化测试? 》中,我提到了在 Python 中实现参数化测试的几个库,并留下一个问题: > 它们是如何做到把一个方法变成多个方法,并且将每个方法与相应的参数绑定起来的呢? 我们再提炼一下,原问题等于是: 在一个类中,如何使用装饰器把一个类方法变成多个类方法(或者产生类似的效果)? # 带有一个方法的测试类 class TestClass: def test_func(self): pass # 使用装饰器,生成多个类方法 class TestClass: def test_func1(self): pass def test_func2(self): pass def test_func3(self): pass Python 中装饰器的本质就是移花接木,用一个新的方法来替代被装饰的方法。在实现参数化的过程中,我们介绍过的几个库到底用了什么手段/秘密武器呢? 1、ddt 如何实现参数化? 先回顾一下上篇文章中 ddt 库的写法: import unittest from ddt import ddt,data,unpack @ddt class MyTest(unittest.TestCase): @data((3, 1), (-1, 0), (1.2, 1.0))

Python 3 modules and package relative import doesn't work?

℡╲_俬逩灬. 提交于 2019-12-22 11:27:03
问题 I have some difficulties constructing my project structure. This is my project directory structure : MusicDownloader/ __init__.py main.py util.py chart/ __init__.py chart_crawler.py test/ __init__.py test_chart_crawler.py These are codes : 1.main.py from chart.chart_crawler import MelonChartCrawler crawler = MelonChartCrawler() 2.test_chart_crawler.py from ..chart.chart_crawler import MelonChartCrawler def test_melon_chart_crawler(): crawler = MelonChartCrawler() 3.chart_crawler.py import sys

falcon, AttributeError: 'API' object has no attribute 'create'

僤鯓⒐⒋嵵緔 提交于 2019-12-22 11:06:03
问题 I'm trying test my falcon routes, but tests always failed, and looks like I make all things right. my app.py import falcon from resources.static import StaticResource api = falcon.API() api.add_route('/', StaticResource()) and my test directory tests/static.py from falcon import testing import pytest from app import api @pytest.fixture(scope='module') def client(): # Assume the hypothetical `myapp` package has a # function called `create()` to initialize and # return a `falcon.API` instance.

How to mock data as request.Response type in python

我的梦境 提交于 2019-12-22 09:47:12
问题 I would like to write some testcase to exercise object_check in isinstance(obj, requests.Response) logic. After I create Mock data as return value for requests.post. The type for mock data is always be Mock class. In that way, how can I rewrite mock data so mock data can be type of requests.Response? so I can exercise line d = obj.json() ? from unittest.mock import patch, Mock import unittest import requests from requests.exceptions import HTTPError import pytest def object_check(obj): if

How to get the total number of tests passed, failed and skipped from pytest

不羁的心 提交于 2019-12-22 09:47:04
问题 How can I get to the statistics information of a test session in pytest? I've tried to define pytest_sessionfinish in the conftest.py file, but I only see testsfailed and testscollected attributes on there. I also need to know the number of tests passed, skipped and the total time it took. Since pytest prints that info at the end of each session, I assume there's a programmatic way to retrieve that. 回答1: Use the pytest_terminal_summary hook. The stats are provided by the terminalreporter

Pytest Version Conflict Error

淺唱寂寞╮ 提交于 2019-12-22 09:19:33
问题 After update of Debian (Jessie), pytest doesn't work : #py.test Traceback (most recent call last): File "/usr/bin/py.test", line 5, in <module> sys.exit(load_entry_point('pytest==2.4.2', 'console_scripts', 'py.test')()) File "/usr/local/lib/python2.7/dist-packages/pkg_resources.py", line 348, in load_entry_point return get_distribution(dist).load_entry_point(group, name) File "/usr/local/lib/python2.7/dist-packages/pkg_resources.py", line 2311, in load_entry_point return ep.load() File "/usr

How to have pytest place memory limits on tests?

蹲街弑〆低调 提交于 2019-12-22 08:46:14
问题 I am using pytest, but I would like to have a decorator that could set a maximum memory usage per test. Similar to this question which was answered with, @pytest.mark.timeout(300) def test_foo(): pass I want, @pytest.mark.maxmem(300) def test_foo(): pass EDIT: I tried, >>> import os, psutil >>> import numpy as np >>> process = psutil.Process(os.getpid()) >>> process.memory_info().rss/1e9 0.01978368 >>> def f(): ... x = np.arange(int(1e9)) ... >>> process.memory_info().rss/1e9 0.01982464 >>> f

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

六眼飞鱼酱① 提交于 2019-12-22 08:35:18
问题 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