diff --git a/examples/tests/assert.py b/examples/tests/assert.py new file mode 100644 index 0000000000000000000000000000000000000000..907c69563621b38ed66e87cfd6b1a830e91d4c91 --- /dev/null +++ b/examples/tests/assert.py @@ -0,0 +1,20 @@ +from avocado import Test + + +class MyException(Exception): + pass + + +def raises_exception(): + raise MyException + + +class Assert(Test): + + def test_assert_raises(self): + with self.assertRaises(MyException): + raises_exception() + + def test_fails_to_raise(self): + with self.assertRaises(MyException): + pass diff --git a/selftests/functional/test_basic.py b/selftests/functional/test_basic.py index 038f2e99038ef2de5019c32bf9e2b203dd38c1f3..8ff9c1a03de8bf615f1b62955f24796a7cdf5aec 100644 --- a/selftests/functional/test_basic.py +++ b/selftests/functional/test_basic.py @@ -361,6 +361,18 @@ class RunnerOperationTest(unittest.TestCase): result)) self.assertIn(b'"status": "FAIL"', result.stdout) + def test_assert_raises(self): + cmd_line = ("%s run --sysinfo=off --job-results-dir %s " + "-- assert.py" % (AVOCADO, self.tmpdir)) + result = process.run(cmd_line, ignore_status=True) + expected_rc = exit_codes.AVOCADO_TESTS_FAIL + self.assertEqual(result.exit_status, expected_rc, + "Avocado did not return rc %d:\n%s" % (expected_rc, + result)) + self.assertIn(b'Assert.test_assert_raises: PASS', result.stdout) + self.assertIn(b'Assert.test_fails_to_raise: FAIL', result.stdout) + self.assertIn(b'PASS 1 | ERROR 0 | FAIL 1 ', result.stdout) + def test_exception_not_in_path(self): os.mkdir(os.path.join(self.tmpdir, "shared_lib")) mylib = script.Script(os.path.join(self.tmpdir, "shared_lib", diff --git a/selftests/functional/test_loader.py b/selftests/functional/test_loader.py index d971e6f82ccacf97cbe3ba98113151cecc33fd51..d3cb9fdad8dbd0df2f843b7f0cbbccc540fbbaea 100644 --- a/selftests/functional/test_loader.py +++ b/selftests/functional/test_loader.py @@ -356,7 +356,8 @@ class LoaderTestFunctional(unittest.TestCase): """ cmd = "%s list examples/tests/:fail" % AVOCADO result = process.run(cmd) - expected = (b"INSTRUMENTED examples/tests/doublefail.py:DoubleFail.test\n" + expected = (b"INSTRUMENTED examples/tests/assert.py:Assert.test_fails_to_raise\n" + b"INSTRUMENTED examples/tests/doublefail.py:DoubleFail.test\n" b"INSTRUMENTED examples/tests/fail_on_exception.py:FailOnException.test\n" b"INSTRUMENTED examples/tests/failtest.py:FailTest.test\n" b"SIMPLE examples/tests/failtest.sh\n")