def start(self, request, monkeypatch): _env_list = self.env _setup_dict = self.setup_dict # first method for monkeypatching def _setup(self, x): _s = copy.deepcopy(_setup_dict) return _s # second method for monkeypatching def _conf(self, x): _e = copy.deepcopy(_env_list) return _e # monkeypatching methods _get_conf and _get_setup monkeypatch.setattr(common3.Environment, "_get_conf", _conf) monkeypatch.setattr(common3.Environment, "_get_setup", _setup) # define environment with fake class # mock Tkinter so we don't need Ixia installed # I only know how to do this with mock, if monkeypatch can do this we should use that with patch.dict('sys.modules', {'Tkinter': MagicMock()}): env = common3.Environment(FakeOpts()) return env
def env_simple(request, monkeypatch): """Fixture of environment for unittests of methods get_ports and get_speed. """ # first method for monkeypatching def _setup(self, x): return SETUP_SIMPLE # second method for monkeypatching def _conf(self, x): return ENV # third method for monkeypatching def _init(self): pass # monkeypatching methods _get_conf and _get_setup monkeypatch.setattr(common3.Environment, "_get_conf", _conf) monkeypatch.setattr(common3.Environment, "_get_setup", _setup) # define environment with fake class env = common3.Environment(FakeOpts()) return env
def test_import_common3_module(monkeypatch): """Verify that all modules can be imported within 'common3' module and 'Cross'/'Environment' objects can be created. """ def fake_get_conf(env_object, path_string): """Get config. """ return {'env': []} module_name = "common3" try: from testlib import common3 common3.Cross(None, None) # replace Environment _get_setup method monkeypatch.setattr(common3.Environment, "_get_setup", fake_get_conf) common3.Environment(FakeOpts()) except ImportError as err: pytest.fail("Import failure in '%s' module: %s" % (module_name, err))
def pytest_sessionstart(session): # Check options session.config.ctlogger = loggers.module_logger("conftest") session.config.ctlogger.debug("Session start...") if not (session.config.option.setup_scope in ["session", "module", "class", "function"]): session.config.ctlogger.error("Incorrect --setup_scope option.") pytest.exit("Incorrect --setup_scope option.") if not (session.config.option.call_check in ["none", "complete", "fast", "sanity_check_only"]): session.config.ctlogger.error("Incorrect --call_check option.") pytest.exit("Incorrect --call_check option.") if not (session.config.option.teardown_check in ["none", "complete", "fast", "sanity_check_only"]): session.config.ctlogger.error("Incorrect --teardown_check option.") pytest.exit("Incorrect --teardown_check option.") if not (session.config.option.fail_ctrl in ["stop", "restart", "ignore"]): session.config.ctlogger.error("Incorrect --fail_ctrl option.") pytest.exit("Incorrect --fail_ctrl option.") # Define environment session.config.env = common3.Environment(session.config.option)
def start(self, request, monkeypatch, xmlrpcs): for serv in xmlrpcs: serv.lags = [] serv.ports_to_lags = [] _env_list = self.env _setup_dict = self.setup_dict # first method for monkeypatching def _setup(self, x): _s = copy.deepcopy(_setup_dict) return _s # second method for monkeypatching def _conf(self, x): _e = copy.deepcopy(_env_list) return _e def clearconfig(*args, **kwargs): pass # monkeypatching methods _get_conf and _get_setup monkeypatch.setattr(common3.Environment, "_get_conf", _conf) monkeypatch.setattr(common3.Environment, "_get_setup", _setup) # define environment with fake class # mock Tkinter so we don't need Ixia installed # I only know how to do this with mock, if monkeypatch can do this we should use that with patch.dict('sys.modules', {'Tkinter': MagicMock()}): env = common3.Environment(FakeOpts()) # Create fake XMLRPC server for switch, serv in zip(iter(env.switch.values()), xmlrpcs): # pylint: disable=no-member # use the actual port of the server from the BaseServer superclass attribute prox = xmlrpc.client.ServerProxy('http://{0[0]}:{0[1]}'.format( serv.server.server_address)) switch.xmlproxy = prox # pylint: disable=no-member switch.hw.max_lags = 256 # pylint: disable=no-member switch.clearconfig = clearconfig return env
def test_tg(request, monkeypatch): """ Fixture of environment with TG for unittests of methods get_ports. """ # first method for monkeypatching def _setup(self, x): return SETUP_TG # second method for monkeypatching def _conf(self, x): return ENV # third method for monkeypatching def _init(self): pass # monkeypatching methods _get_conf and _get_setup monkeypatch.setattr(common3.Environment, "_get_conf", _conf) monkeypatch.setattr(common3.Environment, "_get_setup", _setup) # monkeypatching method _init_tcl from class Ixia monkeypatch.setattr(Ixia, "_init_tcl", _init) # define environment with fake class env = common3.Environment(FakeOpts()) return env
def pytest_sessionstart(self, session): session.config.ctlogger.debug("Session start...") # Define environment session.config.env = common3.Environment(session.config.option)