def _get_pytest_config(self, argv): """ Return the ``pytest`` configuration object based on the command-line arguments """ import _pytest.config as _config plugins = [self] # disable pytest-cov argv += ['-p', 'no:pytest_cov'] # Do not load any conftests initially. # This is a hack to avoid ConftestImportFailure raised when the # pytest does its initial import of conftests in the source directory. # This prevents any manipulation of the command-line via conftests argv += ['--noconftest'] # get the pytest configuration object try: config = _config._prepareconfig(argv, plugins) except _config.ConftestImportFailure as e: tw = _config.py.io.TerminalWriter(sys.stderr) for line in traceback.format_exception(*e.excinfo): tw.line(line.rstrip(), red=True) tw.line("ERROR: could not load %s\n" % (e.path), red=True) raise # Restore the loading of conftests, which was disabled earlier # Conftest files will now be loaded properly at test time config.pluginmanager._noconftest = False return config
def get_cases_from_pytest(group): config = _prepareconfig(args=str("")) session = Session(config) session._fixturemanager = FixtureManager(session) ret = [i for i in session.perform_collect() if group in list(MarkMapping(i.keywords)._mymarks)] return ret
def group_in_pytest(group): config = _prepareconfig(args=str("")) session = Session(config) session._fixturemanager = FixtureManager(session) l = [list(MarkMapping(i.keywords)._mymarks) for i in session.perform_collect()] groups = set([item for sublist in l for item in sublist]) return group in groups
def group_in_pytest(group): from _pytest.config import _prepareconfig from _pytest.main import Session from _pytest.python import FixtureManager from _pytest.mark import MarkMapping config = _prepareconfig(args="") session = Session(config) session._fixturemanager = FixtureManager(session) l = [list(MarkMapping(i.keywords)._mymarks) for i in session.perform_collect()] groups = set([item for sublist in l for item in sublist]) return group in groups
def exec_file(filename, globals=None, locals=None): if globals is None: globals = {} if locals is None: locals = globals locals['__file__'] = filename from py import path from _pytest import config from _pytest.assertion import rewrite f = path.local(filename) filename2 = os.path.relpath(filename, os.getcwd()) config = config._prepareconfig([], []) _, code = rewrite._rewrite_test(config, f) exec(code, globals, locals)
def exec_file(filename, globals=None, locals=None): """Execute the specified file, optionaly setup its context by using globals and locals.""" if globals is None: globals = {} if locals is None: locals = globals locals['__file__'] = filename from py import path from _pytest import config from _pytest.assertion import rewrite f = path.local(filename) filename2 = os.path.relpath(filename, os.getcwd()) config = config._prepareconfig([], []) _, code = rewrite._rewrite_test(config, f) exec(code, globals, locals)
def exec_file(filename, globals=None, locals=None): """Execute the specified file, optionaly setup its context by using globals and locals.""" if globals is None: globals = {} if locals is None: locals = globals locals['__file__'] = filename from py import path from _pytest import config from _pytest.assertion import rewrite f = path.local(filename) config = config._prepareconfig([], []) source_stat, code = rewrite._rewrite_test(config, f) logger.debug('filename: {} source_stat: {} code: {}'.format(filename, source_stat, code)) exec(code, globals, locals)
def group_in_pytest(group): from _pytest.config import _prepareconfig from _pytest.main import Session from _pytest.python import FixtureManager from _pytest.mark import MarkMapping config = _prepareconfig(args="") session = Session(config) session._fixturemanager = FixtureManager(session) l = [ list(MarkMapping(i.keywords)._mymarks) for i in session.perform_collect() ] groups = set([item for sublist in l for item in sublist]) return group in groups
def run(index: int, host: str, port: int, start_sem: Semaphore, fixture_sem: Semaphore, test_q: JoinableQueue, result_q: Queue, node_port: int) -> None: start_sem.acquire() worker = WorkerSession(index, host, port, test_q, result_q) worker._node_fixture_manager = Node.Manager(as_main=False, port=node_port, name=f"Worker-{index}") worker._fixture_sem = fixture_sem args = sys.argv[1:] # remove coverage args, as pytest_cov handles multiprocessing already and will applyt coverage to worker # as a proc that was launched from main thread which itsalef has coverage (otherwise it will attempt # duplicate coverage processing and file conflicts galore) args = [arg for arg in args if not arg.startswith("--cov=")] config = _prepareconfig(args, plugins=[]) # unregister terminal (don't want to output to stdout from worker) # as well as xdist (don't want to invoke any plugin hooks from another distribute testing plugin if present) config.pluginmanager.unregister(name="terminal") config.pluginmanager.register(worker, "mproc_worker") config.option.mproc_worker = worker from pytest_mproc.main import Orchestrator worker._client = Orchestrator.Manager(addr=(worker._host, worker._port)) workerinput = { 'slaveid': "worker-%d" % worker._index, 'workerid': "worker-%d" % worker._index, 'cov_master_host': socket.gethostname(), 'cov_slave_output': os.path.join(os.getcwd(), "worker-%d" % worker._index), 'cov_master_topdir': os.getcwd() } config.slaveinput = workerinput config.slaveoutput = workerinput try: # and away we go.... config.hook.pytest_cmdline_main(config=config) finally: config._ensure_unconfigure() worker._reporter.write(f"\nWorker-{index} finished\n")
config.option.maxprocesses = None config.option.basetemp = basetemp if __name__ == "__channelexec__": channel = channel # noqa workerinput, args, option_dict, change_sys_path = channel.receive() if change_sys_path: importpath = os.getcwd() sys.path.insert(0, importpath) os.environ["PYTHONPATH"] = (importpath + os.pathsep + os.environ.get("PYTHONPATH", "")) os.environ["PYTEST_XDIST_TESTRUNUID"] = workerinput["testrunuid"] os.environ["PYTEST_XDIST_WORKER"] = workerinput["workerid"] os.environ["PYTEST_XDIST_WORKER_COUNT"] = str(workerinput["workercount"]) if hasattr(Config, "InvocationParams"): config = _prepareconfig(args, None) else: config = remote_initconfig(option_dict, args) config.args = args setup_config(config, option_dict.get("basetemp")) config._parser.prog = os.path.basename(workerinput["mainargv"][0]) config.workerinput = workerinput config.workeroutput = {} interactor = WorkerInteractor(config, channel) config.hook.pytest_cmdline_main(config=config)
# -*- coding: utf-8 -*- """For debugging in ide""" from tests.conftest import * from pytest_mock import mocker from _pytest.config import _prepareconfig from tests.test_cmdb_attribute import test_create_attribute for a in app(): for d in database(a): for s in session(d, a): for c in client(a): for m in mocker(_prepareconfig()): clean_db() test_create_attribute(s, c)
# coding=utf-8 import re import sys import pytest from _pytest.config import _prepareconfig from _jb_runner_tools import jb_start_tests, jb_patch_separator, jb_doc_args from teamcity import pytest_plugin if __name__ == '__main__': path, targets, additional_args = jb_start_tests() sys.argv += additional_args joined_targets = jb_patch_separator(targets, fs_glue="/", python_glue="::", fs_to_python_glue=".py::") # When file is launched in py.test it should be file.py: you can't provide it as bare module joined_targets = [t + ".py" if ":" not in t else t for t in joined_targets] sys.argv += [path] if path else joined_targets jb_doc_args("py.test", sys.argv[1:]) # plugin is discovered automatically in 3, but not in 2 # to prevent "plugin already registered" problem we check it first plugins_to_load = [] if not _prepareconfig().pluginmanager.hasplugin("pytest-teamcity"): plugins_to_load.append(pytest_plugin) pytest.main(sys.argv[1:], plugins_to_load)