Best Python code snippet using tox_python
test_z_cmdline.py
Source:test_z_cmdline.py
...26 mocksession.config.logdir27 )28 assert log_name == "what-0.log"29 def test_summary_status(self, initproj, capfd):30 initproj(31 "logexample123-0.5",32 filedefs={33 "tests": {"test_hello.py": "def test_hello(): pass"},34 "tox.ini": """35 [testenv:hello]36 [testenv:world]37 """,38 },39 )40 config = parseconfig([])41 session = Session(config)42 envs = list(session.venv_dict.values())43 assert len(envs) == 244 env1, env2 = envs45 env1.status = "FAIL XYZ"46 assert env1.status47 env2.status = 048 assert not env2.status49 session._summary()50 out, err = capfd.readouterr()51 exp = "{}: FAIL XYZ".format(env1.envconfig.envname)52 assert exp in out53 exp = "{}: commands succeeded".format(env2.envconfig.envname)54 assert exp in out55 def test_getvenv(self, initproj):56 initproj(57 "logexample123-0.5",58 filedefs={59 "tests": {"test_hello.py": "def test_hello(): pass"},60 "tox.ini": """61 [testenv:hello]62 [testenv:world]63 """,64 },65 )66 config = parseconfig([])67 session = Session(config)68 venv1 = session.getvenv("hello")69 venv2 = session.getvenv("hello")70 assert venv1 is venv271 venv1 = session.getvenv("world")72 venv2 = session.getvenv("world")73 assert venv1 is venv274 with pytest.raises(LookupError):75 session.getvenv("qwe")76def test_notoxini_help_still_works(initproj, cmd):77 initproj("example123-0.5", filedefs={"tests": {"test_hello.py": "def test_hello(): pass"}})78 result = cmd("-h")79 msg = "ERROR: tox config file (either pyproject.toml, tox.ini, setup.cfg) not found\n"80 assert result.err == msg81 assert result.out.startswith("usage: ")82 assert any("--help" in l for l in result.outlines), result.outlines83 result.assert_success(is_run_test_env=False)84def test_notoxini_help_ini_still_works(initproj, cmd):85 initproj("example123-0.5", filedefs={"tests": {"test_hello.py": "def test_hello(): pass"}})86 result = cmd("--help-ini")87 assert any("setenv" in l for l in result.outlines), result.outlines88 result.assert_success(is_run_test_env=False)89def test_envdir_equals_toxini_errors_out(cmd, initproj):90 initproj(91 "interp123-0.7",92 filedefs={93 "tox.ini": """94 [testenv]95 envdir={toxinidir}96 """97 },98 )99 result = cmd()100 assert result.outlines[1] == "ERROR: ConfigError: envdir must not equal toxinidir"101 assert re.match(102 r"ERROR: venv \'python\' in .* would delete project", result.outlines[0]103 ), result.outlines[0]104 result.assert_fail()105def test_envdir_would_delete_some_directory(cmd, initproj):106 projdir = initproj(107 "example-123",108 filedefs={109 "tox.ini": """\110 [tox]111 [testenv:venv]112 envdir=example113 commands=114 """115 },116 )117 result = cmd("-e", "venv")118 assert projdir.join("example/__init__.py").exists()119 result.assert_fail()120 assert "cowardly refusing to delete `envdir`" in result.out121def test_recreate(cmd, initproj):122 initproj("example-123", filedefs={"tox.ini": ""})123 cmd("-e", "py", "--notest").assert_success()124 cmd("-r", "-e", "py", "--notest").assert_success()125def test_run_custom_install_command_error(cmd, initproj):126 initproj(127 "interp123-0.5",128 filedefs={129 "tox.ini": """130 [testenv]131 install_command=./tox.ini {opts} {packages}132 """133 },134 )135 result = cmd()136 result.assert_fail()137 re.match(138 r"ERROR: python: InvocationError for command .* \(exited with code \d+\)",139 result.outlines[-1],140 ), result.out141def test_unknown_interpreter_and_env(cmd, initproj):142 initproj(143 "interp123-0.5",144 filedefs={145 "tests": {"test_hello.py": "def test_hello(): pass"},146 "tox.ini": """\147 [testenv:python]148 basepython=xyz_unknown_interpreter149 [testenv]150 changedir=tests151 skip_install = true152 """,153 },154 )155 result = cmd()156 result.assert_fail()157 assert "ERROR: InterpreterNotFound: xyz_unknown_interpreter" in result.outlines158 result = cmd("-exyz")159 result.assert_fail()160 assert result.out == "ERROR: unknown environment 'xyz'\n"161def test_unknown_interpreter_factor(cmd, initproj):162 initproj("py21", filedefs={"tox.ini": "[testenv]\nskip_install=true"})163 result = cmd("-e", "py21")164 result.assert_fail()165 assert "ERROR: InterpreterNotFound: python2.1" in result.outlines166def test_unknown_interpreter(cmd, initproj):167 initproj(168 "interp123-0.5",169 filedefs={170 "tests": {"test_hello.py": "def test_hello(): pass"},171 "tox.ini": """172 [testenv:python]173 basepython=xyz_unknown_interpreter174 [testenv]175 changedir=tests176 """,177 },178 )179 result = cmd()180 result.assert_fail()181 assert any(182 "ERROR: InterpreterNotFound: xyz_unknown_interpreter" == l for l in result.outlines183 ), result.outlines184def test_skip_platform_mismatch(cmd, initproj):185 initproj(186 "interp123-0.5",187 filedefs={188 "tests": {"test_hello.py": "def test_hello(): pass"},189 "tox.ini": """190 [testenv]191 changedir=tests192 platform=x123193 """,194 },195 )196 result = cmd()197 result.assert_success()198 assert any(199 "SKIPPED: python: platform mismatch ({!r} does not match 'x123')".format(sys.platform)200 == l201 for l in result.outlines202 ), result.outlines203def test_skip_unknown_interpreter(cmd, initproj):204 initproj(205 "interp123-0.5",206 filedefs={207 "tests": {"test_hello.py": "def test_hello(): pass"},208 "tox.ini": """209 [testenv:python]210 basepython=xyz_unknown_interpreter211 [testenv]212 changedir=tests213 """,214 },215 )216 result = cmd("--skip-missing-interpreters")217 result.assert_success()218 msg = "SKIPPED: python: InterpreterNotFound: xyz_unknown_interpreter"219 assert any(msg == l for l in result.outlines), result.outlines220def test_skip_unknown_interpreter_result_json(cmd, initproj, tmpdir):221 report_path = tmpdir.join("toxresult.json")222 initproj(223 "interp123-0.5",224 filedefs={225 "tests": {"test_hello.py": "def test_hello(): pass"},226 "tox.ini": """227 [testenv:python]228 basepython=xyz_unknown_interpreter229 [testenv]230 changedir=tests231 """,232 },233 )234 result = cmd("--skip-missing-interpreters", "--result-json", report_path)235 result.assert_success()236 msg = "SKIPPED: python: InterpreterNotFound: xyz_unknown_interpreter"237 assert any(msg == l for l in result.outlines), result.outlines238 setup_result_from_json = json.load(report_path)["testenvs"]["python"]["setup"]239 for setup_step in setup_result_from_json:240 assert "InterpreterNotFound" in setup_step["output"]241 assert setup_step["retcode"] == 0242def test_unknown_dep(cmd, initproj):243 initproj(244 "dep123-0.7",245 filedefs={246 "tests": {"test_hello.py": "def test_hello(): pass"},247 "tox.ini": """248 [testenv]249 deps=qweqwe123250 changedir=tests251 """,252 },253 )254 result = cmd()255 result.assert_fail()256 assert result.outlines[-1].startswith("ERROR: python: could not install deps [qweqwe123];")257def test_venv_special_chars_issue252(cmd, initproj):258 initproj(259 "pkg123-0.7",260 filedefs={261 "tests": {"test_hello.py": "def test_hello(): pass"},262 "tox.ini": """263 [tox]264 envlist = special&&1265 [testenv:special&&1]266 changedir=tests267 """,268 },269 )270 result = cmd()271 result.assert_success()272 pattern = re.compile("special&&1 installed: .*pkg123==0.7.*")273 assert any(pattern.match(line) for line in result.outlines), result.outlines274def test_unknown_environment(cmd, initproj):275 initproj("env123-0.7", filedefs={"tox.ini": ""})276 result = cmd("-e", "qpwoei")277 result.assert_fail()278 assert result.out == "ERROR: unknown environment 'qpwoei'\n"279def test_unknown_environment_with_envlist(cmd, initproj):280 initproj(281 "pkg123",282 filedefs={283 "tox.ini": """284 [tox]285 envlist = py{36,37}-django{20,21}286 """287 },288 )289 result = cmd("-e", "py36-djagno21")290 result.assert_fail()291 assert result.out == "ERROR: unknown environment 'py36-djagno21'\n"292def test_minimal_setup_py_empty(cmd, initproj):293 initproj(294 "pkg123-0.7",295 filedefs={296 "tests": {"test_hello.py": "def test_hello(): pass"},297 "setup.py": """298 """,299 "tox.ini": "",300 },301 )302 result = cmd()303 result.assert_fail()304 assert result.outlines[-1] == "ERROR: setup.py is empty"305def test_minimal_setup_py_comment_only(cmd, initproj):306 initproj(307 "pkg123-0.7",308 filedefs={309 "tests": {"test_hello.py": "def test_hello(): pass"},310 "setup.py": """\n# some comment311 """,312 "tox.ini": "",313 },314 )315 result = cmd()316 result.assert_fail()317 assert result.outlines[-1] == "ERROR: setup.py is empty"318def test_minimal_setup_py_non_functional(cmd, initproj):319 initproj(320 "pkg123-0.7",321 filedefs={322 "tests": {"test_hello.py": "def test_hello(): pass"},323 "setup.py": """324 import sys325 """,326 "tox.ini": "",327 },328 )329 result = cmd()330 result.assert_fail()331 assert any(re.match(r".*ERROR.*check setup.py.*", l) for l in result.outlines), result.outlines332def test_sdist_fails(cmd, initproj):333 initproj(334 "pkg123-0.7",335 filedefs={336 "tests": {"test_hello.py": "def test_hello(): pass"},337 "setup.py": """338 syntax error339 """,340 "tox.ini": "",341 },342 )343 result = cmd()344 result.assert_fail()345 assert any(346 re.match(r".*FAIL.*could not package project.*", l) for l in result.outlines347 ), result.outlines348def test_no_setup_py_exits(cmd, initproj):349 initproj(350 "pkg123-0.7",351 filedefs={352 "tox.ini": """353 [testenv]354 commands=python -c "2 + 2"355 """356 },357 )358 os.remove("setup.py")359 result = cmd()360 result.assert_fail()361 assert any(362 re.match(r".*ERROR.*No pyproject.toml or setup.py file found.*", l)363 for l in result.outlines364 ), result.outlines365def test_no_setup_py_exits_but_pyproject_toml_does(cmd, initproj):366 initproj(367 "pkg123-0.7",368 filedefs={369 "tox.ini": """370 [testenv]371 commands=python -c "2 + 2"372 """373 },374 )375 os.remove("setup.py")376 pathlib2.Path("pyproject.toml").touch()377 result = cmd()378 result.assert_fail()379 assert any(380 re.match(r".*ERROR.*pyproject.toml file found.*", l) for l in result.outlines381 ), result.outlines382 assert any(383 re.match(r".*To use a PEP 517 build-backend you are required to*", l)384 for l in result.outlines385 ), result.outlines386def test_package_install_fails(cmd, initproj):387 initproj(388 "pkg123-0.7",389 filedefs={390 "tests": {"test_hello.py": "def test_hello(): pass"},391 "setup.py": """392 from setuptools import setup393 setup(394 name='pkg123',395 description='pkg123 project',396 version='0.7',397 license='MIT',398 platforms=['unix', 'win32'],399 packages=['pkg123',],400 install_requires=['qweqwe123'],401 )402 """,403 "tox.ini": "",404 },405 )406 result = cmd()407 result.assert_fail()408 assert result.outlines[-1].startswith("ERROR: python: InvocationError for command ")409@pytest.fixture410def example123(initproj):411 yield initproj(412 "example123-0.5",413 filedefs={414 "tests": {415 "test_hello.py": """416 def test_hello(pytestconfig):417 pass418 """419 },420 "tox.ini": """421 [testenv]422 changedir=tests423 commands= pytest --basetemp={envtmpdir} \424 --junitxml=junit-{envname}.xml425 deps=pytest426 """,427 },428 )429def test_toxuone_env(cmd, example123):430 result = cmd()431 result.assert_success()432 assert re.match(433 r".*generated\W+xml\W+file.*junit-python\.xml" r".*\W+1\W+passed.*", result.out, re.DOTALL434 )435 result = cmd("-epython")436 result.assert_success()437 assert re.match(438 r".*\W+1\W+passed.*" r"summary.*" r"python:\W+commands\W+succeeded.*",439 result.out,440 re.DOTALL,441 )442def test_different_config_cwd(cmd, example123):443 # see that things work with a different CWD444 with example123.dirpath().as_cwd():445 result = cmd("-c", "example123/tox.ini")446 result.assert_success()447 assert re.match(448 r".*\W+1\W+passed.*" r"summary.*" r"python:\W+commands\W+succeeded.*",449 result.out,450 re.DOTALL,451 )452def test_result_json(cmd, initproj, example123):453 cwd = initproj(454 "example123",455 filedefs={456 "tox.ini": """457 [testenv]458 deps = setuptools459 commands_pre = python -c 'print("START")'460 commands = python -c 'print("OK")'461 - python -c 'print("1"); raise SystemExit(1)'462 python -c 'print("1"); raise SystemExit(2)'463 python -c 'print("SHOULD NOT HAPPEN")'464 commands_post = python -c 'print("END")'465 """466 },467 )468 json_path = cwd / "res.json"469 result = cmd("--result-json", json_path)470 result.assert_fail()471 data = json.loads(json_path.read_text(encoding="utf-8"))472 assert data["reportversion"] == "1"473 assert data["toxversion"] == tox.__version__474 for env_data in data["testenvs"].values():475 for command_type in ("setup", "test"):476 if command_type not in env_data:477 assert False, "missing {}".format(command_type)478 for command in env_data[command_type]:479 assert isinstance(command["command"], list)480 # builtin venv creation does not have output481 if command["command"][1:] != ['-m', 'venv', 'python']:482 assert command["output"]483 assert "retcode" in command484 assert isinstance(command["retcode"], int)485 # virtualenv, deps install, package install, freeze486 assert len(env_data["setup"]) == 4487 # 1 pre + 3 command + 1 post488 assert len(env_data["test"]) == 5489 assert isinstance(env_data["installed_packages"], list)490 pyinfo = env_data["python"]491 assert isinstance(pyinfo["version_info"], list)492 assert pyinfo["version"]493 assert pyinfo["executable"]494 assert "write json report at: {}".format(json_path) == result.outlines[-1]495def test_developz(initproj, cmd):496 initproj(497 "example123",498 filedefs={499 "tox.ini": """500 """501 },502 )503 result = cmd("-vv", "--develop")504 result.assert_success()505 assert "sdist-make" not in result.out506def test_usedevelop(initproj, cmd):507 initproj(508 "example123",509 filedefs={510 "tox.ini": """511 [testenv]512 usedevelop=True513 """514 },515 )516 result = cmd("-vv")517 result.assert_success()518 assert "sdist-make" not in result.out519def test_usedevelop_mixed(initproj, cmd):520 initproj(521 "example123",522 filedefs={523 "tox.ini": """524 [testenv:dev]525 usedevelop=True526 [testenv:nondev]527 usedevelop=False528 """529 },530 )531 # running only 'dev' should not do sdist532 result = cmd("-vv", "-e", "dev")533 result.assert_success()534 assert "sdist-make" not in result.out535 # running all envs should do sdist536 result = cmd("-vv")537 result.assert_success()538 assert "sdist-make" in result.out539@pytest.mark.parametrize("skipsdist", [False, True])540@pytest.mark.parametrize("src_root", [".", "src"])541def test_test_usedevelop(cmd, initproj, src_root, skipsdist):542 name = "example123-spameggs"543 base = initproj(544 (name, "0.5"),545 src_root=src_root,546 filedefs={547 "tests": {548 "test_hello.py": """549 def test_hello(pytestconfig):550 pass551 """552 },553 "tox.ini": """554 [testenv]555 usedevelop=True556 changedir=tests557 commands=558 pytest --basetemp={envtmpdir} --junitxml=junit-{envname}.xml []559 deps=pytest"""560 + """561 skipsdist={}562 """.format(563 skipsdist564 ),565 },566 )567 result = cmd("-v")568 result.assert_success()569 assert re.match(570 r".*generated\W+xml\W+file.*junit-python\.xml" r".*\W+1\W+passed.*", result.out, re.DOTALL571 )572 assert "sdist-make" not in result.out573 result = cmd("-epython")574 result.assert_success()575 assert "develop-inst-noop" in result.out576 assert re.match(577 r".*\W+1\W+passed.*" r"summary.*" r"python:\W+commands\W+succeeded.*",578 result.out,579 re.DOTALL,580 )581 # see that things work with a different CWD582 with base.dirpath().as_cwd():583 result = cmd("-c", "{}/tox.ini".format(name))584 result.assert_success()585 assert "develop-inst-noop" in result.out586 assert re.match(587 r".*\W+1\W+passed.*" r"summary.*" r"python:\W+commands\W+succeeded.*",588 result.out,589 re.DOTALL,590 )591 # see that tests can also fail and retcode is correct592 testfile = py.path.local("tests").join("test_hello.py")593 assert testfile.check()594 testfile.write("def test_fail(): assert 0")595 result = cmd()596 result.assert_fail()597 assert "develop-inst-noop" in result.out598 assert re.match(599 r".*\W+1\W+failed.*" r"summary.*" r"python:\W+commands\W+failed.*", result.out, re.DOTALL600 )601 # test develop is called if setup.py changes602 setup_py = py.path.local("setup.py")603 setup_py.write(setup_py.read() + " ")604 result = cmd()605 result.assert_fail()606 assert "develop-inst-nodeps" in result.out607def test_warning_emitted(cmd, initproj):608 initproj(609 "spam-0.0.1",610 filedefs={611 "tox.ini": """612 [testenv]613 skipsdist=True614 usedevelop=True615 """,616 "setup.py": """617 from setuptools import setup618 from warnings import warn619 warn("I am a warning")620 setup(name="spam", version="0.0.1")621 """,622 },623 )624 cmd()625 result = cmd()626 assert "develop-inst-noop" in result.out627 assert "I am a warning" in result.err628def test_alwayscopy(initproj, cmd, mocksession):629 initproj(630 "example123",631 filedefs={632 "tox.ini": """633 [testenv]634 commands={envpython} --version635 alwayscopy=True636 """637 },638 )639 venv = mocksession.getvenv("python")640 result = cmd("-vv")641 result.assert_success()642 if use_builtin_venv(venv):643 assert "venv --copies" in result.out644 else:645 assert "virtualenv --always-copy" in result.out646def test_alwayscopy_default(initproj, cmd, mocksession):647 initproj(648 "example123",649 filedefs={650 "tox.ini": """651 [testenv]652 commands={envpython} --version653 """654 },655 )656 venv = mocksession.getvenv("python")657 result = cmd("-vv")658 result.assert_success()659 if use_builtin_venv(venv):660 assert "venv --copies" not in result.out661 else:662 assert "virtualenv --always-copy" not in result.out663@pytest.mark.skipif("sys.platform == 'win32'", reason="no echo on Windows")664def test_empty_activity_ignored(initproj, cmd):665 initproj(666 "example123",667 filedefs={668 "tox.ini": """669 [testenv]670 list_dependencies_command=echo671 commands={envpython} --version672 """673 },674 )675 result = cmd()676 result.assert_success()677 assert "installed:" not in result.out678@pytest.mark.skipif("sys.platform == 'win32'", reason="no echo on Windows")679def test_empty_activity_shown_verbose(initproj, cmd):680 initproj(681 "example123",682 filedefs={683 "tox.ini": """684 [testenv]685 list_dependencies_command=echo686 commands={envpython} --version687 whitelist_externals = echo688 """689 },690 )691 result = cmd("-v")692 result.assert_success()693 assert "installed:" in result.out694def test_test_piphelp(initproj, cmd):695 initproj(696 "example123",697 filedefs={698 "tox.ini": """699 # content of: tox.ini700 [testenv]701 commands=pip -h702 """703 },704 )705 result = cmd("-vv")706 result.assert_success()707def test_notest(initproj, cmd):708 initproj(709 "example123",710 filedefs={711 "tox.ini": """\712 # content of: tox.ini713 [testenv:py26]714 basepython={}715 """.format(716 sys.executable717 )718 },719 )720 result = cmd("-v", "--notest")721 result.assert_success()722 assert re.match(r".*summary.*" r"py26\W+skipped\W+tests.*", result.out, re.DOTALL)723 result = cmd("-v", "--notest", "-epy26")724 result.assert_success()725 assert re.match(r".*py26\W+reusing.*", result.out, re.DOTALL)726def test_notest_setup_py_error(initproj, cmd):727 initproj(728 "example123",729 filedefs={730 "setup.py": """\731 from setuptools import setup732 setup(name='x', install_requires=['fakefakefakefakefakefake']),733 """,734 "tox.ini": "",735 },736 )737 result = cmd("--notest")738 result.assert_fail()739 assert re.search("ERROR:.*InvocationError", result.out)740def test_devenv(initproj, cmd):741 initproj(742 "example123",743 filedefs={744 "setup.py": """\745 from setuptools import setup746 setup(name='x')747 """,748 "tox.ini": """\749 [tox]750 # envlist is ignored for --devenv751 envlist = foo,bar,baz752 [testenv]753 # --devenv implies --notest754 commands = python -c "exit(1)"755 """,756 },757 )758 result = cmd("--devenv", "venv")759 result.assert_success()760 # `--devenv` defaults to the `py` environment and a develop install761 assert "py develop-inst:" in result.out762 assert re.search("py create:.*venv", result.out)763def test_devenv_does_not_allow_multiple_environments(initproj, cmd):764 initproj(765 "example123",766 filedefs={767 "setup.py": """\768 from setuptools import setup769 setup(name='x')770 """,771 "tox.ini": """\772 [tox]773 envlist=foo,bar,baz774 """,775 },776 )777 result = cmd("--devenv", "venv", "-e", "foo,bar")778 result.assert_fail()779 assert result.err == "ERROR: --devenv requires only a single -e\n"780def test_devenv_does_not_delete_project(initproj, cmd):781 initproj(782 "example123",783 filedefs={784 "setup.py": """\785 from setuptools import setup786 setup(name='x')787 """,788 "tox.ini": """\789 [tox]790 envlist=foo,bar,baz791 """,792 },793 )794 result = cmd("--devenv", "")795 result.assert_fail()796 assert "would delete project" in result.out797 assert "ERROR: ConfigError: envdir must not equal toxinidir" in result.out798def test_PYC(initproj, cmd, monkeypatch):799 initproj("example123", filedefs={"tox.ini": ""})800 monkeypatch.setenv("PYTHONDOWNWRITEBYTECODE", "1")801 result = cmd("-v", "--notest")802 result.assert_success()803 assert "create" in result.out804def test_env_VIRTUALENV_PYTHON(initproj, cmd, monkeypatch):805 initproj("example123", filedefs={"tox.ini": ""})806 monkeypatch.setenv("VIRTUALENV_PYTHON", "/FOO")807 result = cmd("-v", "--notest")808 result.assert_success()809 assert "create" in result.out810def test_setup_prints_non_ascii(initproj, cmd):811 initproj(812 "example123",813 filedefs={814 "setup.py": """\815import sys816getattr(sys.stdout, 'buffer', sys.stdout).write(b'\\xe2\\x98\\x83\\n')817import setuptools818setuptools.setup(name='example123')819""",820 "tox.ini": "",821 },822 )823 result = cmd("--notest")824 result.assert_success()825 assert "create" in result.out826def test_envsitepackagesdir(cmd, initproj):827 initproj(828 "pkg512-0.0.5",829 filedefs={830 "tox.ini": """831 [testenv]832 commands=833 python -c "print(r'X:{envsitepackagesdir}')"834 """835 },836 )837 result = cmd()838 result.assert_success()839 assert re.match(r".*\nX:.*tox.*site-packages.*", result.out, re.DOTALL)840def test_envsitepackagesdir_skip_missing_issue280(cmd, initproj):841 initproj(842 "pkg513-0.0.5",843 filedefs={844 "tox.ini": """845 [testenv]846 basepython=/usr/bin/qwelkjqwle847 commands=848 {envsitepackagesdir}849 """850 },851 )852 result = cmd("--skip-missing-interpreters")853 result.assert_success()854 assert re.match(r".*SKIPPED:.*qwelkj.*", result.out, re.DOTALL)855@pytest.mark.parametrize("verbosity", ["", "-v", "-vv"])856def test_verbosity(cmd, initproj, verbosity):857 initproj(858 "pkgX-0.0.5",859 # Note: This is related to https://github.com/tox-dev/tox#935860 # For some reason, the .egg-info/ directory is interacting with the861 # PYTHONPATH on Appveyor, causing the package to *not* be installed862 # since pip already thinks it is. By setting the `src_root`, we can863 # avoid the issue.864 src_root="src",865 filedefs={866 "tox.ini": """867 [testenv]868 """869 },870 )871 result = cmd(verbosity)872 result.assert_success()873 needle = "Successfully installed pkgX-0.0.5"874 if verbosity == "-vv":875 assert any(needle in line for line in result.outlines), result.outlines876 else:877 assert all(needle not in line for line in result.outlines), result.outlines878def test_envtmpdir(initproj, cmd):879 initproj(880 "foo",881 filedefs={882 # This file first checks that envtmpdir is existent and empty. Then it883 # creates an empty file in that directory. The tox command is run884 # twice below, so this is to test whether the directory is cleared885 # before the second run.886 "check_empty_envtmpdir.py": """if True:887 import os888 from sys import argv889 envtmpdir = argv[1]890 assert os.path.exists(envtmpdir)891 assert os.listdir(envtmpdir) == []892 open(os.path.join(envtmpdir, 'test'), 'w').close()893 """,894 "tox.ini": """895 [testenv]896 commands=python check_empty_envtmpdir.py {envtmpdir}897 """,898 },899 )900 result = cmd()901 result.assert_success()902 result = cmd()903 result.assert_success()904def test_missing_env_fails(initproj, cmd):905 initproj("foo", filedefs={"tox.ini": "[testenv:foo]\ncommands={env:VAR}"})906 result = cmd()907 result.assert_fail()908 assert result.out.endswith(909 "foo: unresolvable substitution(s): 'VAR'."910 " Environment variables are missing or defined recursively.\n"911 )912def test_tox_console_script(initproj):913 initproj("help", filedefs={"tox.ini": ""})914 result = subprocess.check_call(["tox", "--help"])915 assert result == 0916def test_tox_quickstart_script(initproj):917 initproj("help", filedefs={"tox.ini": ""})918 result = subprocess.check_call(["tox-quickstart", "--help"])919 assert result == 0920def test_tox_cmdline_no_args(monkeypatch, initproj):921 initproj("help", filedefs={"tox.ini": ""})922 monkeypatch.setattr(sys, "argv", ["caller_script", "--help"])923 with pytest.raises(SystemExit):924 tox.cmdline()925def test_tox_cmdline_args(initproj):926 initproj("help", filedefs={"tox.ini": ""})927 with pytest.raises(SystemExit):928 tox.cmdline(["caller_script", "--help"])929@pytest.mark.parametrize("exit_code", [0, 6])930def test_exit_code(initproj, cmd, exit_code, mocker):931 """ Check for correct InvocationError, with exit code,932 except for zero exit code """933 import tox.exception934 mocker.spy(tox.exception, "exit_code_str")935 tox_ini_content = "[testenv:foo]\ncommands=python -c 'import sys; sys.exit({:d})'".format(936 exit_code937 )938 initproj("foo", filedefs={"tox.ini": tox_ini_content})939 cmd()940 if exit_code:941 # need mocker.spy above942 assert tox.exception.exit_code_str.call_count == 1943 (args, kwargs) = tox.exception.exit_code_str.call_args944 assert kwargs == {}945 (call_error_name, call_command, call_exit_code) = args946 assert call_error_name == "InvocationError"947 # quotes are removed in result.out948 # do not include "python" as it is changed to python.EXE by appveyor949 expected_command_arg = " -c 'import sys; sys.exit({:d})'".format(exit_code)950 assert expected_command_arg in call_command951 assert call_exit_code == exit_code952 else:...
test_tox_direct.py
Source:test_tox_direct.py
...71 assert direct.basepython == sys.executable72 assert direct.envpython == sys.executable73 assert direct.get_envbindir() == str(Path(sys.executable).parent)74def test_does_not_interfere_with_single_normal_env(cmd, initproj):75 initproj(76 "does_not_interfer_with_normal_operation",77 filedefs={78 "tox.ini": """79 [testenv:normal]80 deps = decorator81 commands = 82 pip list83 python -c 'import sys; print(sys.executable);'84 """85 },86 )87 r = cmd()88 assert r.ret == 089 assert not r.session.config.option.direct90 assert not r.session.config.option.direct_yolo91 assert "We need direct action" not in r.out92 assert "won't build a package" not in r.out93 assert "won't install dependencies" not in r.out94 assert "won't install project" not in r.out95 assert "decorator" in r.out96 assert "congratulations :)" in r.out97 assert sys.executable not in r.out98def test_mixed_run_crashes_when_normal_env_needs_package(cmd, initproj):99 projectName = "example_project-1.3"100 initproj(101 projectName,102 filedefs={103 "tox.ini": """104 [testenv:direct]105 deps = decorator106 commands = 107 pip list108 python -c 'import sys; print(sys.executable);'109 [testenv:normal]110 deps = decorator111 commands = 112 pip list113 python -c 'import sys; print(sys.executable);'114 """115 },116 )117 r = cmd()118 assert r.ret == 1119 assert "[tox-direct] FATAL: tox env 'normal' needs a package" in r.err120def test_mixed_run_does_not_crash_when_normal_env_needs_no_package(cmd, initproj):121 projectName = "example_project-1.3"122 initproj(123 projectName,124 filedefs={125 "tox.ini": """126 [testenv:direct]127 deps = decorator128 commands = 129 pip list130 python -c 'import sys; print(sys.executable);'131 [testenv:normal]132 usedevelop = True133 deps = decorator134 commands = 135 pip list136 python -c 'import sys; print(sys.executable);'137 """138 },139 )140 r = cmd()141 assert r.ret == 0142 assert "example-project 1.3" in r.out143def test_direct_vertical(cmd, initproj):144 initproj(145 "direct_vertical",146 filedefs={147 "tox.ini": """148 [testenv:direct]149 deps = dontcare150 commands = python -c 'import sys; print(sys.executable);'151 """152 },153 )154 r = cmd("tox", "-vv")155 assert r.ret == 0156 assert not r.session.config.option.direct157 assert not r.session.config.option.direct_yolo158 assert "won't build a package" in r.out159 assert "won't install dependencies" in r.out160 assert "won't install project" in r.out161 assert "creating no virtual environment" in r.out162 assert "congratulations :)" in r.out163 assert sys.executable in r.out164def test_direct_yolo_normal_vertical(cmd, initproj):165 initproj(166 "yolo_normal_vertical",167 filedefs={168 "tox.ini": """169 [testenv:normal]170 deps = decorator171 commands = 172 pip list173 python -c 'import sys; print(sys.executable);'174 175 """176 },177 )178 r = cmd("tox", "-vv", "--direct-yolo")179 r.out = "\n".join(r.outlines)...
test_plugin.py
Source:test_plugin.py
1# Copyright (c) 2015 BalaBit2# All Rights Reserved.3pytest_plugins = "pytester"4def test_invalid_debian_package_name_cannot_be_fetched(cmd, initproj):5 initproj("debian123-0.42", filedefs={6 'tox.ini': '''7 [testenv]8 debian_deps=9 no-such-debian-package=123.4210 '''11 })12 result = cmd.run("tox", )13 result.stdout.fnmatch_lines([14 "E: Unable to locate package no-such-debian-package",15 ])16 assert result.ret17def test_debian_package_will_be_extracted_into_virtual_env(cmd, initproj):18 initproj("debian123-0.56", filedefs={19 'tox.ini': '''20 [testenv]21 debian_deps=22 graphviz23 commands= ls -1 .tox/python/bin24 '''25 })26 result = cmd.run("tox", )27 result.stdout.fnmatch_lines(["dot"])28 assert result.ret == 029def test_can_extract_multiple_packages(cmd, initproj):30 initproj("debian123-0.56", filedefs={31 'tox.ini': '''32 [testenv]33 debian_deps=34 graphviz35 vim36 commands= ls -1 .tox/python/bin37 '''38 })39 result = cmd.run("tox", )40 result.stdout.fnmatch_lines(["dot", "vim*"])41 assert result.ret == 042def test_empty_debian_dependency_dont_call_apt_get(cmd, initproj):43 initproj("debian123-0.56", filedefs={44 'tox.ini': '''45 [testenv]46 debian_deps=47 '''48 })49 result = cmd.run("tox", )50 assert 'apt-get' not in result.stdout.str()51 assert result.ret == 052def test_can_pass_additional_options_to_apt_get(cmd, initproj):53 initproj("debian123-0.56", filedefs={54 'tox.ini': '''55 [testenv]56 apt_opts=57 --no-such-option58 debian_deps=59 graphviz60 '''61 })62 result = cmd.run("tox", )63 result.stdout.fnmatch_lines(["*no-such-option*"])64 assert result.ret65def test_install32_logs_its_actions(cmd, initproj):66 assert_logs_actions(cmd, initproj, "py32")67def test_install27_logs_its_actions(cmd, initproj):68 assert_logs_actions(cmd, initproj, "py27")69def assert_logs_actions(cmd, initproj, venv_name):70 initproj("debian123-0.56", filedefs={71 'tox.ini': '''72 [tox]73 envlist={venv_name}74 [testenv]75 debian_deps=76 vim77 graphviz78 '''.format(venv_name=venv_name)79 })80 result = cmd.run("tox", )81 result.stdout.fnmatch_lines([82 "{} apt-get download: vim, graphviz".format(venv_name),83 "{} dpkg extract: graphviz*".format(venv_name),84 "{} copy: *bin/dot*".format(venv_name)...
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!!