-1

While my practice I try to build my hybrid test framework. Here is link to GitHub repository of My framework

Its structure is

...root/
   ...Base/
   ...Locators/
   ...Pages/
   ...Tests/
   ...conftest.py
   ...pytest.ini

I would like to make possibility to pass configurations from terminal command or from conftest.py file But each time I run command:

pytest search_test.py --browser chrome --server server_ip/wd/hub

I get error:

ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]    
pytest: error:  unrecognized arguments: --browser chrome --server server_ip/wd/hub/wd/hub
inifile: path to inifile
rootdir: path to root directory

here is content of my conftest.py file:

import pytest
import configparser
from selenium import webdriver


def environment_options(parser):
    parser.addoption('--browser', '-B', dest='BROWSER', choises=['chrome', 'chr', 'firefox', 'ff'],
                     help=f"possible values are: {['chrome', 'chr', 'firefox', 'ff']}")

    parser.addoption('--server', '-S', dest="SERVER")


@pytest.fixture(scope='class')
def environment_configuration(request):
    read_config = configparser.ConfigParser()
    # checking if browser was input from console or from config file from section Environment and assigment
    # it to browser_name variable

    browser_name = request.config.getoption(
        "BROWSER") or read_config.get("Environments", "browser")

    # checking if remote server was input from console or from config file from section Environment and assigment
    # it to remote_server variable
    remote_server = request.config.getoption(
        "SERVER") or read_config.get("Environments", "remote_server")

    try:
        request.cls.driver = webdriver.Remote(
            command_executor=remote_server,
            desired_capabilities={
                "browserName": browser_name})
    except BaseException:
        print("check browser or remote server configs")

    yield request.cls.driver

    request.cls.driver.close()
    request.cls.driver.quit()
Akop Akopov
  • 85
  • 4
  • 13
  • Have you checked out [this](https://stackoverflow.com/a/42145604/15282257) solution? – Paul P May 27 '21 at 19:00
  • Hi, yes i have seen this post previously. And I have read a lot of posts about metafunc. but to me it is not quite clear when it is better to use metafunc or request. If i understand correct pytest docs metafunc.config.option.some_option and request.config.getoption("some_option") are equal – Akop Akopov May 27 '21 at 19:19
  • @p3j4p5 already checked - the same error – Akop Akopov May 27 '21 at 20:06

1 Answers1

-1

need to change name of function from environment_options to pytest_addoption and everything starts to work :-)

Akop Akopov
  • 85
  • 4
  • 13