pytest

Using Ansible variables in testinfra

梦想的初衷 提交于 2019-12-01 21:04:11
Using TestInfra with Ansible backend for testing purposes. Everything goes fine except using Ansible itself while running tests test.py import pytest def test_zabbix_agent_package(host): package = host.package("zabbix-agent") assert package.is_installed package_version = host.ansible("debug", "msg={{ zabbix_agent_version }}")["msg"] (...) where zabbix_agent_version is an Ansible variable from group_vars. It can be obtained by running this playbook - hosts: all become: true tasks: - name: debug debug: msg={{ zabbix_agent_version }} command executing tests pytest --connection=ansible --ansible

Python test fixture to run a single test?

你说的曾经没有我的故事 提交于 2019-12-01 19:57:48
问题 I'm looking for something like ruby rspec's focus metadata or elixir's mix tags to run a single python test. Ruby RSpec Example: # $ rspec spec it 'runs a single test', :focus do expect(2).to eq(2) end Elixir ExUnit & Mix Example: # $ mix test --only focus @tag :focus test "only run this test" do assert true end Is this possible / available with any python test runner and fixture combo? Running single tests by specifying nested module.class.test_name via command line args can become very

How to test a class' inherited methods in pytest

空扰寡人 提交于 2019-12-01 19:28:09
house.py : class House: def is_habitable(self): return True def is_on_the_ground(self): return True conftest.py : import pytest from house import House @pytest.fixture(scope='class') def house(): return House() test_house.py : class TestHouse: def test_habitability(self, house): assert house.is_habitable() def test_groundedness(self, house): assert house.is_on_the_ground() Up to that point, everything is being tested. Now I add a subclass and override a method in house.py : class House: def is_habitable(self): return True def is_on_the_ground(self): return True class TreeHouse(House): def is

Python test fixture to run a single test?

孤街醉人 提交于 2019-12-01 19:07:14
I'm looking for something like ruby rspec's focus metadata or elixir's mix tags to run a single python test. Ruby RSpec Example: # $ rspec spec it 'runs a single test', :focus do expect(2).to eq(2) end Elixir ExUnit & Mix Example: # $ mix test --only focus @tag :focus test "only run this test" do assert true end Is this possible / available with any python test runner and fixture combo? Running single tests by specifying nested module.class.test_name via command line args can become very verbose in larger projects. So Something like: Desired Python Code: # $ nosetests --only focus from tests

Pytest - custom output of test results

你说的曾经没有我的故事 提交于 2019-12-01 18:02:11
I want to completely custom tests results output. In unittest I can implement my own test runner by example of unittest.TextTestRunner . How can I do the same in pytest ? You'll need to work with pytest hooks : py.test calls hook functions to implement initialization, running, test execution and reporting. Also see: py.test code snippets pycon tutorial session (it also goes through using hooks) 来源: https://stackoverflow.com/questions/18496496/pytest-custom-output-of-test-results

Pytest - custom output of test results

回眸只為那壹抹淺笑 提交于 2019-12-01 17:28:02
问题 I want to completely custom tests results output. In unittest I can implement my own test runner by example of unittest.TextTestRunner . How can I do the same in pytest ? 回答1: You'll need to work with pytest hooks: py.test calls hook functions to implement initialization, running, test execution and reporting. Also see: py.test code snippets pycon tutorial session (it also goes through using hooks) 来源: https://stackoverflow.com/questions/18496496/pytest-custom-output-of-test-results

Pytest从测试类外为测试用例动态注入数据

半世苍凉 提交于 2019-12-01 17:04:09
今天Nelly问我Pytest能不能支持从TestClass类外传入参数?从类外批量传入各个test方法需要的参数。因为数据文件可能有很多情况,不方便依次匹配。 然而又必须用类对用例进行归类及复用,数据要通过类外进行遍历。不能直接使用pytest.mark.parametrize。 这里采取的一个做法是: 添加命令行选项 --data,接受一个yaml文件 data这个fixture方法里,获取--data传进来的文件路径,打开并加载所有数据,从request中获取调用data 的用例名,从所有数据中取出该条用例的数据返回 具体参考以下代码: data.yaml文件内容,注意数据字段要与测试方法名一致,方便自动对应数据。 test_a: a: 1 b: 2 test_b: a: 3 b: 4 conftest.py文件内容 import pytest import yaml def pytest_addoption(parser): # 添加运行参数 parser.addoption("--data", action="store", help="data file") @pytest.fixture def data(request): file_path = request.config.getoption("--data") # 获取--data参数传的文件路径 with

Pytest use same fixture twice in one function

非 Y 不嫁゛ 提交于 2019-12-01 15:07:26
For my web server, I have a login fixture that create a user and returns the headers needed to send requests. For a certain test, I need two users. How can I use the same fixture twice in one function? from test.fixtures import login class TestGroups(object): def test_get_own_only(self, login, login): pass An alternative is just to copy the fixture function. This is both simple and correctly handles parameterized fixtures, calling the test function with all combinations of parameters for both fixtures. This example code below raises 9 assertions: import pytest @pytest.fixture(params=[0, 1, 2])

Pytest use same fixture twice in one function

China☆狼群 提交于 2019-12-01 14:40:10
问题 For my web server, I have a login fixture that create a user and returns the headers needed to send requests. For a certain test, I need two users. How can I use the same fixture twice in one function? from test.fixtures import login class TestGroups(object): def test_get_own_only(self, login, login): pass 回答1: An alternative is just to copy the fixture function. This is both simple and correctly handles parameterized fixtures, calling the test function with all combinations of parameters for

pytest 运行文件报错

▼魔方 西西 提交于 2019-12-01 13:58:05
用pytest 执行时报错: Traceback (most recent call last): File "D:\program files\JetBrains\PyCharm Community Edition 2019.2.3\helpers\pycharm\_jb_pytest_runner.py", line 37, in <module> config_result = real_prepare_config(args, plugins_to_load) File "F:\Users\Administrator\PycharmProjects\hogwars10\venv\lib\site-packages\_pytest\config\__init__.py", line 221, in _prepareconfig pluginmanager=pluginmanager, args=args File "F:\Users\Administrator\PycharmProjects\hogwars10\venv\lib\site-packages\pluggy\hooks.py", line 286, in __call__ return self._hookexec(self, self.get_hookimpls(), kwargs) File "F: