Best Python code snippet using autotest_python
test_jython_launcher.py
Source:test_jython_launcher.py
...38 return _uname39def classpath_delimiter():40 return ";" if (os._name == "nt" or uname == "cygwin") else ":"41class TestLauncher(unittest.TestCase):42 def get_cmdline(self, cmd, env):43 output = subprocess.check_output(cmd, env=env).rstrip()44 if is_windows:45 return subprocess._cmdline2list(output)46 else:47 return shlex.split(output)48 def get_newenv(self, env=os.environ):49 newenv = env.copy()50 for var in ("CLASSPATH",51 "JAVA_MEM", "JAVA_HOME", "JAVA_OPTS", "JAVA_STACK",52 "JYTHON_HOME", "JYTHON_OPTS"):53 try:54 del newenv[var]55 except KeyError:56 pass57 return newenv58 def get_properties(self, args):59 props = OrderedDict()60 for arg in args:61 if arg.startswith("-D"):62 k, v = arg[2:].split("=")63 props[k] = v64 return props65 def test_classpath_env(self):66 env = self.get_newenv()67 env["CLASSPATH"] = some_jar68 args = self.get_cmdline([launcher, "--print"], env=env)69 it = iter(args)70 while it:71 arg = next(it)72 if arg == "-classpath":73 self.assertEqual(next(it).split(classpath_delimiter())[-1], some_jar)74 break75 def test_classpath(self):76 env = self.get_newenv()77 args = self.get_cmdline([launcher, "--print", "-J-cp", some_jar], env=env)78 it = iter(args)79 while it:80 arg = next(it)81 if arg == "-classpath":82 self.assertEqual(next(it).split(classpath_delimiter())[-1], some_jar)83 break84 def test_java_home(self):85 env = self.get_newenv()86 my_java = os.path.join(os.sep, "foo", "bar", "my very own (x86) java")87 env["JAVA_HOME"] = my_java88 args = self.get_cmdline([launcher, "--print"], env)89 self.assertEqual(args[0], os.path.join(my_java, "bin", "java"))90 self.assertEqual(args[1], "-Xmx512m")91 self.assertEqual(args[2], "-Xss2560k")92 self.assertEqual(args[-1], "org.python.util.jython")93 def test_java_opts(self):94 env = self.get_newenv()95 env["JAVA_OPTS"] = '-Dfoo=bar -Dbaz="some property" -Xmx2g -classpath %s' % some_jar96 args = self.get_cmdline([launcher, "--print"], env)97 props = self.get_properties(args)98 self.assertEqual(args[0], "java")99 self.assertEqual(args[1], "-Xmx2g")100 self.assertEqual(args[2], "-Xss2560k")101 self.assertEqual(args[3], "-classpath", args)102 self.assertEqual(args[4].split(classpath_delimiter())[-1], some_jar)103 self.assertEqual(args[-1], "org.python.util.jython")104 self.assertEqual(props["foo"], "bar")105 self.assertEqual(props["baz"], "some property")106 def test_default_options(self):107 env = self.get_newenv()108 args = self.get_cmdline([launcher, "--print"], env)109 props = self.get_properties(args)110 self.assertEqual(args[0], "java")111 self.assertEqual(args[1], "-Xmx512m")112 self.assertEqual(args[2], "-Xss2560k")113 self.assertEqual(args[-1], "org.python.util.jython")114 self.assertIn("python.home", props)115 self.assertIn("python.executable", props)116 self.assertIn("python.launcher.uname", props)117 self.assertIn("python.launcher.tty", props)118 def test_mem_env(self):119 env = self.get_newenv()120 env["JAVA_MEM"] = "-Xmx4g"121 env["JAVA_STACK"] = "-Xss2m"122 args = self.get_cmdline([launcher, "--print"], env)123 self.assertEqual(args[0], "java")124 self.assertEqual(args[1], "-Xmx4g")125 self.assertEqual(args[2], "-Xss2m")126 self.assertEqual(args[-1], "org.python.util.jython")127 def test_mem_options(self):128 env = self.get_newenv()129 args = self.get_cmdline([launcher, "-J-Xss2m", "-J-Xmx4g", "--print"], env)130 self.assertEqual(args[0], "java")131 self.assertEqual(args[1], "-Xmx4g", args)132 self.assertEqual(args[2], "-Xss2m", args)133 self.assertEqual(args[-1], "org.python.util.jython")134 def test_jython_opts_env(self):135 env = self.get_newenv()136 env["JYTHON_OPTS"] = '-c "print 47"'137 args = self.get_cmdline([launcher, "--print"], env)138 self.assertEqual(args[0], "java")139 self.assertEqual(args[1], "-Xmx512m")140 self.assertEqual(args[2], "-Xss2560k")141 self.assertEqual(args[-3], "org.python.util.jython")142 self.assertEqual(args[-2], "-c")143 self.assertEqual(args[-1], "print 47")144 def test_options(self):145 env = self.get_newenv()146 args = self.get_cmdline(147 [launcher,148 "-Dquoted=a \"quoted\" option",149 "-Dunder_score=with_underscores",150 "-Dstarred=*/*/more/*/*",151 "--print"], env)152 props = self.get_properties(args)153 self.assertEqual(props["quoted"], 'a "quoted" option')154 self.assertEqual(props["under_score"], "with_underscores")155 self.assertEqual(props["starred"], "*/*/more/*/*")156 def assertHelp(self, output):157 self.assertIn(158 "usage: jython [option] ... [-c cmd | -m mod | file | -] [arg] ...",159 output)160 def test_help(self):161 self.assertHelp(subprocess.check_output([launcher, "--help"], stderr=subprocess.STDOUT))162 self.assertHelp(subprocess.check_output([launcher, "--print", "--help"], stderr=subprocess.STDOUT))163 self.assertHelp(subprocess.check_output([launcher, "--help", "--jdb"], stderr=subprocess.STDOUT))164 with self.assertRaises(subprocess.CalledProcessError) as cm:165 subprocess.check_output([launcher, "--bad-arg"], stderr=subprocess.STDOUT)166 self.assertHelp(cm.exception.output)167 def test_remaining_args(self):168 env = self.get_newenv()169 args = self.get_cmdline([launcher, "--print", "--", "--help"], env)170 self.assertEqual(args[-2], "org.python.util.jython")171 self.assertEqual(args[-1], "--help")172 args = self.get_cmdline([launcher, "--print", "yolk", "--help"], env)173 self.assertEqual(args[-3], "org.python.util.jython")174 self.assertEqual(args[-2], "yolk")175 self.assertEqual(args[-1], "--help")176 def assertCommand(self, command):177 args = self.get_cmdline([launcher, "--print"] + command, self.get_newenv())178 self.assertEqual(args[(len(args) - len(command)):], command)179 def test_file(self):180 self.assertCommand(['test.py'])181 def test_dash(self):182 self.assertCommand(['-i'])183 def test_combined(self):184 self.assertCommand(['-W', 'action', 'line'])185 def test_singlequoted(self):186 self.assertCommand(['-c', "'import sys;'"])187 def test_doublequoted(self):188 self.assertCommand(['-c', '"print \'something\'"'])189 def test_nestedquotes(self):190 self.assertCommand(['-c', '"print \'something \"really\" cool\'"'])191 def test_nestedquotes2(self):...
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!!