pytest

Grouping Parametrized Benchmarks with pytest

帅比萌擦擦* 提交于 2021-01-29 20:36:40
问题 I'm currently benchmarking an implementation of an AVL Tree I made against a non-rebalancing binary search tree using pytest-benchmark. It seems to be working well for me so far but I've run into an issue. In order to consolidate the test file, I found that I can parametrize tests and also group the output of the benchmark for readability but I can't seem to do them both at the same time. My current insertion benchmark: # always the same for repeatability random.seed(0x1C2C6D66) def

pytest.main() capture standard output

自作多情 提交于 2021-01-29 18:44:02
问题 I have a method I am using to get all of the tests we have. def get_test_names_from_file(): get_test_names = pytest.main(['--collect-only', '-q']) print(type(get_test_names)) return 'here is the methods return: ' + str(get_test_names) When I call this method it returns an exist code here is the methods return: 0 and that's fine. What I can not figure out is how I get the resulting standard out into a format that I can use. Here is the standard out when the method is called: test_a.py::TestA:

pytest module not found

谁都会走 提交于 2021-01-29 18:00:28
问题 I'm having trouble running pytest on my package. I have the following structure... tree -f . ├── [ 0 Jan 4 22:04] ./__init__.py ├── [ 34K Jan 12 11:37] ./LICENSE ├── [1.4K Jan 6 07:43] ./README.md ├── [1.1K Jan 12 11:42] ./setup.cfg ├── [ 79 Jan 6 08:22] ./setup.py ├── [4.0K Jan 12 11:49] ./tcx2gpx │ ├── [ 701 Jan 6 23:13] ./tcx2gpx/__init__.py │ ├── [4.0K Jan 12 12:13] ./tcx2gpx/__pycache__ │ │ ├── [ 683 Jan 12 12:13] ./tcx2gpx/__pycache__/__init__.cpython-36.pyc │ │ └── [3.0K Jan 6 21:00] .

Pytest - How to Parameterize tests with multiple scenarios?

倾然丶 夕夏残阳落幕 提交于 2021-01-29 17:55:28
问题 I'm using pytest ,boto3 and aws and want to have dynamic assertions with parameterized tests. How to improve this code to only assert on a specific group of subnetids? production_private_ids = ["subnet-08f6d70b65b5cxx38", "subnet-0b6aaaf1ce207xx03", "subnet-0e54fda8f811fxxd8"]) .... nonproduction_private_ids = ["subnet-11f6xx0b65b5cxx38", "subnet-116aaaf1ce207xx99", "subnet-11xxfda8f811fxx77"]) .... @pytest.mark.parametrize("subnet", ["production_private_ids", "nonproduction_private_ids",

pydev coverage: pytest and local pytest plugin running in two separate processes, talking over http

坚强是说给别人听的谎言 提交于 2021-01-29 17:40:40
问题 All started here: https://github.com/pytest-dev/pytest-cov/issues/425 I can say by now that I have a working solution on my terminal, so when I run: pytest --cov views --cov db --cov-report term-missing:skip-covered -sv ==================== test session starts ==================== platform darwin -- Python 3.7.6, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 -- /usr/local/Caskroom/miniconda/base/bin/python cachedir: .pytest_cache rootdir: /Users/alan/myproject plugins: xdist-1.33.0, forked-1.2.0, cov

Getting Pytest, Relative Import, and Patch.Object to Cooperate

荒凉一梦 提交于 2021-01-29 16:29:48
问题 I'm having a hell of a time getting pytest, relative import, and patch to cooperate. I have the following: from .. import cleanup class CleanupTest(unittest.TestCase): @patch.object(cleanup, 'get_s3_url_components', MagicMock()) @patch.object(cleanup, 'get_db_session', MagicMock(return_value={'bucket', 'key'})) @patch('time.time') def test_cleanup(self, mock_time, mock_session, mock_components): ... I've tried a few variations. The currently shown one. Pytest returns 'TypeError: test_cleanup(

pytest失败重跑插件: pytest-rerunfailures使用与坑(全网独家精华)

泪湿孤枕 提交于 2021-01-29 15:42:09
背景 在编写接口case的时候,我们常遇到一个这样的问题: 测试环境不稳定偶发接口超时(和服务无关,纯粹是环境问题),然后执行接口case也因此偶发失败。比如同一个接口case跑五次,其中有两次失败,另外三次都是成功的,这种偶发性的环境问题就需要我们手动重跑(还不一定能够通过)。有没有一个比较好的机制,保证case能够尽最大努力通过测试呢? 这里我们介绍pytest的一个失败重跑插件: pytest-rerunfailures 介绍 pytest-rerunfailures 是一个通过重跑机制来消除不稳定失败的pytest插件。 项目地址:https://github.com/pytest-dev/pytest-rerunfailures 安装 安装&运行要求: Python 3.6~3.9, or PyPy3 pytest 5.0+ 安装插件 sudo pip(pip3) install pytest-rerunfailures 使用pytest-rerunfailures 使用方式有两种: 命令行参数 装饰器方式 命令行参数模式 示例case:test_demo.py #!/usr/bin/env python3#!coding:utf-8import pytestimport randomdef test_simple_assume(): #每次case运行的值为1或者2

PyTest teardown_class is being run too soon

☆樱花仙子☆ 提交于 2021-01-29 14:23:57
问题 The Python "teardown_class" is not behaving as I expect it to. Below is a summary of my code: @classmethod def setup_class(cls): cls.create_table(table1) cls.create_table(table2) cls.create_table(table3) @classmethod def create_table(cls, some_arg_here): """Some code here that creates the table""" def test_foo(self): """Some test code here""" @classmethod def teardown_class(cls): """Perform teardown things""" I believe the way it is executing is that: create_table is being called from setup

PyTest: Use results from previous test to parametrize next tests

老子叫甜甜 提交于 2021-01-29 08:50:25
问题 I'm using PyTest and Selenium to test all options listed in a dropdown menu on a website. I want to navigate to that page, grab all the available options in the dropdown, and then use parametrize to create new sub-tests to click each option and test UI elements. It looks like this: class TestOptions: dropdown_options = [] def test_login(self): ... def test_navigate_to_page(self): ... def test_grab_all_options(self): self.dropdown_options = get_all_dropdown_option_names() # test that all

List element (object) not subscriptable

依然范特西╮ 提交于 2021-01-29 08:36:59
问题 I want to create a unit test for a function which sorts a list of objects according to some object attribute(s). Here is a code sample: class Foo: """Custom data type Foo.""" def __init__(self, a: str, b: int, c: int, d: int, e: int, f: int): self.a = a self.b = b self.c = c self.d = d self.e = e self.f = f self.g = c * d * e self.h = c * d This is a part of the unit test function which produces the error: from random import randint ... class Test(TestCase): def test_item_sort_foo(self):