Best Python code snippet using tox_python
test_quickstart.py
Source:test_quickstart.py
1import os2import pytest3import tox4from tox._quickstart import (5 ALTERNATIVE_CONFIG_NAME,6 QUICKSTART_CONF,7 list_modificator,8 main,9 post_process_input,10 prepare_content,11)12ALL_PY_ENVS_AS_STRING = ", ".join(tox.PYTHON.QUICKSTART_PY_ENVS)13ALL_PY_ENVS_WO_LAST_AS_STRING = ", ".join(tox.PYTHON.QUICKSTART_PY_ENVS[:-1])14SIGNS_OF_SANITY = (15 "tox.readthedocs.io",16 "[tox]",17 "[testenv]",18 "envlist = ",19 "deps =",20 "commands =",21)22# A bunch of elements to be expected in the generated config as marker for basic sanity23class _answers:24 """Simulate a series of terminal inputs by popping them from a list if called."""25 def __init__(self, inputs):26 self._inputs = [str(i) for i in inputs]27 def extend(self, items):28 self._inputs.extend(items)29 def __str__(self):30 return "|".join(self._inputs)31 def __call__(self, prompt):32 print("prompt: '{}'".format(prompt))33 try:34 answer = self._inputs.pop(0)35 print("user answer: '{}'".format(answer))36 return answer37 except IndexError:38 pytest.fail("missing user answer for '{}'".format(prompt))39class _cnf:40 """Handle files and args for different test scenarios."""41 SOME_CONTENT = "dontcare"42 def __init__(self, exists=False, names=None, pass_path=False):43 self.original_name = tox.INFO.DEFAULT_CONFIG_NAME44 self.names = names or [ALTERNATIVE_CONFIG_NAME]45 self.exists = exists46 self.pass_path = pass_path47 def __str__(self):48 return self.original_name if not self.exists else str(self.names)49 @property50 def argv(self):51 argv = ["tox-quickstart"]52 if self.pass_path:53 argv.append(os.getcwd())54 return argv55 @property56 def dpath(self):57 return os.getcwd() if self.pass_path else ""58 def create(self):59 paths_to_create = {self._original_path}60 for name in self.names[:-1]:61 paths_to_create.add(os.path.join(self.dpath, name))62 for path in paths_to_create:63 with open(path, "w") as f:64 f.write(self.SOME_CONTENT)65 @property66 def generated_content(self):67 return self._alternative_content if self.exists else self._original_content68 @property69 def already_existing_content(self):70 if not self.exists:71 if os.path.exists(self._alternative_path):72 pytest.fail("alternative path should never exist here")73 pytest.fail("checking for already existing content makes not sense here")74 return self._original_content75 @property76 def path_to_generated(self):77 return os.path.join(os.getcwd(), self.names[-1] if self.exists else self.original_name)78 @property79 def _original_path(self):80 return os.path.join(self.dpath, self.original_name)81 @property82 def _alternative_path(self):83 return os.path.join(self.dpath, self.names[-1])84 @property85 def _original_content(self):86 with open(self._original_path) as f:87 return f.read()88 @property89 def _alternative_content(self):90 with open(self._alternative_path) as f:91 return f.read()92class _exp:93 """Holds test expectations and a user scenario description."""94 STANDARD_EPECTATIONS = [ALL_PY_ENVS_AS_STRING, "pytest", "pytest"]95 def __init__(self, name, exp=None):96 self.name = name97 exp = exp or self.STANDARD_EPECTATIONS98 # NOTE extra mangling here ensures formatting is the same in file and exp99 map_ = {"deps": list_modificator(exp[1]), "commands": list_modificator(exp[2])}100 post_process_input(map_)101 map_["envlist"] = exp[0]102 self.content = prepare_content(QUICKSTART_CONF.format(**map_))103 def __str__(self):104 return self.name105@pytest.mark.usefixtures("work_in_clean_dir")106@pytest.mark.parametrize(107 argnames="answers, exp, cnf",108 ids=lambda param: str(param),109 argvalues=(110 (111 _answers([4, "Y", "Y", "Y", "Y", "Y", "N", "pytest", "pytest"]),112 _exp(113 "choose versions individually and use pytest",114 [ALL_PY_ENVS_WO_LAST_AS_STRING, "pytest", "pytest"],115 ),116 _cnf(),117 ),118 (119 _answers([4, "Y", "Y", "Y", "Y", "Y", "N", "py.test", ""]),120 _exp(121 "choose versions individually and use old fashioned py.test",122 [ALL_PY_ENVS_WO_LAST_AS_STRING, "pytest", "py.test"],123 ),124 _cnf(),125 ),126 (127 _answers([1, "pytest", ""]),128 _exp(129 "choose current release Python and pytest with defaut deps",130 [tox.PYTHON.CURRENT_RELEASE_ENV, "pytest", "pytest"],131 ),132 _cnf(),133 ),134 (135 _answers([1, "pytest -n auto", "pytest-xdist"]),136 _exp(137 "choose current release Python and pytest with xdist and some args",138 [tox.PYTHON.CURRENT_RELEASE_ENV, "pytest, pytest-xdist", "pytest -n auto"],139 ),140 _cnf(),141 ),142 (143 _answers([2, "pytest", ""]),144 _exp(145 "choose py27, current release Python and pytest with defaut deps",146 ["py27, {}".format(tox.PYTHON.CURRENT_RELEASE_ENV), "pytest", "pytest"],147 ),148 _cnf(),149 ),150 (151 _answers([3, "pytest", ""]),152 _exp("choose all supported version and pytest with defaut deps"),153 _cnf(),154 ),155 (156 _answers([4, "Y", "Y", "Y", "Y", "Y", "N", "py.test", ""]),157 _exp(158 "choose versions individually and use old fashioned py.test",159 [ALL_PY_ENVS_WO_LAST_AS_STRING, "pytest", "py.test"],160 ),161 _cnf(),162 ),163 (164 _answers([4, "", "", "", "", "", "", "", ""]),165 _exp("choose no version individually and defaults"),166 _cnf(),167 ),168 (169 _answers([4, "Y", "Y", "Y", "Y", "Y", "N", "python -m unittest discover", ""]),170 _exp(171 "choose versions individually and use nose with default deps",172 [ALL_PY_ENVS_WO_LAST_AS_STRING, "", "python -m unittest discover"],173 ),174 _cnf(),175 ),176 (177 _answers([4, "Y", "Y", "Y", "Y", "Y", "N", "nosetests", "nose"]),178 _exp(179 "choose versions individually and use nose with default deps",180 [ALL_PY_ENVS_WO_LAST_AS_STRING, "nose", "nosetests"],181 ),182 _cnf(),183 ),184 (185 _answers([4, "Y", "Y", "Y", "Y", "Y", "N", "trial", ""]),186 _exp(187 "choose versions individually and use twisted tests with default deps",188 [ALL_PY_ENVS_WO_LAST_AS_STRING, "twisted", "trial"],189 ),190 _cnf(),191 ),192 (193 _answers([4, "", "", "", "", "", "", "", ""]),194 _exp("existing not overridden, generated to alternative with default name"),195 _cnf(exists=True),196 ),197 (198 _answers([4, "", "", "", "", "", "", "", ""]),199 _exp("existing not overridden, generated to alternative with custom name"),200 _cnf(exists=True, names=["some-other.ini"]),201 ),202 (203 _answers([4, "", "", "", "", "", "", "", ""]),204 _exp("existing not override, generated to alternative"),205 _cnf(exists=True, names=["tox.ini", "some-other.ini"]),206 ),207 (208 _answers([4, "", "", "", "", "", "", "", ""]),209 _exp("existing alternatives are not overridden, generated to alternative"),210 _cnf(exists=True, names=["tox.ini", "setup.py", "some-other.ini"]),211 ),212 ),213)214def test_quickstart(answers, cnf, exp, monkeypatch):215 """Test quickstart script using some little helpers.216 :param _answers answers: user interaction simulation217 :param _cnf cnf: helper for args and config file paths and contents218 :param _exp exp: expectation helper219 """220 monkeypatch.setattr("six.moves.input", answers)221 monkeypatch.setattr("sys.argv", cnf.argv)222 if cnf.exists:223 answers.extend(cnf.names)224 cnf.create()225 main()226 print("generated config at {}:\n{}\n".format(cnf.path_to_generated, cnf.generated_content))227 check_basic_sanity(cnf.generated_content, SIGNS_OF_SANITY)228 assert cnf.generated_content == exp.content229 if cnf.exists:230 assert cnf.already_existing_content == cnf.SOME_CONTENT231def check_basic_sanity(content, signs):232 for sign in signs:233 if sign not in content:...
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!!