Best Python code snippet using tox_python
test_session.py
Source:test_session.py
...142 "py36-diffcov",143 "py36-diffcov-extra",144 ]145@pytest.fixture()146def popen_env_test(initproj, cmd, monkeypatch):147 def func(tox_env, isolated_build):148 files = {149 "tox.ini": """150 [tox]151 isolated_build = {}152 [testenv:{}]153 commands = python -c "print('ok')"154 """.format(155 "True" if isolated_build else "False",156 tox_env,157 ),158 }159 if isolated_build:160 files[161 "pyproject.toml"162 ] = """163 [build-system]164 requires = ["setuptools >= 35.0.2", "setuptools_scm >= 2.0.0, <3"]165 build-backend = 'setuptools.build_meta'166 """167 initproj("env_var_test", filedefs=files)168 class IsolatedResult(object):169 def __init__(self):170 self.popens = []171 self.cwd = None172 res = IsolatedResult()173 class EnvironmentTestRun(Thread):174 """we wrap this invocation into a thread to avoid modifying in any way the175 current threads environment variable (e.g. on failure of this test incorrect teardown)176 """177 def run(self):178 prev_build = tox.session.build_session179 def build_session(config):180 res.session = prev_build(config)181 res._popen = res.session.popen182 monkeypatch.setattr(res.session, "popen", popen)183 return res.session184 monkeypatch.setattr(tox.session, "build_session", build_session)185 def popen(cmd, **kwargs):186 activity_id = _actions[-1].name187 activity_name = _actions[-1].activity188 ret = "NOTSET"189 try:190 ret = res._popen(cmd, **kwargs)191 except tox.exception.InvocationError as exception:192 ret = exception193 finally:194 res.popens.append(195 (activity_id, activity_name, kwargs.get("env"), ret, cmd),196 )197 return ret198 _actions = []199 from tox.action import Action200 _prev_enter = Action.__enter__201 def enter(self):202 _actions.append(self)203 return _prev_enter(self)204 monkeypatch.setattr(Action, "__enter__", enter)205 _prev_exit = Action.__exit__206 def exit_func(self, *args, **kwargs):207 del _actions[_actions.index(self)]208 _prev_exit(self, *args, **kwargs)209 monkeypatch.setattr(Action, "__exit__", exit_func)210 res.result = cmd("-e", tox_env)211 res.cwd = os.getcwd()212 thread = EnvironmentTestRun()213 thread.start()214 thread.join()215 return res216 yield func217@pytest.mark.network218def test_tox_env_var_flags_inserted_non_isolated(popen_env_test):219 res = popen_env_test("py", False)220 assert_popen_env(res)221@pytest.mark.network222def test_tox_env_var_flags_inserted_isolated(popen_env_test):223 res = popen_env_test("py", True)224 assert_popen_env(res)225def assert_popen_env(res):226 res.result.assert_success()227 for tox_id, _, env, __, ___ in res.popens:228 assert env["TOX_WORK_DIR"] == os.path.join(res.cwd, ".tox")229 if tox_id != "GLOB":230 assert env["TOX_ENV_NAME"] == tox_id231 assert env["TOX_ENV_DIR"] == os.path.join(res.cwd, ".tox", tox_id)232 # ensure native strings for environ for windows233 for k, v in env.items():234 assert type(k) is str, (k, v, type(k))235 assert type(v) is str, (k, v, type(v))236def test_command_prev_post_ok(cmd, initproj, mock_venv):237 initproj(...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!