Skip to content

Parametrize question: how to make 1 selenium-webdriver session per 1 test_*() function call irregardless of the number of parametrized sets of data? #2627

Closed

Description

@inl-pd-autotest

I have conftest.py with a class that starts a selenium webdriver function.
I have test_something.py module with test_a() and test_b() functions.
Each of the functions is parametrized (uses @pytest.mark.parametrize fixture), so that each of them runs twice with different sets of parameters.

Problem: I need to make 1 webdriver session match 2 parametrized calls for each test function separately. In other words: webdriver_session1: test_a(param1), test_a(param2); webdriver_session2: test_b(param1), test_b(param2).

If I create an instance of the class outside the test functions - then I get 1 webdriver session per MODULE (1 session per 2 functions, instead of 1 session per 1 function).

If I create an instance of the class inside the test functions - then I get 1 webdriver sessions per PARAMETRIZED CALL (2 sessions per 1 function, instead of 1 session per 1 function).

I thought that wrapping test functions into test_wrapper_a() and test_wrapper_b() would help, but this way parametrizing doesn't work.

pip list:

appdirs (1.4.3)
certifi (2017.4.17)
chardet (3.0.4)
EasyProcess (0.2.3)
idna (2.5)
lxml (3.8.0)
packaging (16.8)
pip (9.0.1)
py (1.4.33)
pyparsing (2.2.0)
pytest (3.0.7)
pytest-base-url (1.3.0)
pytest-html (1.14.2)
pytest-metadata (1.3.0)
pytest-selenium (1.9.1)
pytest-variables (1.6.0)
PyVirtualDisplay (0.2.1)
requests (2.18.1)
selenium (3.4.1)
setuptools (35.0.1)
six (1.10.0)
urllib3 (1.21.1)
wheel (0.29.0)

CentOS 7x64.

conftest.py:

class Driver():
    def __init__(self):
        self.wd = webdriver.Chrome()

    def __del__(self):
        self.wd.quit()

test_module_fail_1.py:

from conftest import Driver

@pytest.mark.parametrize('arg', [('set1'),('set2')])
def test_a(arg):
    o = Driver()    # I want 1 call of that per the whole function, not per 1 parametrized call of this function.
    assert True

@pytest.mark.parametrize('arg', [('set1'),('set2')])
def test_b(arg):
    o = Driver()    # I want 1 call of that per the whole function, not per 1 parametrized call of this function.
    assert True

test_module_fail_2.py:

from conftest import Driver

def test_a_wrapper():
    o = Driver()

    @pytest.mark.parametrize('arg', [('set1'),('set')])
    def test_a(arg):   # This way pytest doesn't call this function, even if I rename test_a_wrapper to a_wrapper.
        assert True

def test_b_wrapper():
    o = Driver()

    @pytest.mark.parametrize('arg', [('set1'),('set')])
    def test_b(arg):   # This way pytest doesn't call this function, even if I rename test_b_wrapper to b_wrapper.
        assert True

Activity

  1. RonnyPfannschmidt commented on Jul 28, 2017

    @RonnyPfannschmidt
    Member

    currently that is not supported, #2522 would enable it with a new scope

  2. inl-pd-autotest commented on Jul 29, 2017

    @inl-pd-autotest
    Author

    Thank you.
    I could replace the currently non-working parametrizing mechanism inside the wrapper function with a simple for loop that makes calls of the wrapped function as many times as I need, but all the calls would together count only as a single test. Could you provide some mechanism so that I could artificially mark successful tests finishes?

  3. nicoddemus commented on Sep 2, 2018

    @nicoddemus
    Member

    @inl-pd-autotest has this advanced or is still an issue?

  4. inl-pd-autotest commented on Sep 3, 2018

    @inl-pd-autotest
    Author

    @nicoddemus Well, I got no answer, so it is still an issue.

  5. RonnyPfannschmidt commented on Sep 3, 2018

    @RonnyPfannschmidt
    Member

    @inl-pd-autotest you could fake it by having a fixture that hold a browser and a key and at the lowert level request the browser with the "key" and drop it every time the key doesnt match

  6. inl-pd-autotest commented on Sep 3, 2018

    @inl-pd-autotest
    Author

    I just noticed that ticket #2522 referenced in the first comment to this ticket has now been resolved.
    Does it mean that what I initially wanted by this ticket is now possible to do via some new scope?
    How?

  7. RonnyPfannschmidt commented on Sep 3, 2018

    @RonnyPfannschmidt
    Member

    @inl-pd-autotest unfortunately its only half-fixed - we need a major release to fix the rest as far as i can tell (its not availiable as scope or in the node tree, so it cant help you with fixtures

  8. inl-pd-autotest commented on Sep 3, 2018

    @inl-pd-autotest
    Author

    @RonnyPfannschmidt not sure I understand: is it already fixed, but just not in a release yet, or is it only half-fixed and there are still things to fix first before a new release with a new scope could come out?
    If the latter is the case - could you, please, reference related tickets numbers?
    So that I'd know what to wait for.

  9. RonnyPfannschmidt commented on Sep 3, 2018

    @RonnyPfannschmidt
    Member

    its the later and actually good call out - i need to make an issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: questiongeneral question, might be closed after 2 weeks of inactivity

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions