pytest

Ensuring py.test includes the application directory in sys.path

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 09:58:39
问题 I have a project directory structure as follows (which I think is pretty standard): my_project setup.py mypkg __init__.py foo.py tests functional test_f1.py unit test_u1.py I'm using py.test for my testing framework, and I'd expect to be able to run py.test tests when in the my_project directory to run my tests. This does indeed work, until I try to import my application code using (for example) import mypkg in a test. At that point, I get the error "No module named mypkg". On doing a bit of

Is there a way to specify which pytest tests to run from a file?

[亡魂溺海] 提交于 2019-11-27 09:30:33
问题 Is there a way to select pytest tests to run from a file? For example, a file foo.txt containing a list of tests to be executed: tests_directory/foo.py::test_001 tests_directory/bar.py::test_some_other_test Or is there a way to select multiple tests, having no common pattern in test name, from different directories with pytest? py.test -k <pattern> allows a single pattern. One option is to have a pytest.mark against each test, but my requirement is to run different combination of tests from

How do I configure PyCharm to run py.test tests?

风格不统一 提交于 2019-11-27 09:18:51
问题 I want to start writing unit tests for my Python code, and the py.test framework sounds like a better bet than Python's bundled unittest. So I added a "tests" directory to my project, and added test_sample.py to it. Now I want to configure PyCharm to run all the tests in my "tests" directory. PyCharm allegedly supports py.test in its test runner. You're supposed to be able to create a run/debug configuration to run your tests, and PyCharm allegedly has a "create configuration" dialog box

how to share a variable across modules for all tests in py.test

别来无恙 提交于 2019-11-27 09:12:11
I have multiple tests run by py.test that are located in multiple classes in multiple files. What is the simplest way to share a large dictionary - which I do not want to duplicate - with every method of every class in every file to be used by py.test? In short, I need to make a "global variable" for every test. Outside of py.test, I have no use for this variable, so I don't want to store it in the files being tested. I made frequent use of py.test's fixtures, but this seems overkill for this need. Maybe it's the only way? flub Update: pytest-namespace hook is deprecated/removed . Do not use.

pytest相关资源收集

限于喜欢 提交于 2019-11-27 07:57:29
pytest官网 https://docs.pytest.org/en/latest/getting-started.html 官网推荐的plugin https://docs.pytest.org/en/latest/plugins.html#using-plugins allure对应pytest的使用方法 https://docs.qameta.io/allure/#_pytest Faker https://faker.readthedocs.io/en/stable/index.html Factory-boy https://factoryboy.readthedocs.io/en/latest/index.html 来源: https://www.cnblogs.com/moonpool/p/11352398.html

pytest fixture 参数化

馋奶兔 提交于 2019-11-27 07:49:35
@pytest.fixture有一个params参数,接受一个列表,列表中每个数据都可以作为用例的输入。也就说有多少数据,就会形成多少用例。如下面例子,就形成3条用例 test_parametrizing.py 1 import pytest 2 3 seq=["case1","case2","case3"] 4 5 6 @pytest.fixture(scope="module",params=seq) 7 def params(request): 8 return request.param 9 10 11 12 def test_params(params): 13 print(params) 14 assert "case" in params 执行命令: pytest -rA test_parametrizing.py 执行结果: 来源: https://www.cnblogs.com/moonpool/p/11351859.html

pytest fixture中scope试验,包含function、module、class、session、package

前提是你 提交于 2019-11-27 07:28:29
上图是试验的目录结构 conftest.py:存放pytest fixture的文件 1 import uuid 2 import pytest 3 4 @pytest.fixture(scope="module") 5 def test_module(): 6 return "module"+str(uuid.uuid4()) 7 8 @pytest.fixture(scope="class") 9 def test_class(): 10 return "class"+str(uuid.uuid4()) 11 12 @pytest.fixture(scope="function") 13 def test_function(): 14 return "function"+str(uuid.uuid4()) 15 16 @pytest.fixture(scope="session") 17 def test_session(): 18 return "session"+str(uuid.uuid4()) 19 20 21 @pytest.fixture(scope="package") 22 def test_package(): 23 return "package"+str(uuid.uuid4()) test_class.py:类测试文件 1 class Test_for

py.test skips test class if constructor is defined

夙愿已清 提交于 2019-11-27 06:33:32
问题 I have following unittest code running via py.test. Mere presence of the constructor make the entire class skip when running py.test -v -s collected 0 items / 1 skipped Can anyone please explain to me this behaviour of py.test? I am interested in understanding py.test behaviour, I know the constructor is not needed. Thanks, Zdenek class TestClassName(object): def __init__(self): pass def setup_method(self, method): print "setup_method called" def teardown_method(self, method): print "teardown

Pass a parameter to a fixture function

坚强是说给别人听的谎言 提交于 2019-11-27 06:06:23
I am using py.test to test some DLL code wrapped in a python class MyTester. For validating purpose I need to log some test data during the tests and do more processing afterwards. As I have many test_... files I want to reuse the tester object creation (instance of MyTester) for most of my tests. As the tester object is the one which got the references to the DLL's variables and functions I need to pass a list of the DLL's variables to the tester object for each of the test files (variables to be logged are the same for a test_... file). The content of the list is shall be used to log the

Test case execution order in pytest

↘锁芯ラ 提交于 2019-11-27 05:14:15
I am using pytest. I have two files in a directory. In one of the files there is a long running test case that generates some output. In the other file there is a test case that reads that output. How can I ensure the proper execution order of the two test cases? Is there any alternative other than puting the test cases in the same file in the proper order? Frank T In general you can configure the behavior of basically any part of pytest using its well-specified hooks . In your case, you want the "pytest_collection_modifyitems" hook, which lets you re-order collected tests in place. That said,