Best Python code snippet using avocado_python
test_nrunner.py
Source:test_nrunner.py
...6from avocado.utils.network.ports import find_free_port7from .. import AVOCADO, BASEDIR, TestCaseTmpDir, skipUnlessPathExists8RUNNER = "%s -m avocado.core.nrunner" % sys.executable9class NRunnerFeatures(unittest.TestCase):10 @skipUnlessPathExists('/bin/false')11 def test_custom_exit_codes(self):12 status_server = "127.0.0.1:%u" % find_free_port()13 config = {'run.references': ['/bin/false'],14 'run.test_runner': 'nrunner',15 'runner.exectest.exitcodes.skip': [1],16 'nrunner.status_server_listen': status_server,17 'nrunner.status_server_uri': status_server,18 'run.keep_tmp': True}19 with Job.from_config(job_config=config) as job:20 self.assertEqual(job.run(), 0)21class RunnableRun(unittest.TestCase):22 def test_noop(self):23 res = process.run("%s runnable-run -k noop" % RUNNER,24 ignore_status=True)25 self.assertIn(b"'status': 'started'", res.stdout)26 self.assertIn(b"'status': 'finished'", res.stdout)27 self.assertIn(b"'time': ", res.stdout)28 self.assertEqual(res.exit_status, 0)29 def test_exec(self):30 # 'base64:LWM=' becomes '-c' and makes Python execute the31 # commands on the subsequent argument32 cmd = ("%s runnable-run -k exec -u %s -a 'base64:LWM=' -a "33 "'import sys; sys.exit(99)'" % (RUNNER, sys.executable))34 res = process.run(cmd, ignore_status=True)35 self.assertIn(b"'status': 'finished'", res.stdout)36 self.assertIn(b"'returncode': 99", res.stdout)37 self.assertEqual(res.exit_status, 0)38 @skipUnlessPathExists('/bin/sh')39 def test_exec_echo(self):40 # 'base64:LW4=' becomes '-n' and prevents echo from printing a newline41 cmd = ("%s runnable-run -k exec -u /bin/echo -a 'base64:LW4=' -a "42 "_Avocado_Runner_" % RUNNER)43 res = process.run(cmd, ignore_status=True)44 self.assertIn(b"'status': 'finished'", res.stdout)45 self.assertIn(b"'stdout': b'_Avocado_Runner_'", res.stdout)46 self.assertIn(b"'returncode': 0", res.stdout)47 self.assertEqual(res.exit_status, 0)48 @skipUnlessPathExists('/bin/sh')49 @skipUnlessPathExists('/bin/echo')50 def test_recipe(self):51 recipe = os.path.join(BASEDIR, "examples", "nrunner", "recipes",52 "runnables", "exec_sh_echo_env_var.json")53 cmd = "%s runnable-run-recipe %s" % (RUNNER, recipe)54 res = process.run(cmd, ignore_status=True)55 lines = res.stdout_text.splitlines()56 if len(lines) == 1:57 first_status = final_status = lines[0]58 else:59 first_status = lines[0]60 final_status = lines[-1]61 self.assertIn("'status': 'started'", first_status)62 self.assertIn("'time': ", first_status)63 self.assertIn("'status': 'finished'", final_status)64 self.assertIn("'stdout': b'Hello world!\\n'", final_status)65 self.assertIn("'time': ", final_status)66 self.assertEqual(res.exit_status, 0)67 def test_noop_valid_kwargs(self):68 res = process.run("%s runnable-run -k noop foo=bar" % RUNNER,69 ignore_status=True)70 self.assertEqual(res.exit_status, 0)71 def test_noop_invalid_kwargs(self):72 res = process.run("%s runnable-run -k noop foo" % RUNNER,73 ignore_status=True)74 self.assertIn(b'Invalid keyword parameter: "foo"', res.stderr)75 self.assertEqual(res.exit_status, 2)76 @skipUnlessPathExists('/bin/env')77 def test_exec_kwargs(self):78 res = process.run("%s runnable-run -k exec -u /bin/env X=Y" % RUNNER,79 ignore_status=True)80 self.assertIn(b"'status': 'finished'", res.stdout)81 self.assertIn(b"X=Y\\n", res.stdout)82 self.assertEqual(res.exit_status, 0)83class TaskRun(unittest.TestCase):84 def test_noop(self):85 res = process.run("%s task-run -i XXXno-opXXX -k noop" % RUNNER,86 ignore_status=True)87 self.assertIn(b"'status': 'finished'", res.stdout)88 self.assertIn(b"'id': 'XXXno-opXXX'", res.stdout)89 self.assertEqual(res.exit_status, 0)90 @skipUnlessPathExists('/bin/uname')91 def test_recipe_exec_1(self):92 recipe = os.path.join(BASEDIR, "examples", "nrunner", "recipes",93 "tasks", "exec", "1-uname.json")94 cmd = "%s task-run-recipe %s" % (RUNNER, recipe)95 res = process.run(cmd, ignore_status=True)96 lines = res.stdout_text.splitlines()97 if len(lines) == 1:98 first_status = final_status = lines[0]99 else:100 first_status = lines[0]101 final_status = lines[-1]102 self.assertIn("'status': 'started'", first_status)103 self.assertIn("'id': 1", first_status)104 self.assertIn("'id': 1", first_status)105 self.assertIn("'status': 'finished'", final_status)106 self.assertEqual(res.exit_status, 0)107 @skipUnlessPathExists('/bin/echo')108 def test_recipe_exec_2(self):109 recipe = os.path.join(BASEDIR, "examples", "nrunner", "recipes",110 "tasks", "exec", "2-echo.json")111 cmd = "%s task-run-recipe %s" % (RUNNER, recipe)112 res = process.run(cmd, ignore_status=True)113 lines = res.stdout_text.splitlines()114 if len(lines) == 1:115 first_status = final_status = lines[0]116 else:117 first_status = lines[0]118 final_status = lines[-1]119 self.assertIn("'status': 'started'", first_status)120 self.assertIn("'id': 2", first_status)121 self.assertIn("'id': 2", first_status)122 self.assertIn("'status': 'finished'", final_status)123 self.assertIn("'stdout': b'avocado'", final_status)124 self.assertEqual(res.exit_status, 0)125 @skipUnlessPathExists('/bin/sleep')126 def test_recipe_exec_3(self):127 recipe = os.path.join(BASEDIR, "examples", "nrunner", "recipes",128 "tasks", "exec", "3-sleep.json")129 cmd = "%s task-run-recipe %s" % (RUNNER, recipe)130 res = process.run(cmd, ignore_status=True)131 lines = res.stdout_text.splitlines()132 # based on the :data:`avocado.core.nrunner.RUNNER_RUN_STATUS_INTERVAL`133 # this runnable should produce multiple status lines134 self.assertGreater(len(lines), 1)135 first_status = lines[0]136 self.assertIn("'status': 'started'", first_status)137 self.assertIn("'id': 3", first_status)138 final_status = lines[-1]139 self.assertIn("'id': 3", first_status)140 self.assertIn("'status': 'finished'", final_status)141 self.assertEqual(res.exit_status, 0)142class ResolveSerializeRun(TestCaseTmpDir):143 @skipUnlessPathExists('/bin/true')144 def test(self):145 cmd = "%s list --resolver --write-recipes-to-directory=%s -- /bin/true"146 cmd %= (AVOCADO, self.tmpdir.name)147 res = process.run(cmd)148 self.assertEqual(b'exec-test /bin/true\n', res.stdout)149 cmd = "%s runnable-run-recipe %s"150 cmd %= (RUNNER, os.path.join(self.tmpdir.name, '1.json'))151 res = process.run(cmd)...
test_hintfiles.py
Source:test_hintfiles.py
...26 def test_wrong_parser(self):27 with self.assertRaises(SettingsError) as context:28 self.wrong_hint.validate_kind_section('tap')29 self.assertTrue('Section tap is not defined' in str(context.exception))30 @skipUnlessPathExists('/bin/true')31 def test_types(self):32 res = self.good_hint.get_resolutions()33 self.assertEqual(len(res), 1)34 self.assertIsInstance(res[0], ReferenceResolution)35 resolutions = res[0].resolutions36 self.assertEqual(len(resolutions), 1)37 self.assertIsInstance(resolutions[0], Runnable)38 @skipUnlessPathExists('/bin/true')39 def test_reference_names(self):40 res = self.good_hint.get_resolutions()[0]41 self.assertEqual(res.reference, '/bin/true')42 self.assertEqual(res.resolutions[0].uri, '/bin/true')43if __name__ == '__main__':...
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!!