Python pytest.config() Examples

The following are 21 code examples for showing how to use pytest.config(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module pytest , or try the search function .

Example 1
Project: pytorch-lr-finder   Author: davidtvs   File: conftest.py    License: MIT License 6 votes vote down vote up
def pytest_configure(config):
    # Bind a config object to `pytest` module instance
    pytest.custom_cmdopt = CustomCommandLineOption()
    pytest.custom_cmdopt.add("cpu_only", config.getoption("--cpu_only"))

    # Set the random seed so that the tests are reproducible between test runs and
    # hopefully torch and numpy versions. This seed should also allow all range tests
    # with a starting lr of 1e-5 and an ending lr of 1e-1 to run the full test without
    # diverging
    seed = 1
    random.seed(seed)
    os.environ["PYTHONHASHSEED"] = str(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.backends.cudnn.deterministic = True 
Example 2
Project: ocs-ci   Author: red-hat-storage   File: ocscilib.py    License: MIT License 6 votes vote down vote up
def get_cli_param(config, name_of_param, default=None):
    """
    This is helper function which store cli parameter in RUN section in
    cli_params

    Args:
        config (pytest.config): Pytest config object
        name_of_param (str): cli parameter name
        default (any): default value of parameter (default: None)

    Returns:
        any: value of cli parameter or default value

    """
    cli_param = config.getoption(name_of_param, default=default)
    ocsci_config.RUN['cli_params'][name_of_param] = cli_param
    return cli_param 
Example 3
Project: ocs-ci   Author: red-hat-storage   File: ocscilib.py    License: MIT License 6 votes vote down vote up
def pytest_collection_modifyitems(session, config, items):
    """
    Add Polarion ID property to test cases that are marked with one.
    """
    for item in items:
        try:
            marker = item.get_closest_marker(name="polarion_id")
            if marker:
                polarion_id = marker.args[0]
                if polarion_id:
                    item.user_properties.append(
                        ("polarion-testcase-id", polarion_id)
                    )
        except IndexError:
            log.warning(
                f"polarion_id marker found with no value for "
                f"{item.name} in {item.fspath}",
                exc_info=True
            ) 
Example 4
Project: cassandra-dtest   Author: apache   File: conftest.py    License: Apache License 2.0 5 votes vote down vote up
def check_required_loopback_interfaces_available():
    """
    We need at least 3 loopback interfaces configured to run almost all dtests. On Linux, loopback
    interfaces are automatically created as they are used, but on Mac they need to be explicitly
    created. Check if we're running on Mac (Darwin), and if so check we have at least 3 loopback
    interfaces available, otherwise bail out so we don't run the tests in a known bad config and
    give the user some helpful advice on how to get their machine into a good known config
    """
    if platform.system() == "Darwin":
        if len(ni.ifaddresses('lo0')[AF_INET]) < 9:
            pytest.exit("At least 9 loopback interfaces are required to run dtests. "
                            "On Mac you can create the required loopback interfaces by running "
                            "'for i in {1..9}; do sudo ifconfig lo0 alias 127.0.0.$i up; done;'") 
Example 5
Project: cassandra-dtest   Author: apache   File: conftest.py    License: Apache License 2.0 5 votes vote down vote up
def log_global_env_facts(fixture_dtest_config, fixture_logging_setup):
    if pytest.config.pluginmanager.hasplugin('junitxml'):
        my_junit = getattr(pytest.config, '_xml', None)
        my_junit.add_global_property('USE_VNODES', fixture_dtest_config.use_vnodes) 
Example 6
Project: cassandra-dtest   Author: apache   File: conftest.py    License: Apache License 2.0 5 votes vote down vote up
def fixture_maybe_skip_tests_requiring_novnodes(request):
    """
    Fixture run before the start of every test function that checks if the test is marked with
    the no_vnodes annotation but the tests were started with a configuration that
    has vnodes enabled. This should always be a no-op as we explicitly deselect tests
    in pytest_collection_modifyitems that match this configuration -- but this is explicit :)
    """
    if request.node.get_closest_marker('no_vnodes'):
        if request.config.getoption("--use-vnodes"):
            pytest.skip("Skipping test marked with no_vnodes as tests executed with vnodes enabled via the "
                        "--use-vnodes command line argument") 
Example 7
Project: cassandra-dtest   Author: apache   File: conftest.py    License: Apache License 2.0 5 votes vote down vote up
def _upgrade_testing_enabled(config):
    return config.getoption("--execute-upgrade-tests") or config.getoption("--execute-upgrade-tests-only") 
Example 8
Project: agent-python-pytest   Author: reportportal   File: plugin.py    License: Apache License 2.0 5 votes vote down vote up
def is_master(config):
    """
    Validate slaveinput attribute.

    True if the code running the given pytest.config object
    is running in a xdist master node or not running xdist at all.
    """
    return not hasattr(config, 'slaveinput') 
Example 9
Project: agent-python-pytest   Author: reportportal   File: plugin.py    License: Apache License 2.0 5 votes vote down vote up
def pytest_configure_node(node):
    """
    Configure node of tests.

    :param node: _pytest.nodes.Node
    :return: pickle of RPService
    """
    if node.config._reportportal_configured is False:
        # Stop now if the plugin is not properly configured
        return
    node.slaveinput['py_test_service'] = pickle.dumps(node.config.
                                                      py_test_service) 
Example 10
Project: agent-python-pytest   Author: reportportal   File: plugin.py    License: Apache License 2.0 5 votes vote down vote up
def pytest_sessionstart(session):
    """
    Start test session.

    :param session: Session
    :return: None
    """
    if session.config._reportportal_configured is False:
        # Stop now if the plugin is not properly configured
        return

    if is_master(session.config):
        session.config.py_test_service.init_service(
            project=session.config.getini('rp_project'),
            endpoint=session.config.getini('rp_endpoint'),
            uuid=getenv('RP_UUID') or session.config.getini('rp_uuid'),
            log_batch_size=int(session.config.getini('rp_log_batch_size')),
            ignore_errors=bool(session.config.getini('rp_ignore_errors')),
            ignored_attributes=session.config.getini('rp_ignore_attributes'),
            verify_ssl=session.config.getini('rp_verify_ssl'),
            retries=int(session.config.getini('retries')),
        )

        attributes = [{'value': tag} for tag in
                      session.config.getini('rp_launch_attributes')]
        session.config.py_test_service.start_launch(
            session.config.option.rp_launch,
            attributes=attributes,
            description=session.config.option.rp_launch_description
        )
        if session.config.pluginmanager.hasplugin('xdist'):
            wait_launch(session.config.py_test_service.rp) 
Example 11
Project: agent-python-pytest   Author: reportportal   File: plugin.py    License: Apache License 2.0 5 votes vote down vote up
def pytest_collection_finish(session):
    """
    Collect tests if session is configured.

    :param session: pytest.Session
    :return: None
    """
    if session.config._reportportal_configured is False:
        # Stop now if the plugin is not properly configured
        return

    session.config.py_test_service.collect_tests(session) 
Example 12
Project: agent-python-pytest   Author: reportportal   File: plugin.py    License: Apache License 2.0 5 votes vote down vote up
def pytest_sessionfinish(session):
    """
    Finish session if has attr  'slaveinput'.

    :param session: pytest.Session
    :return: None
    """
    if session.config._reportportal_configured is False:
        # Stop now if the plugin is not properly configured
        return

    if is_master(session.config):
        session.config.py_test_service.finish_launch() 
Example 13
Project: PyHDB   Author: SAP   File: conftest.py    License: Apache License 2.0 5 votes vote down vote up
def _get_option(config, key):
    return config.getoption(key) or config.inicfg.get(key) 
Example 14
Project: PyHDB   Author: SAP   File: conftest.py    License: Apache License 2.0 5 votes vote down vote up
def hana_system():
    host = _get_option(pytest.config, 'hana_host')
    port = _get_option(pytest.config, 'hana_port') or 30015
    user = _get_option(pytest.config, 'hana_user')
    password = _get_option(pytest.config, 'hana_password')
    return HANASystem(host, port, user, password) 
Example 15
Project: PyHDB   Author: SAP   File: conftest.py    License: Apache License 2.0 5 votes vote down vote up
def pytest_configure(config):
    config.addinivalue_line(
        "markers",
        "hanatest: mark test to run only with SAP HANA system"
    ) 
Example 16
Project: PyHDB   Author: SAP   File: conftest.py    License: Apache License 2.0 5 votes vote down vote up
def pytest_report_header(config):
    hana = hana_system()
    if hana.host is None:
        return [
            "WARNING: No SAP HANA host defined for integration tests"
        ]
    else:
        return [
            "SAP HANA test system",
            "  Host: %s:%s" % (hana.host, hana.port),
            "  User: %s" % hana.user
        ] 
Example 17
Project: PyHDB   Author: SAP   File: conftest.py    License: Apache License 2.0 5 votes vote down vote up
def pytest_runtest_setup(item):
    hana_marker = item.get_marker("hanatest")

    if hana_marker is not None:
        if item.config.getoption("--no-hana"):
            pytest.skip("Test requires SAP HANA system are omitted due to command line option")
        else:
            hana = hana_system()
            if hana.host is None:
                pytest.skip("Test requires SAP HANA system") 
Example 18
Project: cassandra-dtest   Author: apache   File: conftest.py    License: Apache License 2.0 4 votes vote down vote up
def fixture_logging_setup(request):
    # set the root logger level to whatever the user asked for
    # all new loggers created will use the root logger as a template
    # essentially making this the "default" active log level
    log_level = logging.INFO
    try:
        # first see if logging level overridden by user as command line argument
        log_level_from_option = pytest.config.getoption("--log-level")
        if log_level_from_option is not None:
            log_level = logging.getLevelName(log_level_from_option)
        else:
            raise ValueError
    except ValueError:
        # nope, user didn't specify it as a command line argument to pytest, check if
        # we have a default in the loaded pytest.ini. Note: words are seperated in variables
        # in .ini land with a "_" while the command line arguments use "-"
        if pytest.config.inicfg.get("log_level") is not None:
            log_level = logging.getLevelName(pytest.config.inicfg.get("log_level"))

    logging.root.setLevel(log_level)

    logging_format = None
    try:
        # first see if logging level overridden by user as command line argument
        log_format_from_option = pytest.config.getoption("--log-format")
        if log_format_from_option is not None:
            logging_format = log_format_from_option
        else:
            raise ValueError
    except ValueError:
        if pytest.config.inicfg.get("log_format") is not None:
            logging_format = pytest.config.inicfg.get("log_format")

    logging.basicConfig(level=log_level,
                        format=logging_format)

    # next, regardless of the level we set above (and requested by the user),
    # reconfigure the "cassandra" logger to minimum INFO level to override the
    # logging level that the "cassandra.*" imports should use; DEBUG is just
    # insanely noisy and verbose, with the extra logging of very limited help
    # in the context of dtest execution
    if log_level == logging.DEBUG:
        cassandra_module_log_level = logging.INFO
    else:
        cassandra_module_log_level = log_level
    logging.getLogger("cassandra").setLevel(cassandra_module_log_level) 
Example 19
Project: cassandra-dtest   Author: apache   File: conftest.py    License: Apache License 2.0 4 votes vote down vote up
def fixture_dtest_setup(request,
                        dtest_config,
                        fixture_dtest_setup_overrides,
                        fixture_logging_setup,
                        fixture_dtest_cluster_name,
                        fixture_dtest_create_cluster_func):
    if running_in_docker():
        cleanup_docker_environment_before_test_execution()

    # do all of our setup operations to get the enviornment ready for the actual test
    # to run (e.g. bring up a cluster with the necessary config, populate variables, etc)
    initial_environment = copy.deepcopy(os.environ)
    dtest_setup = DTestSetup(dtest_config=dtest_config,
                             setup_overrides=fixture_dtest_setup_overrides,
                             cluster_name=fixture_dtest_cluster_name)
    dtest_setup.initialize_cluster(fixture_dtest_create_cluster_func)

    if not dtest_config.disable_active_log_watching:
        dtest_setup.begin_active_log_watch()

    # at this point we're done with our setup operations in this fixture
    # yield to allow the actual test to run
    yield dtest_setup

    # phew! we're back after executing the test, now we need to do
    # all of our teardown and cleanup operations

    reset_environment_vars(initial_environment)
    dtest_setup.jvm_args = []

    for con in dtest_setup.connections:
        con.cluster.shutdown()
    dtest_setup.connections = []

    failed = False
    try:
        if not dtest_setup.allow_log_errors:
            errors = check_logs_for_errors(dtest_setup)
            if len(errors) > 0:
                failed = True
                pytest.fail(msg='Unexpected error found in node logs (see stdout for full details). Errors: [{errors}]'
                                     .format(errors=str.join(", ", errors)), pytrace=False)
    finally:
        try:
            # save the logs for inspection
            if failed or not dtest_config.delete_logs:
                copy_logs(request, dtest_setup.cluster)
        except Exception as e:
            logger.error("Error saving log:", str(e))
        finally:
            dtest_setup.cleanup_cluster()


#Based on https://bugs.python.org/file25808/14894.patch 
Example 20
Project: ocs-ci   Author: red-hat-storage   File: ocscilib.py    License: MIT License 4 votes vote down vote up
def pytest_configure(config):
    """
    Load config files, and initialize ocs-ci library.

    Args:
        config (pytest.config): Pytest config object

    """
    if not (config.getoption("--help") or config.getoption("collectonly")):
        process_cluster_cli_params(config)
        config_file = os.path.expanduser(
            os.path.join(
                ocsci_config.RUN['log_dir'],
                f"run-{ocsci_config.RUN['run_id']}-config.yaml",
            )
        )
        dump_config_to_file(config_file)
        log.info(
            f"Dump of the consolidated config file is located here: "
            f"{config_file}"
        )
        # Add OCS related versions to the html report and remove
        # extraneous metadata
        markers_arg = config.getoption('-m')
        if ocsci_config.RUN['cli_params'].get('teardown') or (
            "deployment" in markers_arg
            and ocsci_config.RUN['cli_params'].get('deploy')
        ):
            log.info(
                "Skipping versions collecting because: Deploy or destroy of "
                "cluster is performed."
            )
            return
        elif ocsci_config.ENV_DATA['skip_ocs_deployment']:
            log.info(
                "Skipping version collection because we skipped "
                "the OCS deployment"
            )
            return
        print("Collecting Cluster versions")
        # remove extraneous metadata
        del config._metadata['Python']
        del config._metadata['Packages']
        del config._metadata['Plugins']
        del config._metadata['Platform']

        config._metadata['Test Run Name'] = get_testrun_name()
        gather_version_info_for_report(config) 
Example 21
Project: ocs-ci   Author: red-hat-storage   File: ocscilib.py    License: MIT License 4 votes vote down vote up
def gather_version_info_for_report(config):
    """
    This function gather all version related info used for report.

    Args:
        config (pytest.config): Pytest config object
    """
    gather_version_completed = False
    try:
        # add cluster version
        clusterversion = get_cluster_version()
        config._metadata['Cluster Version'] = clusterversion

        # add ceph version
        ceph_version = get_ceph_version()
        config._metadata['Ceph Version'] = ceph_version

        # add csi versions
        csi_versions = get_csi_versions()
        config._metadata['cephfsplugin'] = csi_versions.get('csi-cephfsplugin')
        config._metadata['rbdplugin'] = csi_versions.get('csi-rbdplugin')

        # add ocs operator version
        if ocsci_config.REPORTING['us_ds'] == 'DS':
            config._metadata['OCS operator'] = (
                get_ocs_build_number()
            )
        mods = {}
        mods = get_version_info(
            namespace=ocsci_config.ENV_DATA['cluster_namespace']
        )
        skip_list = ['ocs-operator']
        for key, val in mods.items():
            if key not in skip_list:
                config._metadata[key] = val.rsplit('/')[-1]
        gather_version_completed = True
    except ResourceNotFoundError as ex:
        log.error(
            "Problem ocurred when looking for some resource! Error: %s",
            ex
        )
    except FileNotFoundError as ex:
        log.error("File not found! Error: %s", ex)
    except CommandFailed as ex:
        log.error("Failed to execute command! Error: %s", ex)
    except Exception as ex:
        log.error("Failed to gather version info! Error: %s", ex)
    finally:
        if not gather_version_completed:
            log.warning(
                "Failed to gather version details! The report of version might"
                "not be complete!"
            )