Skip to content

Collect test with custom registered markers produces wrong test collection #4377

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

Closed
KursLabIgor opened this issue Nov 12, 2018 · 2 comments
Closed
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@KursLabIgor
Copy link

Need help.
I'm using pytest==3.6.1 and pythom 2.7.15.
According to docs if i want make custom test collection I need to use something like:

Doc example

import pytest
def pytest_addoption(parser):
    parser.addoption("-E", action="store", metavar="NAME",
        help="only run tests matching the environment NAME.")

def pytest_configure(config):
    # register an additional marker
    config.addinivalue_line("markers",
        "env(name): mark test to run only on named environment")

def pytest_runtest_setup(item):
    envnames = [mark.args[0] for mark in item.iter_markers(name='env')]
    if envnames:
        if item.config.getoption("-E") not in envnames:
            pytest.skip("test requires env in %r" % envnames)

My implementation

def pytest_addoption(parser):
    parser.addoption("--category", action="store", default="debug", help="test category for run.")

def pytest_runtest_setup(item):
    envnames = [mark.args[0] for mark in item.iter_markers(name='category')]
    if envnames:
        if item.config.getoption("--category") not in envnames:
            pytest.skip("test requires env in %r" % envnames)

def pytest_configure(config):
    """
    Add to report header necessary info like Browser type and test category which used
    :param config: default pytest param. do not change
    """
    # register an additional marker
    config.addinivalue_line("markers",
                            "category(name): mark test to run only on specific category name")
    config._metadata['TestCategory/Marker'] = config.getoption('--category')
    config._metadata['URL'] = config.getoption('--url')
# Tests
@pytest.mark.category("e2e_smoke")
@pytest.mark.category("e2e_fe")
class TestKBFeature(BaseTest):
    @pytest.allure.feature('Check knowledge base widget on the homepage')
    def test_kb_widget_11(self, session):
        pass
    def test_kb_open_category_from_widget_11(self, data_kb_articles, session):
        pass
   def test_kb_left_navigation_11(self, session): 
        pass
   def test_kb_pagination_12c(self):
        pass
   def test_kb_rate_article_12a(self, session):
        pass

In case if I run collect test only it will produce following result:

pytest --category=debug -v tests/kb_test.py --collect-only
image

My expected result should be: 0 collected test.
How to make that test collects only according to specified mark.

@blueyed
Copy link
Contributor

blueyed commented Nov 13, 2018

Just for completeness: please try with 3.10.1.

But in general your tests are only skipped during runtime, not collection.

You should use another hook maybe, e.g.

@pytest.hookimpl(hookwrapper=True)
def pytest_collection_modifyitems(config, items):
    yield

    only_using_fixture = config.getoption('--only-using-fixture')
    if only_using_fixture:
        new_items = []
        for item in items:
            if only_using_fixture in item.funcargnames:
                new_items.append(item)
        items[:] = new_items

@blueyed blueyed added the type: question general question, might be closed after 2 weeks of inactivity label Nov 13, 2018
@KursLabIgor
Copy link
Author

@blueyed thanks, you idea works, thanks. can be closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

2 participants