Skip to content

Commit

Permalink
Remove setup_generated_tests. (#941)
Browse files Browse the repository at this point in the history
`setup_generated_tests` was deprecated many versions ago.
  • Loading branch information
xpconanfan authored Sep 4, 2024
1 parent e445764 commit cf3fbfd
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 84 deletions.
4 changes: 2 additions & 2 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,11 @@ class ManyGreetingsTest(base_test.BaseTestClass):
# When a test run starts, Mobly calls this function to figure out what
# tests need to be generated. So you need to specify what tests to generate
# in this function.
def setup_generated_tests(self):
def pre_run(self):
messages = [('Hello', 'World'), ('Aloha', 'Obama'),
('konichiwa', 'Satoshi')]
# Call `generate_tests` function to specify the tests to generate. This
# function can only be called within `setup_generated_tests`. You could
# function can only be called within `pre_run`. You could
# call this function multiple times to generate multiple groups of
# tests.
self.generate_tests(
Expand Down
26 changes: 2 additions & 24 deletions mobly/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@

# Names of execution stages, in the order they happen during test runs.
STAGE_NAME_PRE_RUN = 'pre_run'
# Deprecated, use `STAGE_NAME_PRE_RUN` instead.
STAGE_NAME_SETUP_GENERATED_TESTS = 'setup_generated_tests'
STAGE_NAME_SETUP_CLASS = 'setup_class'
STAGE_NAME_SETUP_TEST = 'setup_test'
STAGE_NAME_TEARDOWN_TEST = 'teardown_test'
Expand Down Expand Up @@ -372,10 +370,6 @@ def _pre_run(self):
try:
with self._log_test_stage(stage_name):
self.pre_run()
# TODO(angli): Remove this context block after the full deprecation of
# `setup_generated_tests`.
with self._log_test_stage(stage_name):
self.setup_generated_tests()
return True
except Exception as e:
logging.exception('%s failed for %s.', stage_name, self.TAG)
Expand All @@ -397,19 +391,6 @@ def pre_run(self):
requested is unknown at this point.
"""

def setup_generated_tests(self):
"""[DEPRECATED] Use `pre_run` instead.
Preprocesses that need to be done before setup_class.
This phase is used to do pre-test processes like generating tests.
This is the only place `self.generate_tests` should be called.
If this function throws an error, the test class will be marked failure
and the "Requested" field will be 0 because the number of tests
requested is unknown at this point.
"""

def _setup_class(self):
"""Proxy function to guarantee the base implementation of setup_class
is called.
Expand Down Expand Up @@ -906,8 +887,7 @@ def _assert_function_names_in_stack(self, expected_func_names):
def generate_tests(self, test_logic, name_func, arg_sets, uid_func=None):
"""Generates tests in the test class.
This function has to be called inside a test class's `self.pre_run` or
`self.setup_generated_tests`.
This function has to be called inside a test class's `self.pre_run`.
Generated tests are not written down as methods, but as a list of
parameter sets. This way we reduce code repetition and improve test
Expand All @@ -928,9 +908,7 @@ def generate_tests(self, test_logic, name_func, arg_sets, uid_func=None):
arguments as the test logic function and returns a string that
is the corresponding UID.
"""
self._assert_function_names_in_stack(
[STAGE_NAME_PRE_RUN, STAGE_NAME_SETUP_GENERATED_TESTS]
)
self._assert_function_names_in_stack([STAGE_NAME_PRE_RUN])
root_msg = 'During test generation of "%s":' % test_logic.__name__
for args in arg_sets:
test_name = name_func(*args)
Expand Down
59 changes: 1 addition & 58 deletions tests/mobly/base_test_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2128,63 +2128,6 @@ def test_foo(self):
self.assertEqual(class_record.test_name, 'pre_run')
self.assertEqual(bt_cls.results.skipped, [])

# TODO(angli): remove after the full deprecation of `setup_generated_tests`.
def test_setup_generated_tests(self):
class MockBaseTest(base_test.BaseTestClass):

def setup_generated_tests(self):
self.generate_tests(
test_logic=self.logic,
name_func=self.name_gen,
arg_sets=[(1, 2), (3, 4)],
)

def name_gen(self, a, b):
return 'test_%s_%s' % (a, b)

def logic(self, a, b):
pass

bt_cls = MockBaseTest(self.mock_test_cls_configs)
bt_cls.run()
self.assertEqual(len(bt_cls.results.requested), 2)
self.assertEqual(len(bt_cls.results.passed), 2)
self.assertIsNone(bt_cls.results.passed[0].uid)
self.assertIsNone(bt_cls.results.passed[1].uid)
self.assertEqual(bt_cls.results.passed[0].test_name, 'test_1_2')
self.assertEqual(bt_cls.results.passed[1].test_name, 'test_3_4')

# TODO(angli): remove after the full deprecation of `setup_generated_tests`.
def test_setup_generated_tests_failure(self):
"""Test code path for setup_generated_tests failure.
When setup_generated_tests fails, pre-execution calculation is
incomplete and the number of tests requested is unknown. This is a
fatal issue that blocks any test execution in a class.
A class level error record is generated.
Unlike `setup_class` failure, no test is considered "skipped" in this
case as execution stage never started.
"""

class MockBaseTest(base_test.BaseTestClass):

def setup_generated_tests(self):
raise Exception(MSG_EXPECTED_EXCEPTION)

def logic(self, a, b):
pass

def test_foo(self):
pass

bt_cls = MockBaseTest(self.mock_test_cls_configs)
bt_cls.run()
self.assertEqual(len(bt_cls.results.requested), 0)
class_record = bt_cls.results.error[0]
self.assertEqual(class_record.test_name, 'pre_run')
self.assertEqual(bt_cls.results.skipped, [])

def test_generate_tests_run(self):
class MockBaseTest(base_test.BaseTestClass):

Expand Down Expand Up @@ -2308,7 +2251,7 @@ def logic(self, a, b):
self.assertEqual(
actual_record.details,
"'generate_tests' cannot be called outside of the followin"
"g functions: ['pre_run', 'setup_generated_tests'].",
"g functions: ['pre_run'].",
)
expected_summary = (
'Error 1, Executed 1, Failed 0, Passed 0, Requested 1, Skipped 0'
Expand Down

0 comments on commit cf3fbfd

Please sign in to comment.