Bug 1877642 - Disable browser_fullscreen-tab-close-race.js on apple_silicon !debug...
[gecko.git] / testing / mozbase / docs / _static / structured_example.py
blob3ec1aa8dcc9749d8f6f6380f04852454edd78396
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 import argparse
6 import sys
7 import traceback
8 import types
10 import six
11 from mozlog import commandline, get_default_logger
14 class TestAssertion(Exception):
15 pass
18 def assert_equals(a, b):
19 if a != b:
20 raise TestAssertion("%r not equal to %r" % (a, b))
23 def expected(status):
24 def inner(f):
25 def test_func():
26 f()
28 test_func.__name__ = f.__name__
29 test_func._expected = status
30 return test_func
32 return inner
35 def test_that_passes():
36 assert_equals(1, int("1"))
39 def test_that_fails():
40 assert_equals(1, int("2"))
43 def test_that_has_an_error():
44 assert_equals(2, 1 + "1")
47 @expected("FAIL")
48 def test_expected_fail():
49 assert_equals(2 + 2, 5)
52 class TestRunner(object):
53 def __init__(self):
54 self.logger = get_default_logger(component="TestRunner")
56 def gather_tests(self):
57 for item in six.itervalues(globals()):
58 if isinstance(item, types.FunctionType) and item.__name__.startswith(
59 "test_"
61 yield item.__name__, item
63 def run(self):
64 tests = list(self.gather_tests())
66 self.logger.suite_start(tests=[name for name, func in tests])
67 self.logger.info("Running tests")
68 for name, func in tests:
69 self.run_test(name, func)
70 self.logger.suite_end()
72 def run_test(self, name, func):
73 self.logger.test_start(name)
74 status = None
75 message = None
76 expected = func._expected if hasattr(func, "_expected") else "PASS"
77 try:
78 func()
79 except TestAssertion as e:
80 status = "FAIL"
81 message = str(e)
82 except Exception:
83 status = "ERROR"
84 message = traceback.format_exc()
85 else:
86 status = "PASS"
87 self.logger.test_end(name, status=status, expected=expected, message=message)
90 def get_parser():
91 parser = argparse.ArgumentParser()
92 return parser
95 def main():
96 parser = get_parser()
97 commandline.add_logging_group(parser)
99 args = parser.parse_args()
101 logger = commandline.setup_logging("structured-example", args, {"raw": sys.stdout})
103 runner = TestRunner()
104 try:
105 runner.run()
106 except Exception:
107 logger.critical("Error during test run:\n%s" % traceback.format_exc())
110 if __name__ == "__main__":
111 main()