Best Python code snippet using tox_python
test_venv.py
Source:test_venv.py
...95 l = mocksession._pcalls96 assert len(l) >= 197 args = l[0].args98 assert "--system-site-packages" in map(str, args)99 mocksession._clearmocks()100 envconfig = config.envconfigs['nosite']101 venv = VirtualEnv(envconfig, session=mocksession)102 venv.create()103 l = mocksession._pcalls104 assert len(l) >= 1105 args = l[0].args106 assert "--system-site-packages" not in map(str, args)107 assert "--no-site-packages" not in map(str, args)108def test_install_deps_wildcard(newmocksession):109 mocksession = newmocksession([], """110 [tox]111 distshare = {toxworkdir}/distshare112 [testenv:py123]113 deps=114 {distshare}/dep1-*115 """)116 venv = mocksession.getenv("py123")117 venv.create()118 l = mocksession._pcalls119 assert len(l) == 1120 distshare = venv.session.config.distshare121 distshare.ensure("dep1-1.0.zip")122 distshare.ensure("dep1-1.1.zip")123 venv.install_deps()124 assert len(l) == 2125 args = l[-1].args126 assert l[-1].cwd == venv.envconfig.config.toxinidir127 assert "pip" in str(args[0])128 assert args[1] == "install"129 #arg = "--download-cache=" + str(venv.envconfig.downloadcache)130 #assert arg in args[2:]131 args = [arg for arg in args if str(arg).endswith("dep1-1.1.zip")]132 assert len(args) == 1133@pytest.mark.parametrize("envdc", [True, False])134def test_install_downloadcache(newmocksession, monkeypatch, tmpdir, envdc):135 if envdc:136 monkeypatch.setenv("PIP_DOWNLOAD_CACHE", tmpdir)137 else:138 monkeypatch.delenv("PIP_DOWNLOAD_CACHE", raising=False)139 mocksession = newmocksession([], """140 [testenv:py123]141 distribute=True142 deps=143 dep1144 dep2145 """)146 venv = mocksession.getenv("py123")147 venv.create()148 l = mocksession._pcalls149 assert len(l) == 1150 venv.install_deps()151 assert len(l) == 2152 args = l[-1].args153 assert l[-1].cwd == venv.envconfig.config.toxinidir154 assert "pip" in str(args[0])155 assert args[1] == "install"156 assert "dep1" in args157 assert "dep2" in args158 deps = list(filter(None, [x[1] for x in venv._getliveconfig().deps]))159 assert deps == ['dep1', 'dep2']160def test_install_deps_indexserver(newmocksession):161 mocksession = newmocksession([], """162 [tox]163 indexserver =164 abc = ABC165 abc2 = ABC166 [testenv:py123]167 deps=168 dep1169 :abc:dep2170 :abc2:dep3171 """)172 venv = mocksession.getenv('py123')173 venv.create()174 l = mocksession._pcalls175 assert len(l) == 1176 l[:] = []177 venv.install_deps()178 # two different index servers, two calls179 assert len(l) == 3180 args = " ".join(l[0].args)181 assert "-i " not in args182 assert "dep1" in args183 args = " ".join(l[1].args)184 assert "-i ABC" in args185 assert "dep2" in args186 args = " ".join(l[2].args)187 assert "-i ABC" in args188 assert "dep3" in args189def test_installpkg_indexserver(newmocksession, tmpdir):190 mocksession = newmocksession([], """191 [tox]192 indexserver =193 default = ABC194 """)195 venv = mocksession.getenv('python')196 l = mocksession._pcalls197 p = tmpdir.ensure("distfile.tar.gz")198 mocksession.installpkg(venv, p)199 # two different index servers, two calls200 assert len(l) == 1201 args = " ".join(l[0].args)202 assert "-i ABC" in args203def test_install_recreate(newmocksession, tmpdir):204 pkg = tmpdir.ensure("package.tar.gz")205 mocksession = newmocksession(['--recreate'], """206 [testenv]207 deps=xyz208 """)209 venv = mocksession.getenv('python')210 venv.update()211 mocksession.installpkg(venv, pkg)212 mocksession.report.expect("verbosity0", "*create*")213 venv.update()214 mocksession.report.expect("verbosity0", "*recreate*")215def test_test_hashseed_is_in_output(newmocksession):216 original_make_hashseed = tox._config.make_hashseed217 tox._config.make_hashseed = lambda: '123456789'218 try:219 mocksession = newmocksession([], '''220 [testenv]221 ''')222 finally:223 tox._config.make_hashseed = original_make_hashseed224 venv = mocksession.getenv('python')225 venv.update()226 venv.test()227 mocksession.report.expect("verbosity0", "python runtests: PYTHONHASHSEED='123456789'")228def test_test_runtests_action_command_is_in_output(newmocksession):229 mocksession = newmocksession([], '''230 [testenv]231 commands = echo foo bar232 ''')233 venv = mocksession.getenv('python')234 venv.update()235 venv.test()236 mocksession.report.expect("verbosity0", "*runtests*commands?0? | echo foo bar")237def test_install_error(newmocksession, monkeypatch):238 mocksession = newmocksession(['--recreate'], """239 [testenv]240 deps=xyz241 commands=242 qwelkqw243 """)244 venv = mocksession.getenv('python')245 venv.test()246 mocksession.report.expect("error", "*not find*qwelkqw*")247 assert venv.status == "commands failed"248def test_install_command_not_installed(newmocksession, monkeypatch):249 mocksession = newmocksession(['--recreate'], """250 [testenv]251 commands=252 py.test253 """)254 venv = mocksession.getenv('python')255 venv.test()256 mocksession.report.expect("warning", "*test command found but not*")257 assert venv.status == "commands failed"258def test_install_command_whitelisted(newmocksession, monkeypatch):259 mocksession = newmocksession(['--recreate'], """260 [testenv]261 whitelist_externals = py.test262 xy*263 commands=264 py.test265 xyz266 """)267 venv = mocksession.getenv('python')268 venv.test()269 mocksession.report.expect("warning", "*test command found but not*",270 invert=True)271 assert venv.status == "commands failed"272@pytest.mark.skipif("not sys.platform.startswith('linux')")273def test_install_command_not_installed(newmocksession):274 mocksession = newmocksession(['--recreate'], """275 [testenv]276 commands=277 bash278 """)279 venv = mocksession.getenv('python')280 venv.test()281 mocksession.report.expect("warning", "*test command found but not*")282def test_install_python3(tmpdir, newmocksession):283 if not py.path.local.sysfind('python3.3'):284 pytest.skip("needs python3.3")285 mocksession = newmocksession([], """286 [testenv:py123]287 basepython=python3.3288 deps=289 dep1290 dep2291 """)292 venv = mocksession.getenv('py123')293 venv.create()294 l = mocksession._pcalls295 assert len(l) == 1296 args = l[0].args297 assert str(args[1]).endswith('virtualenv')298 l[:] = []299 action = mocksession.newaction(venv, "hello")300 venv._install(["hello"], action=action)301 assert len(l) == 1302 args = l[0].args303 assert 'pip' in str(args[0])304 for x in args:305 assert "--download-cache" not in args, args306class TestCreationConfig:307 def test_basic(self, newconfig, mocksession, tmpdir):308 config = newconfig([], "")309 envconfig = config.envconfigs['python']310 venv = VirtualEnv(envconfig, session=mocksession)311 cconfig = venv._getliveconfig()312 assert cconfig.matches(cconfig)313 path = tmpdir.join("configdump")314 cconfig.writeconfig(path)315 newconfig = CreationConfig.readconfig(path)316 assert newconfig.matches(cconfig)317 assert cconfig.matches(newconfig)318 def test_matchingdependencies(self, newconfig, mocksession):319 config = newconfig([], """320 [testenv]321 deps=abc322 """)323 envconfig = config.envconfigs['python']324 venv = VirtualEnv(envconfig, session=mocksession)325 cconfig = venv._getliveconfig()326 config = newconfig([], """327 [testenv]328 deps=xyz329 """)330 envconfig = config.envconfigs['python']331 venv = VirtualEnv(envconfig, session=mocksession)332 otherconfig = venv._getliveconfig()333 assert not cconfig.matches(otherconfig)334 def test_matchingdependencies_file(self, newconfig, mocksession):335 config = newconfig([], """336 [tox]337 distshare={toxworkdir}/distshare338 [testenv]339 deps=abc340 {distshare}/xyz.zip341 """)342 xyz = config.distshare.join("xyz.zip")343 xyz.ensure()344 envconfig = config.envconfigs['python']345 venv = VirtualEnv(envconfig, session=mocksession)346 cconfig = venv._getliveconfig()347 assert cconfig.matches(cconfig)348 xyz.write("hello")349 newconfig = venv._getliveconfig()350 assert not cconfig.matches(newconfig)351 def test_matchingdependencies_latest(self, newconfig, mocksession):352 config = newconfig([], """353 [tox]354 distshare={toxworkdir}/distshare355 [testenv]356 deps={distshare}/xyz-*357 """)358 xyz = config.distshare.ensure("xyz-1.2.0.zip")359 xyz2 = config.distshare.ensure("xyz-1.2.1.zip")360 envconfig = config.envconfigs['python']361 venv = VirtualEnv(envconfig, session=mocksession)362 cconfig = venv._getliveconfig()363 md5, path = cconfig.deps[0]364 assert path == xyz2365 assert md5 == path.computehash()366 def test_python_recreation(self, tmpdir, newconfig, mocksession):367 pkg = tmpdir.ensure("package.tar.gz")368 config = newconfig([], "")369 envconfig = config.envconfigs['python']370 venv = VirtualEnv(envconfig, session=mocksession)371 cconfig = venv._getliveconfig()372 venv.update()373 assert not venv.path_config.check()374 mocksession.installpkg(venv, pkg)375 assert venv.path_config.check()376 assert mocksession._pcalls377 args1 = map(str, mocksession._pcalls[0].args)378 assert 'virtualenv' in " ".join(args1)379 mocksession.report.expect("*", "*create*")380 # modify config and check that recreation happens381 mocksession._clearmocks()382 venv.update()383 mocksession.report.expect("*", "*reusing*")384 mocksession._clearmocks()385 cconfig.python = py.path.local("balla")386 cconfig.writeconfig(venv.path_config)387 venv.update()388 mocksession.report.expect("verbosity0", "*recreate*")389 def test_dep_recreation(self, newconfig, mocksession):390 config = newconfig([], "")391 envconfig = config.envconfigs['python']392 venv = VirtualEnv(envconfig, session=mocksession)393 venv.update()394 cconfig = venv._getliveconfig()395 cconfig.deps[:] = [("1"*32, "xyz.zip")]396 cconfig.writeconfig(venv.path_config)397 mocksession._clearmocks()398 venv.update()399 mocksession.report.expect("*", "*recreate*")400 def test_distribute_recreation(self, newconfig, mocksession):401 config = newconfig([], "")402 envconfig = config.envconfigs['python']403 venv = VirtualEnv(envconfig, session=mocksession)404 venv.update()405 cconfig = venv._getliveconfig()406 cconfig.distribute = True407 cconfig.writeconfig(venv.path_config)408 mocksession._clearmocks()409 venv.update()410 mocksession.report.expect("verbosity0", "*recreate*")411 def test_develop_recreation(self, newconfig, mocksession):412 config = newconfig([], "")413 envconfig = config.envconfigs['python']414 venv = VirtualEnv(envconfig, session=mocksession)415 venv.update()416 cconfig = venv._getliveconfig()417 cconfig.develop = True418 cconfig.writeconfig(venv.path_config)419 mocksession._clearmocks()420 venv.update()421 mocksession.report.expect("verbosity0", "*recreate*")422class TestVenvTest:423 def test_patchPATH(self, newmocksession, monkeypatch):424 monkeypatch.setenv("PIP_RESPECT_VIRTUALENV", "1")425 mocksession = newmocksession([], """426 [testenv:python]427 commands=abc428 """)429 venv = mocksession.getenv("python")430 envconfig = venv.envconfig431 monkeypatch.setenv("PATH", "xyz")432 oldpath = venv.patchPATH()433 assert oldpath == "xyz"434 res = os.environ['PATH']435 assert res == "%s%sxyz" % (envconfig.envbindir, os.pathsep)436 p = "xyz"+os.pathsep+str(envconfig.envbindir)437 monkeypatch.setenv("PATH", p)438 venv.patchPATH()439 res = os.environ['PATH']440 assert res == "%s%s%s" %(envconfig.envbindir, os.pathsep, p)441 assert envconfig.commands442 monkeypatch.setattr(venv, '_pcall', lambda *args, **kwargs: 0/0)443 py.test.raises(ZeroDivisionError, "venv._install(list('123'))")444 py.test.raises(ZeroDivisionError, "venv.test()")445 py.test.raises(ZeroDivisionError, "venv.run_install_command(['qwe'])")446 py.test.raises(ZeroDivisionError, "venv._pcall([1,2,3])")447 monkeypatch.setenv("PIP_RESPECT_VIRTUALENV", "1")448 monkeypatch.setenv("PIP_REQUIRE_VIRTUALENV", "1")449 py.test.raises(ZeroDivisionError, "venv.run_install_command(['qwe'])")450 assert 'PIP_RESPECT_VIRTUALENV' not in os.environ451 assert 'PIP_REQUIRE_VIRTUALENV' not in os.environ452def test_setenv_added_to_pcall(tmpdir, mocksession, newconfig):453 pkg = tmpdir.ensure("package.tar.gz")454 config = newconfig([], """455 [testenv:python]456 commands=python -V457 setenv =458 ENV_VAR = value459 """)460 mocksession._clearmocks()461 venv = VirtualEnv(config.envconfigs['python'], session=mocksession)462 # import pdb; pdb.set_trace()463 mocksession.installpkg(venv, pkg)464 venv.test()465 l = mocksession._pcalls466 assert len(l) == 2467 for x in l:468 args = x.args469 env = x.env470 assert env is not None471 assert 'ENV_VAR' in env472 assert env['ENV_VAR'] == 'value'473 for e in os.environ:474 assert e in env...
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!!