Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to dynamically modify all ini-values from plugins #204

Closed
pytestbot opened this issue Oct 20, 2012 · 3 comments
Closed

allow to dynamically modify all ini-values from plugins #204

pytestbot opened this issue Oct 20, 2012 · 3 comments
Labels
type: enhancement new feature or API change, should be merged into features branch

Comments

@pytestbot
Copy link
Contributor

Originally reported by: Alexander Schepanovski (BitBucket: suor, GitHub: suor)


Here is my conftest.py

#!python
def pytest_addoption(parser):
    parser.addoption('--bench',
           action="store_true", dest="bench", default=False,
           help=("Enable benchmark mode"))

def pytest_configure(config):
    if config.getvalue("bench"):
        config.addinivalue_line("python_files", "bench_*.py") # Problem here

and it raises AssertError.


@pytestbot
Copy link
Contributor Author

Original comment by Alexander Schepanovski (BitBucket: suor, GitHub: suor):


Same with python_functions

@pytestbot
Copy link
Contributor Author

Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


The config.addinivalue_line is meant to add to linelists, not args.
For example, "markers" is a linelist, whereas python_files/functions
is of type "args", thus returns a tuple. Modifying should be generalized -
in the context of dynamically setting any ini-file value, though.

In the meanwhile here is a workaround. If you use this conftest.py::

# content of conftest.py

def pytest_addoption(parser):
    parser.addoption('--bench',
           action="store_true", dest="bench", default=False,
           help=("Enable benchmark mode"))

def addini_arg(config, name, arg):
    args = config.getini(name)
    args = tuple(args) + (arg,)
    # hack it back to the config ini-value cache
    config._inicache[name] = args
    assert config.getini(name) == args

def pytest_configure(config):
    if config.getvalue("bench"):
        addini_arg(config, "python_files", "bench_*.py")
        addini_arg(config, "python_functions", "bench_")

and add a test like this::

# content of bench_something.py

def bench_1():
    pass

you can run it as expected::

$ py.test --bench
=========================== test session starts ============================
platform linux2 -- Python 2.7.3 -- pytest-2.3.1
plugins: xdist, bugzilla, cache, cli, pep8, timeout, oejskit, cov
collected 1 items
bench_something.py .

========================= 1 passed in 0.02 seconds ========================

@pytestbot pytestbot added the type: enhancement new feature or API change, should be merged into features branch label Jun 15, 2015
@nicoddemus
Copy link
Member

This works for me on current master:

def pytest_addoption(parser):
    parser.addoption('--bench',
           action="store_true", dest="bench", default=False,
           help=("Enable benchmark mode"))

def pytest_configure(config):
    if config.getvalue("bench"):
        config.addinivalue_line("python_files", "bench_*.py")
        config.addinivalue_line("python_functions", "bench_")
============================= test session starts =============================
platform win32 -- Python 3.4.3, pytest-2.8.0.dev4, py-1.4.30, pluggy-0.3.0
rootdir: X:\temp\issues-session, inifile:
collected 1 items

bench_something.py .

========================== 1 passed in 0.01 seconds ===========================

So I'm closing this for now. 😄

cc @Suor, @hpk42

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement new feature or API change, should be merged into features branch
Projects
None yet
Development

No branches or pull requests

2 participants