pytest

pytest - use funcargs inside setup_module

纵饮孤独 提交于 2019-12-04 12:06:43
I include at conftetst.py my own command line options def pytest_addoption(parser): parser.addoption("--backend" , default="test_backend", help="run testx for the given backend, default: test_backend") and def pytest_generate_tests(metafunc): if 'backend' in metafunc.funcargnames: if metafunc.config.option.backend: backend = metafunc.config.option.backend backend = backend.split(',') backend = map(lambda x: string.lower(x), backend) metafunc.parametrize("backend", backend) If I use this command line option inside a normal function inside a module: module: test_this.py; def test_me(backend):

Can I pass arguments to pytest fixtures?

假装没事ソ 提交于 2019-12-04 11:30:06
问题 The baseline of all my tests is that there will always be a taxi with at least one passenger in it. I can easily achieve this setup with some basic fixtures: from blah import Passenger, Taxi @pytest.fixture def passenger(): return Passenger() @pytest.fixture def taxi(passenger): return Taxi(rear_seat=passenger) Testing the baseline is straightforward: def test_taxi_contains_passenger(taxi) assert taxi.has_passenger() My issue crops up when I start needing more complicated test setup. There

Pytest执行用例报Hint: make sure your test modules/packages have valid Python names.

孤街浪徒 提交于 2019-12-04 11:20:59
近日,使用Pytest+Appium 实现APP端UI自动化,遇到Pytest收集用例失败的情况。 报错信息如下: test_room.py:None (test_room.py) ImportError while importing test module '/Users/XXXX/case/test_room.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: test_room.py:5: in <module> from Page.init_page import InitPage ../Page/init_page.py:8: in <module> from Page.home_page import HomePage ../Page/home_page.py:4: in <module> from Page.room_page import RoomPage ../Page/room_page.py:5: in <module> from Page.home_page import HomePage E ImportError: cannot import name 'HomePage' from 'Page.home_page' (/Users

py.test logging control

 ̄綄美尐妖づ 提交于 2019-12-04 08:51:47
问题 We have recently switched to py.test for python testing (which is fantastic btw). However, I'm trying to figure out how to control the log output (i.e. the built-in python logging module). We have pytest-capturelog installed and this works as expected and when we want to see logs we can pass --nologcapture option. However, how do you control the logging level (e.g. info, debug etc.) and also filter the logging (if you're only interested in a specific module). Is there existing plugins for py

11、pytest -- 测试的参数化

◇◆丶佛笑我妖孽 提交于 2019-12-04 08:26:41
目录 1. @pytest.mark.parametrize 标记 1.1. empty_parameter_set_mark 选项 1.2. 多个标记组合 1.3. 标记测试模块 2. pytest_generate_tests 钩子方法 往期索引: https://www.cnblogs.com/luizyao/p/11771740.html 在实际工作中,测试用例可能需要支持多种场景,我们可以把和场景强相关的部分抽象成参数,通过对参数的赋值来驱动用例的执行; 参数化的行为表现在不同的层级上: fixture 的参数化:参考 4、fixtures:明确的、模块化的和可扩展的 -- fixture 的参数化 ; 测试用例的参数化:使用 @pytest.mark.parametrize 可以在测试用例、测试类甚至测试模块中标记多个参数或 fixture 的组合; 另外,我们也可以通过 pytest_generate_tests 这个钩子方法自定义参数化的方案; 1. @pytest.mark.parametrize 标记 @pytest.mark.parametrize 的根本作用是在 收集 测试用例的过程中,通过对 指定参数 的赋值来新增被标记对象的 调用(执行) ; 首先,我们来看一下它在源码中的定义: # _pytest/python.py def parametrize

pytest running with another version of python

冷暖自知 提交于 2019-12-04 05:44:13
I've installed pyenv and have different versions of python installed with it: $ pyenv versions system 2.7.1 3.2.5 3.5.0 3.5.1 * 3.5.2 I use the following command to switch to python 3.5.2 : pyenv shell 3.5.2 And when I check the python version this is what I get: $ python --version Python 3.5.2 But when I run pytest , it still runs under python 2.7.6 : pytest -v ==================================================================== test session starts ==================================================================== platform linux2 -- Python 2.7.6, pytest-3.0.3, py-1.4.31, pluggy-0.4.0 --

How to run only unmarked tests in pytest

和自甴很熟 提交于 2019-12-04 05:28:45
I have several markers in my python test code: @pytest.mark.slowtest @pytest.mark.webtest @pytest.mark.stagingtest I am able to selectively run tests with a marker using for example pytest -m slowtest How can I run unmarked tests without resorting to pytest -m "not (slowtest or webtest or stagingtest)" ? As you can imagine, we might use other markers in the future... You can use following code snippet in top level conftest.py. def pytest_runtest_setup(item): envmarker = item.get_marker() if envmarker: pytest.skip("Skipping as the test has a marker") Remember that parametrize is also a marker.

Pytest Generate Tests Based on Arguments

别说谁变了你拦得住时间么 提交于 2019-12-04 04:45:00
New to pytest... I have the following in conftest.py to collect a team argument from the command line, and read in a yaml config file: import pytest import yaml def pytest_addoption(parser): parser.addoption( '--team', action='store', ) @pytest.fixture def team(request): return request.config.getoption('--team') @pytest.fixture def conf(request): with open('config.yml', 'r') as f: conf = yaml.load(f.read()) return conf I want to run a test on each player inside conf[team]['players'] (a list). I can do so as follows in test_players.py: def test_players(team, conf): players = conf[team]['players

How to I organize my pytest tests if I'm not distributing them with my package?

Deadly 提交于 2019-12-04 04:01:06
问题 As suggested in the pytest documentation, I have set up my package with the intent not not distributing my tests along with my package: setup.py mypkg/ __init__.py mypkg/ appmodule.py tests/ test_app.py ... but I am confused about how to ensure that these tests will run correctly when present (e.g. on Travis CI, or in clones of the project directory). I want the imports in my test scripts to apply to the source in the adjacent mypkg/ directory (and not to any mypkg that might be installed in

pytest: How to pass a class parameter to setup_class

和自甴很熟 提交于 2019-12-04 03:42:51
I am using pytest's parametrize annotations to pass params into a class. I'm able to use the parameters in the test methods, however, I can't figure out how to use the parameters in the setup_class method. import pytest params = ['A','B','C'] @pytest.mark.parametrize('n', params) class TestFoo: def setup_class(cls): print ("setup class:TestFoo") # Do some setup based on param def test_something(self, n): assert n != 'D' def test_something_else(self, n): assert n != 'D' I tried adding 'n' as a parameter like the test methods, like this: def setup_class(cls, n): print ("setup class:TestFoo") #