How to use test_fails_to_raise method in avocado

Best Python code snippet using avocado_python

test_loader.py

Source:test_loader.py Github

copy

Full Screen

1import json2import os3import signal4import stat5import subprocess6import time7import unittest8from avocado.core import exit_codes9from avocado.utils import process, script10from .. import (AVOCADO, BASEDIR, TestCaseTmpDir, skipOnLevelsInferiorThan,11 skipUnlessPathExists)12AVOCADO_TEST_OK = """#!/usr/bin/env python13from avocado import Test14from avocado import main15class PassTest(Test):16 def test(self):17 pass18if __name__ == "__main__":19 main()20"""21AVOCADO_TEST_SLEEP_ELEVEN = """#!/usr/bin/env python22import time23from avocado import Test24from avocado import main25class SleepEleven(Test):26 def test(self):27 time.sleep(10)28 def test_2(self):29 time.sleep(1)30time.sleep(11)31if __name__ == "__main__":32 main()33"""34AVOCADO_TEST_MULTIPLE_CLASSES = """#!/usr/bin/env python35import time36from avocado import Test37from avocado import main38class First(Test):39 def test(self):40 pass41class Second(Test):42 def test(self):43 pass44if __name__ == "__main__":45 main()46"""47AVOCADO_TEST_MULTIPLE_METHODS_SAME_NAME = """#!/usr/bin/env python48from avocado import Test49from avocado import main50class Multiple(Test):51 def test(self):52 raise53 def test(self):54 pass55if __name__ == "__main__":56 main()57"""58NOT_A_TEST = """59def hello():60 print('Hello World!')61"""62PY_SIMPLE_TEST = """#!/usr/bin/env python63def hello():64 print('Hello World!')65if __name__ == "__main__":66 hello()67"""68SIMPLE_TEST = """#!/bin/sh69true70"""71AVOCADO_SIMPLE_PYTHON_LIKE_MULTIPLE_FILES = """#!/usr/bin/env python72# A simple test (executable bit set when saved to file) that looks like73# an Avocado instrumented test, with base class on separate file74from avocado import Test75from avocado import main76from test2 import *77class BasicTestSuite(SuperTest):78 '''79 :avocado: disable80 '''81 def test1(self):82 self.xxx()83 self.assertTrue(True)84if __name__ == '__main__':85 main()86"""87AVOCADO_SIMPLE_PYTHON_LIKE_MULTIPLE_FILES_LIB = """88#!/usr/bin/python89from avocado import Test90class SuperTest(Test):91 def xxx(self):92 print "ahoj"93"""94AVOCADO_TEST_SIMPLE_USING_MAIN = """#!/usr/bin/env python95from avocado import main96if __name__ == "__main__":97 main()98"""99class LoaderTestFunctional(TestCaseTmpDir):100 MODE_0664 = (stat.S_IRUSR | stat.S_IWUSR |101 stat.S_IRGRP | stat.S_IWGRP |102 stat.S_IROTH)103 MODE_0775 = (stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR |104 stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP |105 stat.S_IROTH | stat.S_IXOTH)106 def _test(self, name, content, exp_str, mode=MODE_0664, count=1):107 test_script = script.TemporaryScript(name, content,108 'avocado_loader_test',109 mode=mode)110 test_script.save()111 cmd_line = ('%s -V list %s' % (AVOCADO, test_script.path))112 result = process.run(cmd_line)113 self.assertIn('%s: %s' % (exp_str, count), result.stdout_text)114 test_script.remove()115 def _run_with_timeout(self, cmd_line, timeout):116 current_time = time.time()117 deadline = current_time + timeout118 test_process = subprocess.Popen(cmd_line, stdout=subprocess.PIPE, # pylint: disable=W1509119 stderr=subprocess.PIPE,120 preexec_fn=os.setsid, shell=True)121 while not test_process.poll():122 if time.time() > deadline:123 os.killpg(os.getpgid(test_process.pid), signal.SIGKILL)124 self.fail("Failed to run test under %s seconds" % timeout)125 time.sleep(0.05)126 self.assertEqual(test_process.returncode, exit_codes.AVOCADO_TESTS_FAIL)127 def test_simple(self):128 self._test('simpletest.sh', SIMPLE_TEST, 'simple', self.MODE_0775)129 def test_simple_not_exec(self):130 # 2 because both FileLoader and the TAP loader cannot recognize the test131 self._test('simpletest.sh', SIMPLE_TEST, 'not_a_test', count=2)132 def test_pass(self):133 self._test('passtest.py', AVOCADO_TEST_OK, 'instrumented')134 def test_not_python_module(self):135 # 2 because both FileLoader and the TAP loader cannot recognize the test136 self._test('passtest', AVOCADO_TEST_OK, 'not_a_test', count=2)137 @skipOnLevelsInferiorThan(2)138 def test_sleep_a_lot(self):139 """140 Verifies that the test loader, at list time, does not load the Python141 module and thus executes its contents.142 :avocado: tags=parallel:1143 """144 test_script = script.TemporaryScript('sleepeleven.py',145 AVOCADO_TEST_SLEEP_ELEVEN,146 'avocado_loader_test',147 mode=self.MODE_0664)148 test_script.save()149 cmd_line = ('%s -V list %s' % (AVOCADO, test_script.path))150 initial_time = time.time()151 result = process.run(cmd_line, ignore_status=True)152 test_script.remove()153 actual_time = time.time() - initial_time154 self.assertLess(actual_time, 3.0,155 ("Took more than 3 seconds to list tests. Loader "156 "probably loaded/executed Python code and slept for "157 "eleven seconds."))158 self.assertIn(b'instrumented: 2', result.stdout)159 def test_multiple_class(self):160 self._test('multipleclasses.py', AVOCADO_TEST_MULTIPLE_CLASSES,161 'instrumented', self.MODE_0664, 2)162 def test_multiple_methods_same_name(self):163 self._test('multiplemethods.py', AVOCADO_TEST_MULTIPLE_METHODS_SAME_NAME,164 'instrumented', self.MODE_0664, 1)165 def test_load_not_a_test(self):166 self._test('notatest.py', NOT_A_TEST, 'simple', self.MODE_0775)167 def test_load_not_a_test_not_exec(self):168 # 2 because both FileLoader and the TAP loader cannot recognize the test169 self._test('notatest.py', NOT_A_TEST, 'not_a_test', count=2)170 @skipOnLevelsInferiorThan(2)171 def test_runner_simple_python_like_multiple_files(self):172 """173 :avocado: tags=parallel:1174 """175 mylib = script.TemporaryScript(176 'test2.py',177 AVOCADO_SIMPLE_PYTHON_LIKE_MULTIPLE_FILES_LIB,178 'avocado_simpletest_functional',179 self.MODE_0664)180 mylib.save()181 mytest = script.Script(182 os.path.join(os.path.dirname(mylib.path), 'test.py'),183 AVOCADO_SIMPLE_PYTHON_LIKE_MULTIPLE_FILES)184 os.chdir(BASEDIR)185 mytest.save()186 cmd_line = "%s -V list %s" % (AVOCADO, mytest)187 result = process.run(cmd_line)188 self.assertIn(b'simple: 1', result.stdout)189 # job should be able to finish under 5 seconds. If this fails, it's190 # possible that we hit the "simple test fork bomb" bug191 cmd_line = ("%s run --disable-sysinfo --job-results-dir '%s' -- '%s'"192 % (AVOCADO, self.tmpdir.name, mytest))193 self._run_with_timeout(cmd_line, 5)194 @skipOnLevelsInferiorThan(2)195 def test_simple_using_main(self):196 """197 :avocado: tags=parallel:1198 """199 mytest = script.TemporaryScript("simple_using_main.py",200 AVOCADO_TEST_SIMPLE_USING_MAIN,201 'avocado_simpletest_functional')202 mytest.save()203 os.chdir(BASEDIR)204 # job should be able to finish under 5 seconds. If this fails, it's205 # possible that we hit the "simple test fork bomb" bug206 cmd_line = ("%s run --disable-sysinfo --job-results-dir '%s' -- '%s'"207 % (AVOCADO, self.tmpdir.name, mytest))208 self._run_with_timeout(cmd_line, 5)209 def test_python_unittest(self):210 test_path = os.path.join(BASEDIR, "selftests", ".data", "unittests.py")211 cmd = ("%s run --disable-sysinfo --job-results-dir %s --json - -- %s"212 % (AVOCADO, self.tmpdir.name, test_path))213 result = process.run(cmd, ignore_status=True)214 jres = json.loads(result.stdout_text)215 self.assertEqual(result.exit_status, 1, result)216 exps = [("unittests.Second.test_fail", "FAIL"),217 ("unittests.Second.test_error", "ERROR"),218 ("unittests.Second.test_skip", "CANCEL"),219 ("unittests.First.test_pass", "PASS")]220 for test in jres["tests"]:221 for exp in exps:222 if exp[0] in test["id"]:223 self.assertEqual(test["status"], exp[1], "Status of %s not"224 " as expected\n%s" % (exp, result))225 exps.remove(exp)226 break227 else:228 self.fail("No expected result for %s\n%s\n\nexps = %s"229 % (test["id"], result, exps))230 self.assertFalse(exps, "Some expected result not matched to actual"231 "results:\n%s\n\nexps = %s" % (result, exps))232 def test_list_subtests_filter(self):233 """234 Check whether the subtests filter works for both INSTRUMENTED235 and SIMPLE in a directory list.236 """237 cmd = "%s list examples/tests/:fail" % AVOCADO238 result = process.run(cmd)239 expected = (b"INSTRUMENTED examples/tests/assert.py:Assert.test_fails_to_raise\n"240 b"INSTRUMENTED examples/tests/doublefail.py:DoubleFail.test\n"241 b"INSTRUMENTED examples/tests/fail_on_exception.py:FailOnException.test\n"242 b"INSTRUMENTED examples/tests/failtest.py:FailTest.test\n"243 b"SIMPLE examples/tests/failtest.sh\n")244 self.assertEqual(expected, result.stdout)245 @skipUnlessPathExists('/bin/sh')246 def test_loader_and_external_runner_incompatibility(self):247 """248 Check if the user is inform about incompatibility between loader and249 external_runner.250 """251 test_script = script.TemporaryScript('simpletest.sh', SIMPLE_TEST,252 'avocado_loader_test',253 mode=self.MODE_0775)254 test_script.save()255 cmd = ("%s run --loaders=FOO "256 "--external-runner=/bin/sh %s") % (AVOCADO, test_script.path)257 result = process.run(cmd)258 expected_warning = ("The loaders and external-runner are incompatible."259 "The values in loaders will be ignored.")260 self.assertIn(expected_warning, result.stderr_text)261 cmd = "%s run --external-runner=/bin/sh %s" % (AVOCADO,262 test_script.path)263 result = process.run(cmd)264 self.assertNotIn(expected_warning, result.stderr_text)265 test_script.remove()266if __name__ == '__main__':...

Full Screen

Full Screen

assert.py

Source:assert.py Github

copy

Full Screen

...6class Assert(Test):7 def test_assert_raises(self):8 with self.assertRaises(MyException):9 raises_exception()10 def test_fails_to_raise(self):11 with self.assertRaises(MyException):...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful