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
@inl-pd-autotest

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

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