pytest

Importing modules from a sibling directory for use with py.test

ぃ、小莉子 提交于 2019-12-05 16:22:58
I am having problems importing anything into my testing files that I intend to run with py.test. I have a project structure as follows: /ProjectName | |-- /Title | |-- file1.py | |-- file2.py | |-- file3.py | |-- __init__.py | |-- /test | |-- test_file1.py I have not been able to get any import statements working with pytest inside the test_file1.py file, and so am currently just attempting to use a variable declared in file_1.py and print it out when test_file1.py is run. file1.py contains: file1_variable = "Hello" test_file1.py contains: import sys import os sys.path.append(os.path.abspath('

Dynamically control order of tests with pytest

China☆狼群 提交于 2019-12-05 15:56:51
I would like to control the order of my tests using logic which will reorder them on the fly, while they are already running. My use case is this: I am parallelizing my tests with xdist, and each test uses external resources from a common and limited pool. Some tests use more resources than others, so at any given time when only a fraction of the resources are available, some of the tests have the resources they need to run and others don't. I want to optimize the usage of the resources, so I would like to dynamically choose which test will run next, based on the resources currently available.

使用pytest————持续更新中

回眸只為那壹抹淺笑 提交于 2019-12-05 15:50:11
pytest 1,最简单的示例 import pytest def test_case_01(): print("执行test01") assert 1 # 断言成功 def test_case_02(): print("执行test02") assert 0 # 断言失败 if __name__ == '__main__': pytest.main(['test_01.py']) 运行结果如下: pytest 1,最简单的示例 import pytest def test_case_01(): print("执行test01") assert 1 # 断言成功 def test_case_02(): print("执行test02") assert 0 # 断言失败 if __name__ == '__main__': pytest.main(['test_01.py']) 运行结果如下: 如图所示: 在执行完成之后先会显示test01.py,后面跟着.F 其中.代表执行成功,F代表执行失败,并且在下方会展示错误的提示 2,pytest使用步骤: 1,导入pytest 2,编写测试用例 一,无需在测试类下编写测试用例,可以直接编写测试函数 二,测试函数名必须包含test_ 开头,或者_test结尾; 3,在pytest框架下执行测试用例 在py文件内执行测试用例: pytest

Force py.test to use installed version of module

倾然丶 夕夏残阳落幕 提交于 2019-12-05 15:10:08
I have a mixed Python/C++ library with test files mixed in amongst source files in the same directories. The layout looks like /home/irving/geode geode __init__.py vector __init__.py test_vector.py ... ... Unfortunately, the library is unusable in-place since it lacks .so extension modules. Question : Can I make py.test always use an installed version, even when run from /home/irving/geode or a subdirectory? The test files have from __future__ import absolute_import , and run fine if executed directly as scripts. For example, if I do cd geode/vector ./test_vector.py which does import geode ,

How to have pytest place memory limits on tests?

社会主义新天地 提交于 2019-12-05 14:38:45
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() >>> process.memory_info().rss/1e9 0.019832832 Which doesn't catch the memory allocation in the

How to get a list of TestReports at the end of a py.test run?

亡梦爱人 提交于 2019-12-05 12:58:11
I want to get a list of all tests (e.g. in the form of a py.test TestReport) at the end of all tests. I know that pytest_runtest_makereport does something similar, but only for a single test. But I want to implement a hook or something in conftest.py to process the whole list of tests before the py.test application terminates. Is there a way to do this? Here an example which can help you. Structure of files: /example: __init__.py # empty file /test_pack_1 __init__.py # empty file conftest.py # pytest hooks test_my.py # a few tests for demonstration There are 2 tests in test_my.py : def test

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

大城市里の小女人 提交于 2019-12-05 12:05:44
问题 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(

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

烂漫一生 提交于 2019-12-05 09:27:19
问题 Is it possible to run py.test with different versions of python without plugins (like xdist ) or tox ? 回答1: 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. 回答2: You can create a standalone pytest script with py.test --genscript=mypytest and then do pythonXY

py.test can't import my module

帅比萌擦擦* 提交于 2019-12-05 09:09:06
I am struggeling getting a python import right. What I want to achieve is to have a module with several source files and a test folder with unit tests. No matter what I do, I can't get py.test-3 to execute my tests. My directory layout looks like this: . ├── module │ ├── __init__.py │ └── testclass.py └── tests └── test_testclass.py The __init__.py file looks like this: __all__ = ['testclass'] The testclass.py file looks like this: class TestClass(object): def __init__(self): self.id = 1 And my unit test like this: import pytest from module import TestClass def test_test_class(): tc =

py.test: error: unrecognized arguments: --cov=ner_brands --cov-report=term-missing --cov-config

╄→尐↘猪︶ㄣ 提交于 2019-12-05 08:20:50
问题 when I am trying to run my test through command line py.test file_name.py I got this error: py.test: error: unrecognized arguments: --cov=ner_brands --cov-report=term-missing --cov-config How can I fix this? 回答1: pytest-cov package is required if you want to pass --cov arguments to pytest, by default it should not be passed though. Are you using a modified version of py.test? pip install pytest-cov would fix your issue. 回答2: For those who use CentOS 6, the version of setuptools is old and you